A Docker Deep-Dive Cheatsheet
Docker is something that has taken the devops world by storm the last few years. You are going to need to get good at it sooner rather than later.
Docker is something that has taken the devops world by storm the last few years. You are going to need to learn about it sooner rather than later. ๐
Iโve created a Docker Cheat Sheet that will help you remember and master all the most used Docker commands, and write your own Docker files!
We go over:
What is Docker?
Why do we need Docker?
Basic Docker concepts
Dockerfiles
How to start and interact with Docker containers
How to manage multi-container Docker applications
My goal is that this will be something you can bookmark and return to whenever you need a docker refresher, or canโt quite remember a command.
Todayโs content will be more technical in nature than usual. If you enjoyed this / found it useful, let me know in the comments, and Iโll do more technical deep-dives in the future!
My goal as always with this newsletter is to help you grow both your soft skills, and your technical skills so you can hit that next level in your career.
Alright, letโs dive in! ๐๐ผ
First off: What is docker?
Docker allows you to isolate an application with all its dependencies, configuration and code into lightweight and portable boxes called images. Itโs like a shipping container.
Based on these docker images, your docker containers can be created and started.
If you are familiar with object oriented programming, you can think of an image as a class, and a container as a class instance.
Why do we need docker?
Docker eliminates the common problem of "it worked on my machine", by providing a contained environment that runs the same everywhere. It gives you the confident of running your app in a virtual machine, without all the overhead.
Docker containers can do something VMs canโt. They can be packaged & shipped directly to production environments to simplify development & deploy pipelines.
It gives you the confidence that the environment you built and tested in locally, will be the same environment the code will run in on production.
Basic Docker Concepts
Here are some basic Docker terms you should we understand before we jump in to using Docker in your apps.
Image
A docker image is a read-only template, or set of instructions used to create a container. Docker images can be built from scratch or based on an existing image.
Container
A container is a lightweight, portable, and self-contained executable package that includes everything needed to run an application. Containers are isolated from each other and from the host system, which makes them an excellent choice for deploying applications in the cloud and other environments.
It will contain all your code, packages, libraries, etc.
Dockerfile
A dockerfile is an instruction file for taking a Docker image and building it into a container. It is used to automate the building of Docker images and ensure that they are consistent and reproducible across different environments.
Network
A docker network is primarily used to communicate between various docker containers and the outside world via the host machine where the Docker daemon is running.
Docker Compose
Docker compose is a tool for defining and running multi-container Docker applications, called services. You use a YAML file to configure the services, that can be created/started/stopped with a single command.
Docker files
Remember: A dockerfile is an instruction file for taking a Docker image and building it into a container. It is used to automate the building of Docker images and ensure that they are consistent and reproducible across different environments.
Dockerfile keywords
Here are some common Dockerfile keywords you should become familiar with:
FROM: specifies the base image for the Dockerfile.
RUN: executes a command in a new layer on top of the image and commits the changes.
ADD/COPY: copies files from the host to the container.
CMD: specifies the default command to run when starting a container from the image.
ENTRYPOINT: configures the container to run as an executable.
ENV: sets environment variables for the container.
EXPOSE: specifies the network ports that the container listens on at runtime.
VOLUME: creates a mount point for a volume.
USER: sets the user for the container.
WORKDIR: sets the working directory for the container.
Dockerfile example
Now letโs see what docker commands look like in practice.
Hereโs a basic docker file for nodejs. The comments throughout the file will walk you through each line/command and what they are doing.
Thanks to Sid Palas for the helpful thread on docker files that this example file is based off.
# Pin node version for stability
# Pull an image from the docker hub repository
FROM node:19.4-alpine
# Set NODE_ENV
ENV NODE_ENV production
# Specify a working directory
WORKDIR /usr/src/app
# Copy only files required to install dependencies
COPY ./package.json /www
# Install only production dependencies
RUN npm ci --only=production
# Use non-root user
USER node
# Copy source code AFTER installing dependencies
COPY --chown=node:node ./src/ .
# Indicate an expected port
EXPOSE 3000
CMD [ "node", "index.js" ]
Docker Container commands
Remember: A container is a lightweight, portable, and self-contained executable package that includes everything needed to run an application.
Here are some helpful commands I use on a daily basis to interact with docker containers.
Create and start a new container
You can specify the image to use, set environment variables, and map ports between the container and the host system.
docker run [OPTIONS] <image-id-or-name> [COMMAND] [ARG...]
List all running docker containers
You can see details such as the container ID, image used, and port mappings.
docker ps
View logs for a running container
docker logs <container-id-or-name>
Stop a running container:
You can specify the container ID or name to stop.
docker stop <container-name>
Remove a container:
docker rm <container-name>
Execute a command in a running container
Use this to execute a command in a running container, such as rolling back a migration, or copying a file.
docker exec -d <container-name> yarn knex migrate:rollback
Create an interactive shell on a running container
Use this command to exec into a running container which you can then perform commands on/in it.
docker exec -it <container-name> sh
Docker compose
Remember: Docker compose is a tool for defining and running multi-container Docker applications, called services. You use a YAML file to configure the services, that can be created/started/stopped with a single command.
Docker-compose file
Docker compose allows you to manage multi-container Docker applications.
Itโs used to define and run multiple containers as a single service.
First youโll need to write the Dockerfile(s).
Next youโll create a docker-compose.yml file to build images based on the Dockerfile(s)
Here is an example docker-compose.yml
file:
Keep reading with a 7-day free trial
Subscribe to Level Up Software Engineering ๐ to keep reading this post and get 7 days of free access to the full post archives.