Docker Cheatsheet
Basic Docker commands.
List all running containers:-
sudo docker psList all running and stopped containers:-
sudo docker ps -aList all Docker images:-
sudo docker imagesPull a Docker image:-
sudo docker pull alpineRun a Docker container using port:-
docker run -itd -p83:80 --name apache2 ubuntu/apache2Check logs of a Docker container:-
sudo docker logs <container_id>Run a command in a running Docker container:-
sudo docker exec -it <container_id> bashStop 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_NAMECreating a Bridge Network:-
docker network create -d bridge MyBridgeNetworkCreating an Overlay Network:-
docker network create -d overlay MyOverlayNetworkCreating 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" MyOverlayNetworkRemoving a Network:-
docker network rm MyOverlayNetworkListing Networks:-
docker network lsGetting Information About a Network:-
docker network inspect MyOverlayNetworkConnecting a Running Container to a Network:-
docker network connect MyOverlayNetwork nginxConnecting a Container to a Network When it Starts:-
docker container run -it -d --network=MyOverlayNetwork nginxDisconnecting a Container from a Network:-
docker network disconnect MyOverlayNetwork nginxVolume:-
Syntax:
docker volume create [OPTIONS] VOLUME_NAMECreate a new Docker volume:-
docker volume create <volume_name>List Docker volumes:-
docker volume lsInspect 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.ymlfile in your project directory, defining the services, networks, and volumes required for your application. Then, you can use thedocker-composecommand to manage your application.Starts the containers defined in the docker-compose.yml file.
docker-compose upStops and removes the containers, networks, and volumes defined in the docker-compose.yml file.
docker-compose downDisplays the logs of the services defined in the docker-compose.yml file.
docker-compose logsBuilds the Docker images for the services defined in the docker-compose.yml file.
docker-compose buildDocker Hub:-
Login into Docker:-
docker login -uLog 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.jsonThe 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 -vdocker runis used to create and start a new container
docker run -itd --name apache2 ubuntu/apache2Use the
docker inspectcommand to view detailed information about a container or image
docker inspectUse the
docker portcommand to list the port mappings for a container.
docker port <Container-ID/Name>Use the
docker statscommand 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 container2Listed existed containers only
docker ps -f status=exitedUse the
docker topcommand to view the processes running inside a container.docker top <Container-ID/Name>Use the
docker savecommand 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
Comments
Join the discussionNo comments yet. Be the first to comment.