Docker Cheat Sheet

Docker Cheat Sheet

Images

ImagesCommand
List available imagesdocker images [options]
Download an image from a registrydocker pull [image_name]
Build an image from a Dockerfiledocker build [options] [path_to_dockerfile]
Tag an imagedocker tag [image_name] [new_image_name]
Remove an imagedocker rmi [image_name]
Remove all unused imagesdocker image prune
Export an image as a tar archivedocker save [image_name] -o [output_file]
Import an image from a tar archivedocker load -i [input_file]

Containers

ContainersCommand
Create and start a containerdocker run [options] [image_name] [command]
Start a stopped containerdocker start [container_name]
Stop a running containerdocker stop [container_name]
Remove a stopped containerdocker rm [container_name]
List running containersdocker ps [options]
Create and start a container with an interactive shelldocker run -it [options] [image_name] /bin/bash
Create and start a container in detached modedocker run -d [options] [image_name] [command]
Create and start a container with a namedocker run --name [container_name] [options] [image_name] [command]
Create and start a container with a specific networkdocker run --network [network_name] [options] [image_name] [command]
Create and start a container with a specific volumedocker run -v [host_path]:[container_path] [options] [image_name] [command]
Create and start a container with environment variablesdocker run -e [variable_name]=[variable_value] [options] [image_name] [command]

Dockerfile

DockerfileCommand
Set the base image for the DockerfileFROM [image_name]
Run a command during the image build processRUN [command]
Copy files from the host to the containerCOPY [source] [destination]
Set the working directory for subsequent commandsWORKDIR [directory_path]
Expose a port on the containerEXPOSE [port]
Specify the command to be run when the container startsCMD [command]

Docker Compose

Docker ComposeCommand
Start a stackdocker-compose up [options]
Start a stack in detached modedocker-compose up -d [options]
Stop a stackdocker-compose down [options]
Restart a servicedocker-compose restart [service_name]
List running containers for a stackdocker-compose ps

Volumes

VolumesCommand
Create a named volumedocker volume create [volume_name]
List available volumesdocker volume ls
Inspect a volumedocker volume inspect [volume_name]
Remove a volumedocker volume rm [volume_name]
Mount a volume when creating a containerdocker run -v [volume_name]:[container_path] [options] [image_name] [command]

Networks

Create a network

docker network create [network_name]

List available networks

docker network ls

Inspect a network

docker network inspect [network_name]

Remove a network

docker network rm [network_name]

Connect a container to a network

docker network connect [network_name] [container_name]

Disconnect a container from a network

docker network disconnect [network_name] [container_name]

Logs & Stats

View container logs

docker logs [container_name]

View container logs with timestamps

docker logs -t [container_name]

View container logs in real-time

docker logs -f [container_name]

View container resource usage stats in real-time

docker stats [container_name]

View resource usage stats for all running containers

docker stats

Registries

Upload an image to a registry

docker push [image_name]

Log in to a registry

docker login [options]

Log out from a registry

docker logout [registry]