The adventures of Docker and the Space Purple Unicorn Association


Docker Desktop


  • Images are snapshots of an environment, easily distributable and ready to be used as templates for containers.
  • Containers are executions of the images, often with configuration added on top, and usually meant for single use.
  • Running a container usually implies creating a new copy, so it is important to clean up regularly.
  • Docker Desktop is a great dashboard that allows us to understand and visualize the lifecycle of images and containers. It could potentially be all you need to use if you only consume images out of the box. However, it is very limited in most cases (even for consumers), and rarely allows the user to configure and interact with the containers adequately.

Building our Docker CLI toolkit


  • All the commands are structured with a main command, a specialising command, an action command, and the name of the object to act on.
  • Everything we did in Docker Desktop (and more!) can be done in the Docker CLI with:
Command Description
Images
docker pull <image> Pull an image from a registry
docker image ls List all images on the system
docker inspect <image> Show detailed information about an image
docker run <image> Run a container from an image
docker image rm <image> Remove an image
Containers
docker logs <container> Show the logs of a container
docker exec <container> <cmd> Run a command in a running container
docker stop <container> Stop a running container
docker kill <container> Immediately stop a running container
docker start <container> Start a stopped container
docker rm <container> Remove a container
System
docker ps List all running containers
docker stats Show live resource usage of containers
docker system prune Remove all stopped containers and unused images
Flag Used on Description
-f inspect Specify the output format
-f logs Follow the logs in real time
-a ps List all containers, including stopped ones
-it run, exec Interactively run a command in a running container
-d run Run a container in the background
-p run Expose a port from the container to the host
--name run Name a container
--rm run Remove the container when it is stopped

Sharing information with containers


  • Volumes and bind mounts help us persist and share data with containers.
  • The syntax for both is very similar, but they have different use cases:
    • Volumes are managed by Docker. They are best for persisting data you do not need to access. docker run -v <volume_name>:<path_in_container> image
    • Bind mounts are managed by the user. They are best for passing data to the container. docker run -v <path_on_host>:<path_in_container> image
  • They both overwrite files in the container, and have their own challenges.

The Docker Hub


  • The Docker Hub is an online repository of container images.
  • The repositories include the container image documentation.
  • Container images may have multiple versions, indicated by tags.
  • The naming convention for Docker container images is: OWNER/CONTAINER_IMAGE_NAME:TAG

Configuring containers


  • Environment variables and overriding the command and entrypoint of containers are the main ways to configure the behaviour of a container. A well structured container will have sensible defaults, but will also allow for configuration to be changed easily.
  • Environment variables can be configured using the flag -e
  • The command can be used to pass parameters to the container, like so:
    docker run <image> <parameters>
    This actually overrides the default command of the container.
  • The entrypoint can also be overridden using the --entrypoint flag.

Creating Your Own Container Images


  • You can create your own container images using a Dockerfile.
  • A Dockerfile is a text file that contains a list of instructions to produce a container image.
  • Each instruction in a Dockerfile creates a new layer in the image.
  • FROM, WORKDIR, RUN, COPY, ENV, ENTRYPOINT and CMD are some of the most important instructions used in a Dockerfile.
  • To build a container image from a Dockerfile you use the command:
    docker build -t <image_name> <context_path>
  • You can run a container from a local image just like any other image, with docker run.

Using Docker Compose


  • Docker Compose is a tool for defining and running multi-container stacks in a YAML file. They can also serve as a way of structuring and documenting docker run commands for single containers.
  • Instructions are saved in a docker-compose.yml file, where services, networks, and volumes are defined.
  • Each service is a separate container, and it can be fully configured from within the file.
  • Bind mounts and volumes can be declared for each service, and they can be shared between containers too.
  • You can define networks, which can be used to connect or isolate containers from each other.
  • All the services, volumes and networks are started together using the docker compose up command.
  • They can be stopped using the docker compose down command.
  • Container images can be built as the services are spun up by using the --build flag.
  • The order in which services start can be controlled using the depends_on key.
  • A healthcheck can be defined to verify the status of a service. These are commands run from within the container to make sure it is ready.

And they lived happily ever after