Essential Docker Commands and Concepts
Installation
Before using Docker, you need to install it on your system. For Debian-based distributions like Ubuntu, you can use the official installation script.
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
Core Container Operations
Listing Containers
To view currently running containers, use the docker ps command.
docker ps
To see all containers, including those that are stopped, add the -a flag.
docker ps -a
Accessing a Container's Shell
You can execute a command, such as a shell, inside a running container using docker exec. The -it flags allocate a pseudo-TTY and keep STDIN open, allowing for interactive sessions.
docker exec -it my_web_server /bin/bash
In this example, my_web_server is the container name, and /bin/bash specifies the shell to run.
Managing Images
To list all images available on the host, use the docker images command.
docker images
To build an image from a Dockerfile, navigate to the directory containing the file and use docker build. The -t flag tags the image with a name and version.
docker build -t mycorp/app:v1.0 .
To remove an image, you first need its ID. You can then use docker rmi.
docker rmi <image_id>
Container Lifecycle
To create a new containre from an image without starting it, use docker create. The -p flag maps a port from the host to the container.
docker create -p 8080:80 --name my_app_container mycorp/app:v1.0
To start a stopped container, use its name or ID.
docker start my_app_container
To stop a running container, use docker stop.
docker stop my_app_container
To remove a stopped container, use docker rm.
docker rm my_app_container
Pushing Images to a Registry
Before pushing an image, you may need to tag it with a registry hostname.
docker tag mycorp/app:v1.0 myregistry.com/mycorp/app:v1.0
Then, you can push the tagged image to the registry.
docker push myregistry.com/mycorp/app:v1.0
Dockerfile Instructions
FROM
Specifies the base image for the new image. This must be the first instruction in the Dockerfile.
FROM ubuntu:22.04
RUN
Executes commands in a new layer on top of the current image and commits the results. It has two forms: shell and exec.
# Shell form
RUN apt-get update && apt-get install -y curl
# Exec form
RUN ["apt-get", "update"]
RUN ["apt-get", "install", "-y", "curl"]
COPY
Copies new files or directories from the <src> to the filesystem of the container at the path <dest>.
COPY . /app
ADD
Similar to COPY, but also has features like extracting local tar archives and accepting URLs as <src>.
ADD https://example.com/config.tar.gz /config/
ADD app_config.conf /etc/app/
EXPOSE
Informs Docker that the container listens on the specified network ports at runtime.
EXPOSE 80 443
ENV
Sets environment variables in the image. These variables will be available for subsequent RUN commands and can be consumed by processes running in the final container.
ENV APP_ENV production
ENV DB_PASSWORD secret
ENTRYPOINT
Configures a container that will run as an executable. It allows you to set a default applicaiton to be used every time a container is created from your image.
ENTRYPOINT ["npm", "start"]
VOLUME
Creates a mount point with the specified name and marks it as holding externally mounted volumes from the native host or other containers.
VOLUME ["/data", "/logs"]
USER
Sets the user name or UID to use when running the image and for any subsequent RUN, CMD, and ENTRYPOINT instructions.
RUN groupadd -r appuser && useradd -r -g appuser appuser
USER appuser
WORKDIR
Sets the working directory for any subsequent RUN, CMD, ENTRYPOINT, COPY, and ADD instructions.
WORKDIR /app
WORKDIR api
# Final working directory is /app/api
ONBUILD
Adds a trigger instruction to the image that will be executed later, when the image is used as the base for another build. The trigger will be inserted immediately after the FROM instruction in the child Dockerfile.
ONBUILD COPY . /app/src
ONBUILD RUN /usr/local/bin/python-build --dir /app/src