Docker Commands Cheatsheet: Streamlining Container Management Tasks

Mohammed Affan
AWS in Plain English
5 min readAug 21, 2023

--

In the ever-evolving landscape of software development and deployment, containerization has emerged as a game-changing technology. At the forefront of this revolution is Docker, a platform that simplifies the process of developing, packaging, and deploying applications using containers. Containers encapsulate an application along with its dependencies, making it portable and consistent across different environments.

As Docker continues to gain popularity, mastering its commands becomes essential for efficient container management. This blog presents a comprehensive Docker command cheatsheet to streamline your container management tasks.

The power of docker commands:

Docker commands are the building blocks of managing containers and their associated components. They empower developers and operations teams to interact with the Docker engine, creating, configuring, and managing containers effortlessly. Docker’s command-line interface (CLI) offers a wide range of commands to perform tasks ranging from building images to orchestrating multi-container applications.

Docker command cheatsheet:

Let’s dive into the Docker command cheatsheet categorized by their functionality:

Managing containers:

1. Running a container:

docker run <image_name>

This command starts a new container instance from the specified image.

For example, the docker run postgres command will start a PostgreSQL container.

2. Running a container with port forwarding:

docker run -p <host_port>:<container_port> <container_name>

This command runs a container and also exposes the specified port of the container to the local host. For example, thedocker run -p 80:80 nginx command runs the nginx container by exposing the container's port 80 to the localhost:80.

3. Running a container in detached mode:

docker run -d <container_name>

This command runs a container in the background without attaching it to your terminal session.

4. List running containers:

docker ps

This command lists all running containers.

5. List both running/stopped containers:

docker ps -a

Lists all of your containers including both running/stopped.

6. Stop a container:

docker stop <container_id/container_name>

Gracefully stop a running container. Use docker ps to get the container id. Use the docker kill command for immediate termination.

7. Restart a container:

docker restart <container_id/container_name>

Restarts a docker container.

8. Remove a container:

docker rm <container_id/container_name>

Deletes a stopped container. Use docker ps -a to get the container id.

9. Forcefully remove a container:

docker rm -f <container_id/container_name>

Adding the -f flag forcefully removes a running container.

10. Run shell commands:

docker exec <container_id/container_name> <command>

Executes a shell command on a docker container.

For example, docker exec nginx ls.

11. Get the shell of a docker container:

docker exec -it <container_id/container_name> <shell_executable>

This command lets you access the shell of the container. The -it flag stands for interactive tty session. You also need to specify a shell executable that is installed inside your container.

Here are some examples:

docker exec -it nginx sh

docker exec -it nginx bash

12. Get logs of a docker container:

docker logs <container_id/container_name>

Lists logs of a container.

13. Inspect container:

docker inspect <container_id>

Retrieve detailed information about a container or image in JSON format.

14. Remove all stopped containers:

docker system prune

This will remove all stopped containers, unused networks, and dangling images. Use with caution!!

15. View basic stats for all containers:

docker stats

This command will display real-time resource usage statistics for all running containers. You can specify the container names or IDs to view stats for specific containers. For example docker stats nginx.

Managing images:

  1. List images:
docker images

Or

docker image ls

Display a list of available images on the host machine.

2. Build an image:

docker build -t <image_name:tag> <path_to_Dockerfile>

Create a new Docker image using a Dockerfile located at the specified path.

3. Pull an image:

docker pull <image_name:tag>

Download an image from a Docker registry.

4. Push an image:

docker push <image_name:tag>

Upload an image to a Docker registry.

5. Remove an image:

docker rmi <image_name:tag>

Delete an image from the host machine. Use the -f flag to force removal.

6. Save an image locally:

docker save -o <output_file.tar> <image_name:tag>

Save an image to a tar archive.

7. Load an image from the local filesystem:

docker load -i <input_file.tar>

Load an image from a tar archive.

Working with networks:

  1. List networks:
docker network ls

View a list of Docker networks.

2. Create a network:

docker network create <network_name>

Establish a new Docker network for container communication.

3. Connect container to network:

docker network connect <network_name> <container_name>

Attach a container to a specified network.

Managing volumes:

  1. List volumes:
docker volume ls

List all Docker volumes on the host.

2. Create a volume:

docker volume create <volume_name>

Generate a new Docker volume.

3. Attach volume to container:

docker run -v <volume_name:mount_path> <image_name>

Mount a volume to a container at the specified mount path.

Managing docker compose:

  1. Run services:
docker-compose up

Deploy multi-container applications defined in a docker-compose.yml file. Use -d flag for running containers in the background.

2. Scale services:

docker-compose up -d --scale <service_name>=<num_instances>

Scale a specific service to a specified number of instances.

3. Stop services:

docker-compose down

Stop and remove containers, networks, and volumes defined in the docker-compose.yml file.

Streamlining container management:

Mastering Docker commands is pivotal for efficient container management. By incorporating these commands into your workflow, you can streamline various aspects of your containerized applications:

  • Development: Quickly spin up containers for local development environments, making it easy to test code changes without affecting the host system.
  • Deployment: Automate the deployment process using scripts or CI/CD pipelines, ensuring consistent and reliable deployments across different environments.
  • Scaling: Utilize Docker’s command-line interface to scale services up or down as needed, adapting to varying levels of user demand.
  • Debugging: Investigate container behaviour and troubleshoot issues by inspecting container logs, metadata, and configurations.
  • Versioning: Efficiently build, tag, and push images to version-controlled repositories, ensuring a consistent history of your application’s evolution.

In conclusion, Docker commands are the cornerstone of containerization, offering a versatile toolkit to manage every facet of containers, images, networks, and volumes. By mastering these commands, you can navigate the container landscape with confidence, efficiency, and precision. Whether you’re a developer, DevOps engineer, or system administrator, this Docker command cheatsheet equips you with the knowledge to streamline your container management tasks and revolutionize your software deployment strategies. Happy containerizing! 🐳

Last words:

Thank you for reaching the end of this article. If you found it helpful, consider showing your appreciation with a few claps!

For more insights into Docker 🐳, explore my other articles. If cloud technologies or DevOps fascinate you, feel free to follow my profile. If you have any questions or queries, drop them in the comments — I typically respond within a day.

I look forward to connecting with you on LinkedIn: https://www.linkedin.com/in/mohammedaffan7/

More content at PlainEnglish.io. Sign up for our free weekly newsletter. Follow us on Twitter, LinkedIn, YouTube, and Discord. Interested in Growth Hacking? Check out Circuit.

--

--

DevOps Engineer♾️| Cloud Computing☁️| Linux🐧| AWS🖥 | Python🐍| Docker⚓| Kubernets🛞| Terraform⚙️| Connect with me👇 www.linkedin.com/in/mohammedaffan7