Docker Cheat Sheet
Basic Docker Commands
1. Getting Help:
docker --help
docker command --help
2. Version:
docker --version
docker version
Working with Docker Images
1. Pull an Image:
docker pull [image_name]
2. List Images:
docker images
3. Remove an Image:
docker rmi [image_name_or_id]
4. Build an Image:
docker build -t [image_name] [path_to_dockerfile]
5. Tag an Image:
docker tag [image_id] [new_image_name]:[tag]
Working with Docker Containers
1. Run a Container:
docker run [options] [image_name]
Common options:
-d
: Run container in background (detached mode)-it
: Run container in interactive mode with a TTY--name
: Assign a name to the container-p
: Publish a container’s port(s) to the host
2. List Running Containers:
docker ps
3. List All Containers (including stopped):
docker ps -a
4. Stop a Running Container:
docker stop [container_id_or_name]
5. Start a Stopped Container:
docker start [container_id_or_name]
6. Remove a Container:
docker rm [container_id_or_name]
7. Remove All Stopped Containers:
docker container prune
Container Information and Management
1. Inspect a Container:
docker inspect [container_id_or_name]
2. View Container Logs:
docker logs [container_id_or_name]
3. Access a Running Container:
docker exec -it [container_id_or_name] /bin/bash
Docker Networks
1. List Networks:
docker network ls
2. Create a Network:
docker network create [network_name]
3. Inspect a Network:
docker network inspect [network_name]
4. Connect a Container to a Network:
docker network connect [network_name] [container_name]
5. Disconnect a Container from a Network:
docker network disconnect [network_name] [container_name]
Docker Volumes
1. List Volumes:
docker volume ls
2. Create a Volume:
docker volume create [volume_name]
3. Inspect a Volume:
docker volume inspect [volume_name]
4. Remove a Volume:
docker volume rm [volume_name]
5. Remove All Unused Volumes:
docker volume prune
Docker Compose
1. Start Services:
docker-compose up
2. Start Services in the Background:
docker-compose up -d
3. Stop Services:
docker-compose down
4. View Service Logs:
docker-compose logs [service_name]
Dockerfile Commands
1. Basic Syntax:
# Use a base image
FROM [base_image]
# Set environment variables
ENV [key]=[value]
# Copy files from host to container
COPY [source] [destination]
# Set the working directory
WORKDIR [path]
# Run commands in the container
RUN [command]
# Expose ports
EXPOSE [port]
# Define the command to run when the container starts
CMD ["executable", "param1", "param2"]
# Alternative to CMD, more flexible
ENTRYPOINT ["executable", "param1", "param2"]
This cheat sheet covers common Docker commands and practices to help you get started and manage your Docker environments effectively.