Docker Cheat Sheet to Quicken App Development

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.

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.

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.

Learn more: Introduction to Docker Networking: Workflow, Networking

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.

Let’s now move on to container commands.

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

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

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]

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.

Get our free ebook!
Computer Vision with Successful Applications in Healthcare, Retail and More
Download Now
  • 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

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

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.

Prepare for a Career of the Future

INDUSTRY TRUSTED LEARNING - INDUSTRY RECOGNIZED CERTIFICATION.
Apply Now

Leave a comment

Your email address will not be published. Required fields are marked *

×
Become A Full Stack Developer
Download syllabus & learn from a cutting-edge software development course designed by upGrad & IIIT-B.
Download syllabus
By clicking Download syllabus, I authorize upGrad and its representatives to contact me
via SMS / Email / Phone / WhatsApp / any other modes.
I agree to upGrad terms and conditions and our privacy policy.
Download EBook
Download EBook
By clicking Download EBook, you agree to our terms and conditions and our privacy policy.
Get our free ebook!
Computer Vision with Successful Applications in Healthcare, Retail and More
Download Now