Docker is a very popular tool introduced to make it easier for developers to create, deploy, and run applications using containers. A container is a utility provided by Docker to package and run an application in a loosely isolated environment. Containers are lightweight and contain everything needed to run an application, such as libraries and other dependencies packed by the developer during the application’s packaging process. This assures developers that their application can be run on any other machine. Here, we’re going to provide you with an ultimate Docker Cheat Sheet that will help you to learn Docker Commands easily.
This Docker command cheatsheet is a summary of commonly used Docker commands and their options, as well as other useful information related to Docker. It covers all the important commands required for Docker operations, including Docker installation, building, running, shipping, and cleaning up, as well as interaction with containers. This Docker cheat sheet is useful for both – DevOps freshers who’re learning Docker and experienced Docker users who need to recall a specific command or option but may not remember all the details.
Docker Cheat Sheet – Table of Content
Pre-requisite: Docker, DockerHub
The below Docker cheat sheet contains commands to manage the docker containers, images, network, volumes, building running, and deploying containers and also covered commands related to Docker Compose.
Docker Commands Cheat Sheet
The Docker cheat sheet will help you as a reference guide from where you can quickly read of mostly used common commands of Docker. The cheat sheet will help as a handy guide for developers and other system administrations who are working with Docker. Let’s get started:
Installation Commands
curl -sSL https://gcurl -fsSL https://get.docker.com -o get-docker.sh && sudo sh get-docker.sh
|
Docker Login Commands
docker login
|
docker logout
|
Image Management Commands
docker image pull nginx
|
docker image pull <Name of The Image>:<Tag>
|
Image Transfer Commands
docker image push <usernameofregistry:Imagename: tag>
|
docker image push eon01/nginx localhost:5000/myadmin/nginx
|
Containers Management Commands
docker container start nginx
|
docker container stop nginx
|
docker container restart nginx
|
docker container pause nginx
|
docker container unpause nginx
|
docker container wait nginx
|
docker container kill nginx
|
docker container kill -s HUP nginx
|
docker container attach nginx
|
docker ps
|
docker container ls
|
docker logs infinite
|
docker container logs infinite -f
|
docker container inspect infinite
|
docker container inspect –format ‘{{ .NetworkSettings.IPAddress }}’ $(docker ps -q)
|
docker system events infinite
|
docker container port infinite
|
docker container top infinite
|
docker container stats infinite
|
docker container diff infinite
|
Docker Image Management Commands
docker image ls
|
docker build.
|
docker build github.com/creack/docker-firefox
|
docker build – < Dockerfile
|
docker build -t eon/infinite.
|
docker build -f myOtherDockerfile.
|
curl example.com/remote/Dockerfile | docker build -f – .
|
docker image rm nginx
|
docker image load < ubuntu.tar.gz
|
docker image save busybox > ubuntu.tar
|
docker image history
|
docker container commit nginx
|
docker image tag nginx eon01/nginx
|
docker image push eon01/nginx
|
Docker Network Commands
docker network create -d overlay MyOverlayNetwork
|
docker network create -d bridge MyBridgeNetwork
|
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
|
docker network rm MyOverlayNetwork
|
docker network ls
|
docker network inspect MyOverlayNetwork
|
docker network connect MyOverlayNetwork nginx
|
docker container run -it -d –network=MyOverlayNetwork nginx
|
docker network disconnect MyOverlayNetwork nginx
|
Docker Exposing Ports Commands
EXPOSE <port_number>
|
docker run -p $HOST_PORT:$CONTAINER_PORT –name <container_name> -t <image>
|
Docker Commands Removing Containers, Images, Volumes, And Networks
docker container rm nginx
|
docker container rm -v nginx
|
docker container rm $(docker container ls -a -f status=exited -q)
|
docker container rm `docker container ls -a -q`
|
docker image rm nginx
|
docker image rm $(docker image ls -f dangling=true -q)
|
docker image rm $(docker image ls -a -q)
|
docker image rm -f $(docker image ls | grep “^” | awk “{print $3}”)
|
docker container stop $(docker container ls -a -q) && docker container rm $(docker container ls -a -q)
|
docker volume rm $(docker volume ls -f dangling=true -q)
|
docker system prune -f
|
docker system prune -a
|
Docker Swarm Commands
curl -ssl https://get.docker.com | bash
|
docker swarm init –advertise-addr 192.168.10.1
|
docker swarm join-token worker
|
docker swarm join-token manager
|
docker service ls
|
docker node ls
|
docker service create –name vote -p 8080:80 instavote/vote
|
docker service ps
|
docker service scale vote=3
|
docker service update –image instavote/vote:movies vote
|
docker service update –force –update-parallelism 1 –update-delay 30s nginx
|
Docker file Commands
Specifies the base image for the build
|
FROM ubuntu:latest
|
Executes a command inside the container during build time
|
RUN apt-get update && apt-get install -y curl
|
Specifies the default command to run when the container starts
|
CMD [“npm”, “start”]
|
Informs Docker that the container listens on specific network ports at runtime
|
EXPOSE 80/tcp
|
Sets environment variables inside the container
|
ENV NODE_ENV=production
|
Copies files or directories from the build context into the container
|
COPY app.js /usr/src/app/
|
Similar to COPY but supports additional features like URL retrieval and decompression
|
ADD https://example.com/file.tar.gz /usr/src/
|
Sets the working directory for subsequent instructions
|
WORKDIR /usr/src/app
|
Defines variables that users can pass at build-time to the builder with the docker build command
|
ARG VERSION=1.0
|
Configures a container to run as an executable
|
ENTRYPOINT [“python”, “app.py”]
|
Creates a mount point and assigns it to a specified volume
|
VOLUME /data
|
Sets the user or UID to use when running the image
|
USER appuser
|
Adds metadata to an image in the form of key-value pairs
|
LABEL version=”1.0″ maintainer=”John Doe
|
Configures commands to run when the image is used as the base for another build
|
ONBUILD ADD . /app/src
|
Docker Volume Commands
Creates a named volume
|
docker volume create mydata
|
Lists the available volumes
|
docker volume ls
|
Displays detailed information about a volume
|
docker volume inspect mydata
|
Removes one or more volumes
|
docker volume rm mydata
|
Removes all unused volumes
|
docker volume prune
|
Docker CP commands
Copies files or directories from the local filesystem to the specified container
|
docker cp myfile.txt mycontainer:/usr/src/app/
|
Copies files or directories from the specified container to the local filesystem
|
docker cp mycontainer:/usr/src/app/result.txt /tmp/result/
|
Docker Security Commands (Docker Scout)
[experimental] Compare two images and display differences
|
docker scout compare image1:tag image2:tag
|
Display CVEs identified in a software artifact
|
docker scout cves image: tag
|
Quick overview of an image
|
docker scout quickview image: tag
|
Display available base image updates and remediation recommendations
|
docker scout recommendations image:tag
|
Show Docker Scout version information
|
docker scout version
|
Conclusion
In conclusion, this Docker cheat sheet helps you with a quick revision of all the Docker commands that are required for Docker operations, including Docker installation, building, running, shipping, and cleaning up, as well as interaction with containers.
FAQs on Docker CheatSheet
Q1. What is the architecture of Docker?
Answer:
Docker follows a client-server architecture. The Docker client communicates with the Docker daemon, which is responsible for building, running, and managing Docker containers. The client and daemon can run on the same host, or the client can connect to a remote daemon.
Q2. Which language is Docker built on?
Answer:
Docker is built using Go programming language because of its advantage of several features of the Linux kernel to deliver its functionality.
Q3. Does Docker require coding?
Answer:
No, Docker does not require any prior coding knowledge. It is a containerization platform that enables developers to package, deploy, and run applications using containers.
Q4. Are Docker secrets safe?
Answer:
You can use Docker secrets to centrally manage this data and securely transmit it to only those containers that need access to it. Secrets are encrypted during transit and at rest in a Docker swarm.
Q5. How many types of volumes are there in Docker?
Answer:
Docker supports three types of volumes:
a) Named Volumes: These are volumes with a user-defined name that can be used across multiple containers.
b) Bind Mounts: These are directories on the host machine that are mounted into a container, allowing direct access to the host’s file system.
c) tmpfs Mounts: These are volumes stored in the host’s memory, allowing fast read and write operations but with limited size and durability.
Q6. What is the flag in Docker?
Answer:
In Docker, a flag is a command-line option that modifies the behavior of a Docker command. Flags are used to provide additional instructions or parameters to Docker commands, allowing you to customize the execution according to your needs.
Q7. Why is Docker used in DevOps?
Answer:
Docker is widely used in DevOps practices due to its ability to create reproducible and portable environments. With Docker, developers can package their applications and dependencies into containers, ensuring consistent behavior across different stages of the software development lifecycle. Docker also facilitates the automation of deployment, testing, and scaling processes, enabling faster and more reliable software delivery in DevOps pipelines.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
28 Jun, 2023
Like Article
Save Article