Docker Cheat Sheet: A Beginner’s Guide

Satish Rundla
Cypik
Published in
3 min readApr 15, 2024

Absolutely! Here’s a revised version with the text formatted into a single paragraph:

“Are you just stepping into the world of Docker and feeling a bit overwhelmed by all the commands and options? Don’t worry; you’re not alone! Docker is a powerful tool for containerization, but it can take some time to get comfortable with its commands and functionalities. To help you along your journey, we’ve put together a comprehensive Docker cheat sheet tailored for beginners. Let’s dive in!”

Prerequisites

Before delving into Docker commands, ensure that Docker is installed on your system. You can download and install Docker from the official Docker website.

Getting Started

First things first, let’s familiarize ourselves with some basic Docker commands:

  • docker version: Displays detailed information about your Docker CLI and daemon versions.
  • docker system info: Lists data about your Docker environment, including active plugins and container/image counts.
  • docker help: View the help index, a reference of all the supported commands.
  • docker <command> --help: View detailed information about a particular command.

Building Images

Building images from Dockerfiles is a fundamental aspect of Docker:

  • docker build .: Build the Dockerfile in your working directory into a new image.
  • docker build -t example-image:latest .: Build the Dockerfile and tag the resulting image.
  • docker build -f docker/app-dockerfile: Build a Dockerfile at a specific path.
  • docker build --build-arg foo=bar .: Set build arguments during the build process.
  • docker build --pull .: Pull updated versions of images referenced in the FROM instructions.
  • docker build --quiet .: Build an image without emitting output.

Running Containers

Once you have your images, you’ll want to run containers:

  • docker run example-image:latest: Run a container using a specific image.
  • docker run -d example-image:latest: Detach the terminal from the running container.
  • docker run -it example-image:latest: Attach the terminal's input stream and TTY for interactive commands.
  • docker run --name my-container example-image:latest: Name the new container.
  • docker run -p 8080:80 example-image:latest: Bind ports between host and container.
  • docker run -v /host-directory:/container-directory example-image:latest: Bind mount directories between host and container.
  • docker run --network my-network example-image:latest: Connect the container to a specific network.

Managing Containers

Managing containers is crucial for effective Docker usage:

  • docker ps: List all running containers.
  • docker ps -a: List all containers, including stopped ones.
  • docker attach <container>: Attach the terminal to a running container.
  • docker stop <container>: Stop a running container.
  • docker rm <container>: Delete a container.

Copying to and from Containers

Copying files to and from containers is made easy with docker cp:

  • docker cp example.txt my-container:/data: Copy file from host to container.
  • docker cp my-container:/data/example.txt /demo/example.txt: Copy file from container to host.

Executing Commands in Containers

Run commands inside containers with docker exec:

  • docker exec my-container demo-command: Run command inside the container.
  • docker exec -it my-container demo-command: Run interactive command inside container.

Accessing Container Logs

Access logs from containers using docker logs:

  • docker logs <container>: Stream existing log output.
  • docker logs <container> --follow: Stream new logs continuously.
  • docker logs <container> -n 10: Get the last 10 logs.

Managing Images

Interact with Docker images using these commands:

  • docker images: List all stored images.
  • docker rmi <image>: Delete an image.
  • docker tag <image> example-image:latest: Add a new tag to an existing image.

Pulling and Pushing Images

Push and pull images to/from remote registries:

  • docker push example.com/user/image:latest: Push the image to the remote registry.
  • docker pull example.com/user/image:latest: Pull the image from the remote registry.

Conclusion

Docker is a versatile tool that can revolutionize the way you develop, deploy, and manage your applications. With this cheat sheet, you have a handy reference to help you navigate the world of Docker as a beginner. As you continue to explore and experiment, don’t hesitate to refer back to this guide for assistance. Happy Dockerizing!

Enjoy it! That’s It; we are done…

For hassle-free Cloud Management with DevOps at the center of the process, contact us at info@cypik.com
Cypik

About the author:
I’m Satish Chand Rundla, an experienced DevOps engineer at Cypik. With a strong focus on automating development processes, my expertise lies in the AWS cloud and Terraform platforms. Currently, I’m part of the team at Cypik, I strive to optimize software delivery and infrastructure management, aiming to increase efficiency and scalability in all aspects of our work.

--

--

Satish Rundla
Cypik
Writer for

DevOps Engineer || DevOps || Aws || Jenkins || Networking || Terraform || Github || Mysql || Ansible || Linux || Docker