Cheat Sheet Docker¶
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:
- Starts all services defined in thedocker-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:
- Stops all services defined in thedocker-compose.yml
file. - With the--volumes
or-v
flag, it also removes the volumes defined in thedocker-compose.yml
file.Build or rebuild services:
- Builds all services defined in thedocker-compose.yml
file that have abuild
directive. - You can also specify specific services to build.List the status of services:
- Lists the status of all services defined in thedocker-compose.yml
file. - Can show status likeUp
,Exit
, orRestarting
.Execute a command in a service:
- Executes a command in a running service. - Useful for debugging or interacting with a service.View output from services:
- Displays the logs from services. - Useful for debugging.Create and start containers:
- Starts existing containers for a service.Stop running containers without removing them:
- Stops running containers without removing them.
Containers¶
List Docker containers:
- 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):
- Lists all Docker containers, including those that are stopped. - Shows details similar to the 'docker ps' command.Start a Docker container:
- 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:
- 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:
- Removes a stopped Docker container. - Use the '-f' option to force-remove a running container.Execute a command in a running Docker container:
- Executes the specified command within a running Docker container. - Use the '-it' option for an interactive session.View logs of a Docker 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:
- 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:
- Uploads a Docker image from the local system to a registry (such as Docker Hub). - Requires authentication with the registry.
Images¶
List 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:
- 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 thelatest
tag.Build a Docker image from a Dockerfile:
- 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 thelatest
image and you only change the source codeRemove a Docker image:
- 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:
- 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 usingdocker login
before pushing an image.Save a Docker image to a tar archive:
- 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:
- 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:
- 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:
- 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:
- Lists all the networks that Docker knows about. - Networks could be of different types, includingbridge
,host
,overlay
,none
, andmacvlan
.Inspect a 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:
- Creates a new network. - You can specify the type of network to create with the--driver
option.Connect a container to a network:
- 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:
- 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:
- 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:
- 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:
- 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:
- Lists all volumes in Docker. - This command helps you keep track of all the volumes on your Docker host.Inspect a 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:
- Removes a volume. - A volume can only be removed if no containers are using it.Prune volumes:
- Removes all unused volumes. - This command is useful for freeing up storage space on your Docker host.
Swarm¶
Initialize a Docker Swarm:
- Initializes a new Docker Swarm. - The Docker daemon targeted by this command becomes a manager in the new Swarm.Join a Docker Swarm:
- Adds the Docker daemon targeted by this command to an existing Docker Swarm as a worker or manager node. - Requires a token generated bydocker swarm init
ordocker swarm join-token
.Display join token:
- Displays the join token for worker or manager nodes. - This token is required for a node to join the Swarm.Create a new service:
- Creates a new service in the Docker Swarm. - The service can be scaled across different nodes in the Swarm.Update a service:
- Updates a service in the Docker Swarm. - Options include changing the image, update strategy, environment variables, and more.Remove a service:
- Removes the specified service from the Docker Swarm.Inspect a service:
- Provides detailed information about the specified service.List services:
- Lists all services in the Docker Swarm.Display the tasks of a service:
- Lists all tasks for a specific service, showing which nodes are running them and their status.Leave a Swarm:
- Causes the node to leave the Swarm it's part of. - If the node is a manager, the Swarm must be demoted first.