Categories: DevOps

Docker Cheatsheet: Essential Commands and Explanations

Introduction

By enabling containerization, Docker has transformed the way software is built, deployed, and managed. Containers provide a uniform and portable environment in which an application and its dependencies are encapsulated. To fully utilise Docker’s power, it is critical to learn and use its fundamental commands. This Docker cheatsheet includes crucial commands and explanations to help you confidently navigate the containerization world.

Images and Containers

Pulling Images

To get started, you often need a base image from which to build your containers. The docker pull command fetches images from a Docker registry, such as Docker Hub:

docker pull <image_name>:<tag>

Building Images

Docker images are created using Dockerfiles, which define the instructions to build an image. The docker build command, paired with the -t flag, constructs an image from a Dockerfile:

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

Listing Images

To see the images available on your system, employ the docker image ls command:

docker image ls

Managing Containers

Creating containers from images is a core aspect of Docker usage. The docker run command is pivotal here. It starts a container, allowing you to map host and container ports, name the container, and more:

docker run -d -p <host_port>:<container_port> --name <container_name> <image_name>:<tag>

Use docker ps to list running containers and docker ps -a to list all containers, including stopped ones.

Starting and Stopping Containers

To control containers, you can start, stop, and restart them using commands like docker start, docker stop, and docker restart.

docker start <container_id/name>
docker stop <container_id/name>
docker restart <container_id/name>

Removing Containers and Images

When you’re done with a container, remove it using docker rm. Similarly, to clear out unneeded images, docker rmi can be used:

docker rm <container_id/name>
docker rmi <image_name>:<tag>

Executing Commands in Containers

For debugging or maintenance, the docker exec command lets you execute a command within a running container interactively:

docker exec -it <container_id/name> <command>

Volumes and Networking

Data Volumes

Docker volumes ensure that data is retained across container runs. Docker volume is used to create volumes. With docker volume ls, you may build new volumes and list existing ones.

docker volume create <volume_name>
docker volume ls

Mounting Host Directories

It is usual to mount host directories as volumes in containers. To accomplish this, use the -v flag with docker run:

docker run -v <host_path>:<container_path> <image_name>

Networking

Containers frequently require communication. Docker network can be used to create bespoke networks. With docker network ls, you may create and list accessible networks.

docker network create <network_name>
docker network ls

Docker Compose

Docker Compose makes multi-container applications easier to manage. Services are defined in the docker-compose.yml file, and they are managed using commands like docker-compose up and docker-compose down.

docker-compose up
docker-compose down

Miscellaneous

Inspecting Containers

To gain detailed insights into a container, use docker inspect:

docker inspect <container_id/name>

Managing Disk Usage

Check Docker’s disk usage with docker system df, and clean up unused resources with docker system prune.

docker system df
docker system prune

Version and System Information

To ascertain Docker’s version and system details, utilize docker version and docker info:

docker version
docker info

Remember that Docker commands may differ somewhat depending on your operating system and Docker version. This cheatsheet covers the essentials, although Docker has a wide range of features. Always consult Docker’s official documentation for the most up-to-date information and updates.

Related Articles

Difference Between Git Stash Pop And Git Stash Apply

What Is Git Stash And Why Do You Need It?

Git Delete Remote Branch – How To Remove A Remote Branch In Git

MongoDB Interview Questions And Anwers

Understand The Background Of Free AI Tool Now

The Requested Url /Phpmyadmin/ Was Not Found On This Server

Conclusion

Mastering these essential Docker commands empowers you to harness the full potential of containerization for streamlined application development and deployment. With these tools at your disposal, you’re poised to navigate the dynamic landscape of Docker confidently and efficiently.

Developer Diary

Share
Published by
Developer Diary

Recent Posts

Git Tag Cheat Sheet

Introduction Git tags are an essential feature of version control systems, offering a simple way…

3 months ago

Understanding Web Storage: Cookies, Local Storage

Introduction The methods that browsers employ to store data on a user's device are referred…

4 months ago

Setting up OpenVPN Access Server in Amazon VPC – AWS

Introduction A well-known open-source VPN technology, OpenVPN provides strong protection for both people and businesses.…

4 months ago

Enhance Error Tracking & Monitoring: Integrate Sentry with Node.js & Express.js

Introduction Integrating Sentry into a Node.js, Express.js, and MongoDB backend project significantly enhances error tracking…

4 months ago

Comparing Callbacks, Promises, and Async/Await in JavaScript

Introduction In the world of JavaScript development, efficiently managing asynchronous operations is essential. Asynchronous programming…

6 months ago

How To Secure Nginx with Let’s Encrypt on Ubuntu EC2 Instance

Introduction Let's Encrypt is a Certificate Authority (CA) that makes it simple to obtain and…

8 months ago