Docker Notes.

My notes on learning Docker.

Cargo ship

I recently attended a course about Docker (and Kubernetes), and here I want to share my notes. They are by any mean exhaustive, yet contain some valuable information and an overview about the technology and its core concepts.

Under the hood emoji 🐳

  • It's based on a linux kernel VM;
  • Uses namespaces (to isolate the disk);
  • Uses c-groups (to isolate other resources like CPU, RAM, network, ...).

Docker Images

  • A Dockerfile is used to define the steps to create an image;
  • It creates and uses a new temporary image and container for every single step till the final image is created and "emitted";
  • Every successive step is based on the result of the previous one (ie. it takes the disk snapshot of the previous step and then executes the current step on it);
  • A docker image is in a nutshell a disk snapshot + an initial execution command;
  • You can overwrite the inital command when running an image.

Basic CLI commands

  • docker build [-f FILENAME]: build an image;
  • -f FILENAME: specify a specific Dockerfile with a different name (ex. Dockerfile.dev);
  • docker run IMAGE_ID: run an image in a new container;
  • docker exec [-it] IMAGE_ID: execute a command in an already running container (ex. open a new shell);
  • -it maps stdin/stdout/stderr to the host console to make the container output interactive and well formatted;
  • docker ps: list all the running containers.

Logs

  • You can retrieve the logs/output of a running background container not attached to a shell;
  • docker logs CONTAINER_ID: retrieve all the logs/output of the container.

Ports

  • You can map ports of the host system to the docker containers;
  • docker run -p 3000:8080 IMAGE_ID: map the port 3000 of the host system to the port 8080 inside the docker container.

Docker compose

  • Docker-compose can run multiple images at once and creates a virtual network for them to easily communicate;
  • You can reference the networked containers within the docker network with the label you provided in the docker-compose.yml file;
  • In the docker-compose.yml file you can specify the images, the ports, the volumes, the env variables, etc.

Volumes

  • Docker volumes allow you to access files on the host machine (as a reference) so that you can develop without rebuilding an image on every single change;
  • It behaves like a shared HDD space between the host system and the docker container;
  • docker run -v $(pwd):/app IMAGE_ID: shares the content of the current working directory inside the location /app of the docker container;
  • It overwrites everything in the target folder, unless you specify a previous folder without a target, in that case it will assume that that folder needs to be kept (example: docker run -v /app/node_modules $(pws):/app IMAGE_ID will maintain the /app/node_modules folder intact in the container while copying all the other folders and files into /app).

Multi-phase dockerfile

  • You can use multiple base images (each base image used defines a distinct new phase);
  • You can take the output files/disk snapshot from a previous phase and utilize it in a successive phase;
  • When it finishes all the images gets destroyed except for the last one, which defines the resulting image emitted/created.

For updates, insights or suggestions, feel free to post a comment below! emoji 🙂

×