Docker Cheat Sheet

Docker Cheat Sheet

Compose

These commands need to be executed in the directory containing the docker-compose.yml file, or you need to specify the file with the -f option.

Start a Compose application:

docker-compose up [OPTIONS] [SERVICE...]
  • Starts all services defined in the docker-compose.yml file.
  • With the -d flag, the services will run in detached mode (background).
  • You can also specify specific services to start.

Stop a Compose application:

docker-compose down [OPTIONS]
  • Stops all services defined in the docker-compose.yml file.
  • With the --volumes or -v flag, it also removes the volumes defined in the docker-compose.yml file.

Build or rebuild services:

docker-compose build [OPTIONS] [SERVICE...]
  • Builds all services defined in the docker-compose.yml file that have a build directive.
  • You can also specify specific services to build.

List the status of services:

docker-compose ps [OPTIONS] [SERVICE...]
  • Lists the status of all services defined in the docker-compose.yml file.
  • Can show status like Up, Exit, or Restarting.

Execute a command in a service:

docker-compose exec [OPTIONS] SERVICE COMMAND [ARGS...]
  • Executes a command in a running service.
  • Useful for debugging or interacting with a service.

View output from services:

docker-compose logs [OPTIONS] [SERVICE...]
  • Displays the logs from services.
  • Useful for debugging.

Create and start containers:

docker-compose start [OPTIONS] [SERVICE...]
  • Starts existing containers for a service.

Stop running containers without removing them:

docker-compose stop [OPTIONS] [SERVICE...]
  • Stops running containers without removing them.

Containers

List Docker containers:

docker ps
  • Lists all running Docker containers on the local system.
  • Shows details such as container ID, image, command, creation time, status, ports, and names.

List all Docker containers (running and stopped):

docker ps -a
  • Lists all Docker containers, including those that are stopped.
  • Shows details similar to the ‘docker ps’ command.

Start a Docker container:

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
  • Starts a new Docker container using the specified image.
  • You can provide options like ‘–name’ to give the container a custom name, and ‘-p’ to map ports.

Stop a Docker container:

docker stop [OPTIONS] CONTAINER
  • Stops a running Docker container by sending a SIGTERM signal, followed by a SIGKILL if the container doesn’t stop within the specified timeout.

Remove a Docker container:

docker rm [OPTIONS] CONTAINER
  • Removes a stopped Docker container.
  • Use the ‘-f’ option to force-remove a running container.

Execute a command in a running Docker container:

docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
  • Executes the specified command within a running Docker container.
  • Use the ‘-it’ option for an interactive session.

View logs of a Docker container:

docker logs [OPTIONS] CONTAINER
  • Displays the logs of a running or stopped Docker container.
  • Use the ‘–follow’ or ‘-f’ option to stream the logs in real-time.

Pull a Docker image from a registry:

docker pull [OPTIONS] NAME[:TAG|@DIGEST]
  • Downloads a Docker image from a registry (such as Docker Hub) to the local system.
  • You can specify the image version using the ‘TAG’ or ‘DIGEST’ value.

Push a Docker image to a registry:

docker push [OPTIONS] NAME[:TAG]
  • Uploads a Docker image from the local system to a registry (such as Docker Hub).
  • Requires authentication with the registry.

Images

List Docker images:

docker images
  • Lists all the Docker images available on the local system.
  • Shows details such as repository, tag, image ID, creation time, and size.

Pull a Docker image from a registry:

docker pull <image_name>:<tag>
  • Downloads the specified Docker image from a registry (e.g., Docker Hub) to the local system.
  • Replace <image_name> with the name of the image and <tag> with the desired version (e.g., latest).
  • If the <tag> is not specified, Docker defaults to pulling the latest tag.

Build a Docker image from a Dockerfile:

docker build -t <image_name>:<tag> <build_context>
  • Builds a Docker image using the Dockerfile and build context provided.
  • Replace <image_name> with the desired name for the image and <tag> with the version (e.g., v1).
  • Replace <build_context> with the path to the build context (e.g., . for the current directory).
  • The -t flag is used to tag the image with a name and version.

If changes are not detected by Docker, such as an edit within the source files, then you need to use the --no-cache attribute if you want to overwrite an existing tagged image. This is often the case for instance with the latest image and you only change the source code

Remove a Docker image:

docker rmi <image_id>
  • Removes a Docker image from the local system.
  • Replace <image_id> with the ID of the image you want to remove.
  • To remove multiple images, provide a space-separated list of image IDs.
  • You cannot remove an image if it is currently being used by a running or stopped container.

Push a Docker image to a registry:

docker push <repository>:<tag>
  • Pushes a Docker image to a remote registry (e.g., Docker Hub).
  • Replace <repository> with the name of the repository and <tag> with the version (e.g., latest).
  • You must be logged in to the registry using docker login before pushing an image.

Save a Docker image to a tar archive:

docker save -o <output_file> <image_name>:<tag>
  • Saves a Docker image to a tar archive file.
  • Replace <output_file> with the desired file path (e.g., image.tar) and <image_name> and <tag> with the image and version to be saved.
  • The -o flag is used to specify the output file path.

Load a Docker image from a tar archive:

docker load -i <input_file>
  • Loads a Docker image from a tar archive file.
  • Replace <input_file> with the path to the tar archive file containing the Docker image.
  • The -i flag is used to specify the input file path.

Inspect a Docker image:

docker image inspect <image_name>:<tag>
  • Retrieves detailed information about a Docker image in JSON format.
  • Replace <image_name> with the name of the image and <tag> with the desired version (e.g., latest).
  • The output includes information such as the image ID, creation time, size, and layers.

Inspect a Docker image manifest:

docker manifest inspect <repository>:<tag>
  • Retrieves the image manifest and its metadata for a specific image from a registry.
  • Replace <repository> with the name of the repository and <tag> with the desired version (e.g., latest).
  • The output provides information about the image’s platform, architecture, and layers.

Networking

List all networks:

docker network ls
  • Lists all the networks that Docker knows about.
  • Networks could be of different types, including bridge, host, overlay, none, and macvlan.

Inspect a network:

docker network inspect NETWORK
  • Provides detailed information about a specific network.
  • You can use either the network ID or the network name to specify the network to inspect.

Create a network:

docker network create [OPTIONS] NETWORK
  • Creates a new network.
  • You can specify the type of network to create with the --driver option.

Connect a container to a network:

docker network connect NETWORK CONTAINER
  • Connects a running container to a network.
  • You can use either the network ID or the network name, and the container ID or container name to specify the network and the container.

Disconnect a container from a network:

docker network disconnect NETWORK CONTAINER
  • Disconnects a container from a network.
  • You can use either the network ID or the network name, and the container ID or container name to specify the network and the container.

Remove a network:

docker network rm NETWORK
  • Removes a network.
  • You can use either the network ID or the network name to specify the network to remove.
  • Containers must be disconnected from the network before it can be removed.

Create an overlay network in Swarm mode:

docker network create -d overlay NETWORK
  • Creates a new overlay network that spans all the nodes participating in the swarm.
  • Requires Docker Swarm mode to be enabled.

Storage

Create a volume:

docker volume create [OPTIONS] [VOLUME]
  • Creates a new volume that containers can use for storage.
  • Docker volumes are the preferred way to persist data produced by and used by Docker containers.

List volumes:

docker volume ls
  • Lists all volumes in Docker.
  • This command helps you keep track of all the volumes on your Docker host.

Inspect a volume:

docker volume inspect [VOLUME]
  • Returns detailed information about a volume.
  • This can be used to find out more about the volume, such as its driver, mount point, or options.

Remove a volume:

docker volume rm [VOLUME]
  • Removes a volume.
  • A volume can only be removed if no containers are using it.

Prune volumes:

docker volume prune
  • Removes all unused volumes.
  • This command is useful for freeing up storage space on your Docker host.

Swarm

Initialize a Docker Swarm:

docker swarm init [OPTIONS]
  • Initializes a new Docker Swarm.
  • The Docker daemon targeted by this command becomes a manager in the new Swarm.

Join a Docker Swarm:

docker swarm join [OPTIONS] HOST:PORT
  • Adds the Docker daemon targeted by this command to an existing Docker Swarm as a worker or manager node.
  • Requires a token generated by docker swarm init or docker swarm join-token.

Display join token:

docker swarm join-token [worker|manager]
  • Displays the join token for worker or manager nodes.
  • This token is required for a node to join the Swarm.

Create a new service:

docker service create [OPTIONS] IMAGE [COMMAND] [ARG...]
  • Creates a new service in the Docker Swarm.
  • The service can be scaled across different nodes in the Swarm.

Update a service:

docker service update [OPTIONS] SERVICE
  • Updates a service in the Docker Swarm.
  • Options include changing the image, update strategy, environment variables, and more.

Remove a service:

docker service rm SERVICE
  • Removes the specified service from the Docker Swarm.

Inspect a service:

docker service inspect [OPTIONS] SERVICE
  • Provides detailed information about the specified service.

List services:

docker service ls
  • Lists all services in the Docker Swarm.

Display the tasks of a service:

docker service ps SERVICE
  • Lists all tasks for a specific service, showing which nodes are running them and their status.

Leave a Swarm:

docker swarm leave [OPTIONS]
  • Causes the node to leave the Swarm it’s part of.
  • If the node is a manager, the Swarm must be demoted first.
Last modified July 21, 2024: update (e2ae86c)