Working on Docker? You obviously can’t rely on your memory for the usual machine commands, and so, we present to you a comprehensive Docker cheat sheet.
Before working on Docker, it is important to become familiar with the associated terminology – Dockfires, images, etc.
But first, let’s understand what Docker is and why it should be used.
Check out our free courses to get an edge over the competition
What is Docker?
Also known as container manager, Docker is an open platform that helps developers develop and run applications.
But if Docker is a container manager, what is a container?
An environment in a capsule, running on shallow abstractions, meant for providing developers with virtual isolation to run processes without obstructions is called a container.
Docker is basically a place where you can manage these containers containing packaged software.
Check out upGrad’s Advanced Certification in Blockchain
Why use Docker?
Docker allows developers to build any kind of app in whichever language the client needs by using any toolchain that they prefer. Such apps are also portable and flexible to run anywhere, such as on Windows, Ubuntu, and Red Hat.
Since Docker tracks all the changes and manages them, it gets easier for the systems administrator to see how the app is being built and how it would work.
All in all, Docker is useful when you want to build high-quality apps faster.
Check out upGrad’s Advanced Certification in DevOps
Learn more: Introduction to Docker Networking: Workflow, Networking
Explore Our Software Development Free Courses
Prerequisites for Working on Docker
1. Docker plugin along with zsh configuration manager to autocomplete Docker commands.
2. Linux – 3.10.x kernel or more
3. MacOS – 10.8 Mountain Lion or newer
Remember that after installing Docker, you need to check its version to know the features that are compatible with it. Here’s how you can find it out.
Use the command docker version to check the version of the Docker that you’re using.
Use the following command to know the server version.
$ docker version –format ‘{{.Server.Version}}’
Read: Docker Salary in India
Installing Docker
Here are links to install Docker on different operating systems.
- Linux – curl -sSL https://get.docker.com/ | sh
- Max – https://download.docker.com/mac/stable/Docker.dmg
- Windows: https://download.docker.com/win/stable/InstallDocker.msi
Let’s now move on to container commands.
Explore our Popular Software Engineering Courses
Container Commands
What threads are to processes, containers are to virtual machines.
Here’s the chain of commands for different functions to perform on a container.
Lifecycle
- docker create – to create a container
- docker rename – to rename a container
- docker run – to create and start a container in one operation
- docker rm – to delete a container
- docker update – to update a container’s resource limits
In order to keep the container running, use docker run -td container_id.
Starting and stopping containers
- docker start nginx – to start a container
- docker stop nginx – to stop a container
- docker restart nginx – to restart a container
- docker pause nginx – to pause a container
- docker unpause nginx – to unpause a container
- docker wait nginx – to block a container
- docker kill nginx – to send a SIGKILL
- docker attach nginx – to attach to one container to an existing container
upGrad’s Exclusive Software Development Webinar for you –
SAAS Business – What is So Different?
Image Commands in Docker
- Creating an image from a Dockerfile: Docker build [URL], and Docker build -t for building an image from a Dockerfile in the current directory and tags the image
- Pulling an image from a registry: Docker pull [IMAGE]
- Pushing an image to a registry: docker push [IMAGE]
- Creating an image from a tarball: docker import [URL/FILE]
- Creating an image from a container: Docker commit [CONTAINER] [NEW_IMAGE_NAME]
- Removing an image: Docker rmi [IMAGE]
- Loading an image from a tar archive or stdin: docker load [TAR_FILE/STDIN_FILE]
- Saving an image to a tar archive, streamed to STDOUT with all parent layers, tags, and versions: Docker save [IMAGE] > [TAR_FILE]
In-Demand Software Development Skills
Container and Image Information Commands
To manage the containers after setting them up, it is important to get all the relevant information out of them. Use to following commands to get image and container details in Docker.
- docker ps – to list all the running containers
- docker ps -a – to list both stopped and running containers
- docker logs [CONTAINER] – to get all logs from a running container
- docker inspect [OBJECT_NAME/ID] – to list low-level information on Docker objects
- docker events [CONTAINER] – to list all real-time events in a container
- docker port [CONTAINER] – to show the specific mapping of a container
- docker top [CONTAINER] – to display all running processes
- docker stats [CONTAINER] – to display usage statistics of a container in real-time
- docker diff [CONTAINER] – to display all changed to a file or directory in a container
- docker image ls – to list all locally stored images in the Docker engine
- docker history [IMAGE] – to see the history of an image
Check out : 12 Interesting Docker Project Ideas For Beginners
Loading and Saving Images Commands in Docker
- docker load < my_image.tar.gz – to load an image from a file
- docker save my_image:my_tag | gzip > my_image.tar.gz – to save an existing image
Importing and Exporting Container
- cat my_container.tar.gz | docker import – my_image:my_tag – to import a container as an image
- docker export my_container | gzip > my_container.tar.gz – to export an existing container
Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs or Masters Programs to fast-track your career.
Network Commands in Docker
1. To create a network, use the following command:
docker network create -d overlay MyOverlayNetworkdocker network create -d bridge MyBridgeNetworkdocker network create -d overlay \
–subnet=192.167.0.0/16 \
–subnet=192.172.0.0/16 \
–gateway=192.167.0.100 \
–gateway=192.172.0.100 \
–ip-range=192.167.1.0/24 \
–aux-address=”my-router=192.167.1.5″ –aux-address=”my-switch=192.167.1.6″ \
–aux-address=”my-printer=192.172.1.5″ –aux-address=”my-nas=192.172.1.6″ \
MyOverlayNetwork
2. To remove a network, use:
docker network rm MyOverlayNetwork
3. To list a network,
docker network ls
4. To get information about a network
docker network inspect MyOverlayNetwork
5. To connect a running container to a network
docker network connect MyOverlayNetwork nginx
6. To connect a container to a network when it starts
docker run -it -d –network=MyOverlayNetwork nginx
7. To disconnect a container from a network
docker network disconnect MyOverlayNetwork nginx
Also read: Docker Interview Questions & Answers
Read our Popular Articles related to Software Development
Conclusion
This Docker cheat sheet should save you enough time while you’re working on building a high-quality app. Keep it handy at all times to quicken the process of app development!
If you’re interested to learn more about docker, full stack software development , check out upGrad & IIIT-B’s PG Diploma in Full-stack Software Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.
Is Docker better than Virtual Machines (VM)?
Docker is an open platform container manager that enables developers and system coordinators to oversee the function of all the containers. It is accessible by any language and toolchain. Virtual Machines provide a software platform that developers use to go through an Operating System (OS). It utilizes an entire OS and has a singular non-shareable host kernel. Whereas in Docker, one OS can handle numerous workloads. Docker also allows one host kernel to be shared by multiple containers. In terms of storage space required, containers in Docker take up less space than Virtual Machines. Docker’s ability to operate from a single server gives it an upper hand over Virtual Machines. Hence, Docker is relatively better than Virtual Machines.
How are Docker containers and images different?
Docker images and containers work with each other, which makes the functioning of Docker so optimal. They do vary in their origins and attributes. Docker images are the prototype or template through which Docker containers are reflected. Docker images are unchangeable; they provide a multi-layered structured template with guidelines for the container. This means that Docker containers cannot change by themselves unless their pre-existing Docker image is removed or updated. Since the image acts as a prototype, it’s made only once, while containers are producible for innumerable times. Docker images do not require containers to function, but containers are dependent on Docker images.
Why are Kubernetes used with Docker?
Docker and Kubernetes collaborate to create scalable and functional application platforms. While Docker is excellent at intaking multiple containers and maintaining an Operating System, it does fall behind in running multiple containers independently and managing workloads significantly while scaling the containers across multiple servers. This is where Kubernetes can provide its expertise to ensure the smooth functioning of all the operations of Docker. It can handle vast containers and navigate their usage with specific users. So, while Docker focuses on containerization, Kubernetes provides orchestration of those containers.