Docker Cheatsheet
Basic Docker commands.
List all running containers:-
sudo docker ps
List all running and stopped containers:-
sudo docker ps -a
List all Docker images:-
sudo docker images
Pull a Docker image:-
sudo docker pull alpine
Run a Docker container using port:-
docker run -itd -p83:80 --name apache2 ubuntu/apache2
Check logs of a Docker container:-
sudo docker logs <container_id>
Run a command in a running Docker container:-
sudo docker exec -it <container_id> bash
Stop a Docker container:-
sudo docker stop <container_id>
Delete a Docker container:-
sudo docker rm <container_id>
Delete a Docker image:-
sudo docker rmi <image_id>
Removing all Images:-
docker image rm $(docker image ls -a -q)
Networking:-
Syntax:
docker network create [OPTIONS] NETWORK_NAME
Creating a Bridge Network:-
docker network create -d bridge MyBridgeNetwork
Creating an Overlay Network:-
docker network create -d overlay MyOverlayNetwork
Creating a Customized Overlay Network:-
Please note that overlay networks require Docker Swarm mode to be enabled, and you need to initialize a Docker Swarm cluster before creating overlay networks.docker network create -d overlay --subnet=192.168.0.0/16 --subnet=192.170.0.0/16 --gateway=192.168.0.100 --gateway=192.170.0.100 --ip-range=192.168.1.0/24 --aux-address="my-router=192.168.1.5" --aux-address="my-switch=192.168.1.6" --aux-address="my-printer=192.170.1.5" --aux-address="my-nas=192.170.1.6" MyOverlayNetwork
Removing a Network:-
docker network rm MyOverlayNetwork
Listing Networks:-
docker network ls
Getting Information About a Network:-
docker network inspect MyOverlayNetwork
Connecting a Running Container to a Network:-
docker network connect MyOverlayNetwork nginx
Connecting a Container to a Network When it Starts:-
docker container run -it -d --network=MyOverlayNetwork nginx
Disconnecting a Container from a Network:-
docker network disconnect MyOverlayNetwork nginx
Volume:-
Syntax:
docker volume create [OPTIONS] VOLUME_NAME
Create a new Docker volume:-
docker volume create <volume_name>
List Docker volumes:-
docker volume ls
Inspect details about a Docker volume:-
docker volume inspect <volume_name>
Remove a Docker volume:-
docker volume rm <volume_name>
Docker Compose:-
To use Docker Compose, you need to have it installed on your system. Once installed, you can create a
docker-compose.yml
file in your project directory, defining the services, networks, and volumes required for your application. Then, you can use thedocker-compose
command to manage your application.Starts the containers defined in the docker-compose.yml file.
docker-compose up
Stops and removes the containers, networks, and volumes defined in the docker-compose.yml file.
docker-compose down
Displays the logs of the services defined in the docker-compose.yml file.
docker-compose logs
Builds the Docker images for the services defined in the docker-compose.yml file.
docker-compose build
Docker Hub:-
Login into Docker:-
docker login -u
Log out from a Docker registry [This command removes the stored credentials from your Docker configuration, effectively logging you out.]
docker logout
docker logout [REGISTRY]
The stored credentials for Docker registries, including Docker Hub, are stored in the Docker configuration file on your system.
cat ~/.docker/config.json
The Docker configuration file is a JSON file that contains various settings and authentication information for Docker, including the stored credentials for Docker registries.
Publish an image to Docker Hub:-
docker push /<image_name>
Search Hub for an image:-
docker search <image_name>
Pull an image from a Docker Hub:-
docker pull <image_name>
Other Docker Cmds:
Check the docker version
docker --version
docker -v
docker run
is used to create and start a new container
docker run -itd --name apache2 ubuntu/apache2
Use the
docker inspect
command to view detailed information about a container or image
docker inspect
Use the
docker port
command to list the port mappings for a container.
docker port <Container-ID/Name>
Use the
docker stats
command to view resource usage statistics for one or more containers. It displays real-time resource usage statistics for running Docker containers. It provides an overview of the CPU, memory, network I/O, and block I/O usage of each container.docker stats docker stats [OPTIONS] [CONTAINER...]
docker stats --no-trunc --no-stream
docker stats --no-stream
docker stats container1 container2
Listed existed containers only
docker ps -f status=exited
Use the
docker top
command to view the processes running inside a container.docker top <Container-ID/Name>
Use the
docker save
command to save an image to a tar archive. It is used to save one or more Docker images as a tar archive. This command allows you to export Docker images from your local Docker environment, which can then be transferred and imported into another Docker environment or distributed to others.
docker save [OPTIONS] IMAGE [IMAGE...]
docker save -o <output_file.tar> <image_name>
Here, IMAGE refers to the name or ID of the Docker image(s) you want to save. You can specify multiple images as separate arguments.
--output
, -o
: Specifies the output file name or path where the exported tar archive should be saved. If not specified, the output is sent to the standard output (stdout). --quiet
, -q
: Suppresses the progress and informational messages during the image export process.--tag
, -t
: Specifies a tag to include when saving the image. You can use this option to save a specific tagged version of an image.
Use the docker load command to load an image from a tar archive. The docker load command is used to load Docker images from a tar archive created with the docker save command. It allows you to import Docker images into your local Docker environment.
docker load [OPTIONS]
docker load -i <input_file.tar>
The docker load command will import the image(s) from the specified tar archive file and make them available in your local Docker environment. You can then use these images to create and run containers as needed.
Please note that the docker load command only imports the Docker images from the tar archive, not the associated containers or their data.
To view the history of a Docker image. It displays the intermediate layers that make up the image, along with information about each layer, such as the command executed to create the layer and the size of the layer.
It can help you understand the build process, identify the impact of changes on the image size, and optimize your Docker image builds by minimizing unnecessary layers.docker history [OPTIONS] IMAGE
docker history <IMAGE> --no-trunc
docker history <IMAGE> -H