Docker Cheatsheet
#
Docker is an open-source platform that automates the deployment, scaling, and management of applications using containerization. Containers are lightweight, portable, and consistent across different environments, making Docker a popular choice for developers and system administrators. Docker was first released in 2013 by Docker, Inc.
Docker Installation
#
Command/Option | Example | Description |
---|
docker –version | docker --version | Check the installed Docker version |
docker info | docker info | Display system-wide information |
Working with Docker Images
#
Command/Option | Example | Description |
---|
docker pull | docker pull ubuntu | Download an image from Docker Hub |
docker images | docker images | List all images on the local machine |
docker rmi | docker rmi ubuntu | Remove an image from the local machine |
Working with Docker Containers
#
Command/Option | Example | Description |
---|
docker run | docker run -it ubuntu | Run a command in a new container |
docker ps | docker ps | List running containers |
docker ps -a | docker ps -a | List all containers, including stopped ones |
docker stop | docker stop container_id | Stop a running container |
docker start | docker start container_id | Start a stopped container |
docker rm | docker rm container_id | Remove a container |
docker exec | docker exec -it container_id bash | Run a command in a running container |
Managing Docker Volumes
#
Command/Option | Example | Description |
---|
docker volume create | docker volume create my_volume | Create a new volume |
docker volume ls | docker volume ls | List all volumes |
docker volume rm | docker volume rm my_volume | Remove a volume |
Working with Docker Networks
#
Command/Option | Example | Description |
---|
docker network create | docker network create my_network | Create a new network |
docker network ls | docker network ls | List all networks |
docker network rm | docker network rm my_network | Remove a network |
Docker Compose
#
Command/Option | Example | Description |
---|
docker-compose up | docker-compose up | Build, (re)create, start, and attach to containers for a service |
docker-compose down | docker-compose down | Stop and remove containers, networks, images, and volumes |
docker-compose build | docker-compose build | Build or rebuild services |
docker-compose logs | docker-compose logs | View output from containers |
Docker Swarm
#
Command/Option | Example | Description |
---|
docker swarm init | docker swarm init | Initialize a swarm |
docker swarm join | docker swarm join --token <token> <manager_ip> | Join a node to a swarm |
docker service create | docker service create --name my_service nginx | Create a service |
docker service ls | docker service ls | List services in a swarm |
docker node ls | docker node ls | List nodes in a swarm |
Dockerfile
#
Command/Option | Example | Description |
---|
FROM | FROM ubuntu:latest | Set the base image for subsequent instructions |
RUN | RUN apt-get update && apt-get install -y python3 | Execute commands in a new layer on top of the current image |
CMD | CMD ["python3", "app.py"] | Provide defaults for an executing container |
COPY | COPY . /app | Copy files from the host to the container |
EXPOSE | EXPOSE 80 | Inform Docker that the container listens on the specified network ports at runtime |
This cheatsheet covers the most commonly used Docker commands and options, helping you to manage Docker images, containers, volumes, networks, and services effectively.