Essential Docker Commands You Should Kno ...

Essential Docker Commands You Should Know

Mar 09, 2023

Originally Published: https://medium.com/@rajvivan11/essential-docker-commands-you-should-know-263781e2616e
Docker is a platform that allows developers to create, deploy, and run applications in containers. Containers are isolated environments that package the application along with all its dependencies, libraries, and configuration files. This approach eliminates the need for developers to worry about system compatibility issues, as containers can be run on any machine with Docker installed.

As the world of software development continues to evolve, Docker has become an essential tool for building modern applications. With Docker, you can easily create, manage, and deploy applications in a consistent, repeatable way. Whether you’re a seasoned developer or just starting out, learning Docker is an investment that will pay off in the long run.

As Steve Jobs once said, “Innovation distinguishes between a leader and a follower.” By learning Docker, you can set yourself apart from the pack and become a leader in your field.

So, why should you learn Docker? For starters, Docker offers a range of benefits that make it a valuable tool for developers, including:

  • Consistent development environments

  • Faster deployment times

  • Improved scalability and reliability

  • Simplified DevOps workflows

When it comes to Docker, two key concepts to understand are images and containersAn image is a lightweight, standalone, executable package that includes everything needed to run an application, including the code, libraries, dependencies, and configuration files. A container, on the other hand, is a runtime instance of an image that is isolated from the host system and other containers.

To put it simply, an image is like a blueprint, while a container is like a building that is created from that blueprint.

Learning the Docker commands is crucial for anyone looking to master the platform. These commands enable you to create, manage, and manipulate Docker images and containers, giving you complete control over your development workflow. By familiarizing yourself with the Docker commands, you can streamline your development process, increase productivity, and ultimately build better applications.

If you’re looking to take your development skills to the next level, learning Docker is a must. With its ability to simplify development environments, improve deployment times, and streamline workflows, Docker is quickly becoming the go-to platform for modern application development. So, what are you waiting for? Dive in and start containerizing your applications today!

Before you dive into the commands, let me tell you about an article that you might find helpful. If you haven’t read our previous piece on Docker, you might want to give it a quick scan first.

Why, you ask? Well, understanding the basics of Docker can really help you grasp the commands and their importance. Think of it like building a house — you need a strong foundation before you start building the walls.

Data Engineering — Basic Introduction
Demystifying virtualization, containerization, and Docker architecture

So take a few minutes to check out our previous article on Docker, and then come back here to start using those commands like a pro!

My Ipad Notes: -i vs -it

Docker Installation

Setting up Docker on a Mac is a simple process. Follow these steps to get started:

Docker Engine Platform

  1. Once installed open Docker Desktop and ensure that the Docker icon is visible in your system tray. If it’s not visible, open the application from your Applications folder.

  2. Open Terminal and enter the command “docker version” to verify that Docker is installed and running properly. If you see output similar to “Client: Docker Engine — Community” and “Server: Docker Engine — Community”, then Docker is up and running.

After setting up Docker Desktop, the next step would be to start exploring Docker’s capabilities. This could involve creating your first Docker image, pulling an image from the Docker Hub, or running a container. The possibilities are endless!

To start the Docker daemon and see what version of Docker is installed, you can use the following command:

sudo systemctl start docker
docker version

This command starts the Docker daemon and then displays the version information for both the Docker client and server.

Docker is built on a client-server architecture because it enables developers to build, ship, and run distributed applications more efficiently. The client component of Docker is responsible for sending requests to the Docker daemon, while the server component is responsible for managing the Docker objects such as images, containers, networks, and volumes. This architecture allows developers to work with Docker on their local machine while leveraging the power of remote servers to build and deploy their applications. Additionally, the client-server architecture enables Docker to scale horizontally, making it an ideal choice for large, complex applications.

Here are 15 essential Docker commands that every Docker user should know:

docker pull: This command is used to download a Docker image from a Docker registry. For example, the following command will download the nginx image:

docker pull nginx

docker run: This command is used to run a Docker container based on a Docker image. For example, the following command will run a container based on the nginx image:

docker run nginx

docker ps: This command is used to list all the currently running Docker containers. For example, the following command will list all the running containers:

docker ps

docker images: This command is used to list all the Docker images that are currently available on your system. For example, the following command will list all the images:

docker images

How to resolve an error when an image is not available?

docker stop: This command is used to stop a running Docker container. For example, the following command will stop a container with ID 123abc:

e52 Container is not available under Blue Rectangle

docker stop 123abc

docker rm: This command is used to remove a Docker container. For example, the following command will remove a container with ID 123abc:

If you attempt to remove a running container without stopping it first, Docker will return an error message like “You cannot remove a running container a19af024c93db1c67c4ffb82576f3d72001630ca9b19d7db404ea11cae6bee29. Stop the container before attempting removal or force remove”, indicating that you need to either stop the container before removing it or force remove it using the -f option with the docker rm command. However, it’s generally not recommended to force remove a container, as it can result in data loss or other issues. It’s always better to stop a container gracefully before removing it.

When you launch a Docker container, a new writable layer is created on top of the read-only image layer. Any changes made to the container, such as installing software, writing files, or modifying configurations, are saved in this writable layer. When you remove a container, Docker deletes the writable layer and any associated resources, such as network interfaces and volumes, but leaves the image layer intact.

You can remove a running container in Docker by first stopping it and then removing it. To stop a running container, you can use the docker stop command followed by the container ID or name:

docker rm 123abc

docker rmi: This command is used to remove a Docker image. For example, the following command will remove an image with the name myimage:

docker rmi myimage

rm vs rmi command in Docker

docker exec: This command is used to execute a command inside a running Docker container. For example, the following command will execute the ls command inside a container with ID 123abc:

docker exec 123abc ls

docker push: This command is used to upload a Docker image to a Docker registry. For example, the following command will upload an image with the name myimage to the Docker registry:

docker push myimage

docker build: This command is used to build a Docker image from a Dockerfile. For example, the following command will build an image from the Dockerfile in the current directory:

docker build -t myimage

docker inspect: This command is used to display detailed information about a Docker container or image. For example, the following command will display information about a container with ID 123abc:

Docker inspect

docker inspect 123abc

By default, the output of docker inspect command is in a JSON format. However, there are various tools and techniques available to display the output of docker inspect in a more readable or tabular format.

Using the — format option: The — format option of docker inspect allows you to specify a Go template to format the output. You can use this option to display the output in a tabular format. For example, the following command displays the container’s ID, name, and status in a table format:

docker inspect --format='{{.Id}}\t{{.Name}}\t{{.State.Status}}' <container-id>

Note: The “\t” character is used to represent a tab space in the output but somehow my terminal is not showing the correct output

docker network: This command is used to manage Docker networks. For example, the following command will create a new network called mynetwork:

docker network create mynetwork

docker port: This command is used to display the public-facing port of a Docker container. For example, the following command will display the public-facing port of a container with ID 123abc:

docker port 123abc

docker login: This command is used to log in to a Docker registry. For example, the following command will log in to the Docker Hub registry:

docker login

docker logout: This command is used to log out of a Docker registry. For example, the following command will log out of the Docker Hub registry:

docker logout

These are just a few of the many Docker commands that are available. By learning and mastering these commands, you’ll be able to manage Docker containers and images with ease.

Detached mode and Attached mode in Docker

Docker containers can run in two modes: detached mode and attached mode

Detached mode: Detached mode (also known as background mode) is the default mode for running Docker containers. In detached mode, a container runs in the background and does not attach to the terminal. This means that you can continue to use your terminal for other tasks while the container is running. You can use the docker run command with the -d or — detach flag to run a container in detached mode.

Example: The following command runs an nginx container in detached mode:

docker run -d nginx

Attached mode: In attached mode (also known as foreground mode), a container attaches to the terminal and streams its output to the terminal. This means that you can see the output of the container in real-time and interact with it using the terminal. You can use the docker run command without the -d or — detach flag to run a container in attached mode.

When you run a Docker container in detached mode with the -d flag, it creates a container in the background and returns control of the terminal to you. However, the container is still running and you can interact with it using various Docker commands.

Example: The following command attaches to a running container with ID “abc123”:

docker attach abc123

Few docker scenarios that will aid the developer in his command during the production deployment

  1. Find the total number of running containers in Docker:

As a developer, you may want to know how many containers are currently running in your Docker environment. You can use the docker ps command with the — format option to display only the container IDs, and then use the wc -l command to count the number of lines in the output, which corresponds to the number of running containers.

docker ps --format '{{.ID}}' | wc -l

This command will output the total number of running containers in your Docker environment.

2. Check the logs of a specific container:
During production deployment, you may need to check the logs of a specific container to troubleshoot any issues. You can use the docker logs command followed by the container ID or name to display the logs of a specific container.

docker logs <container-id>

This command will display the logs of the specified container.

3. Stop and remove all containers:
Before deploying a new version of your application, you may want to stop and remove all existing containers to ensure a clean deployment. You can use the following command to stop and remove all running and stopped containers:

docker rm $(docker ps -aq) -f

This command will stop and remove all running and stopped containers.

4. Check the resource usage of a specific container: During production deployment, you may want to check the resource usage of a specific container to ensure that it’s not consuming too many resources. You can use the docker stats command followed by the container ID or name to display the resource usage of a specific container.

docker stats <container-id>

This command will display the CPU, memory, and network usage of the specified container.

How to rename a container in a docker

Run a command to start a container in detached mode:

docker run --name webapp -d nginx:1.14-alpine

This command creates a container with the name webapp and the nginx:1.14-alpine image, and starts it in detached mode. However, even though the container is running in the background, you can still view information about it using the docker ps command.

When you run the docker ps command, it shows all the containers that are currently running on your system. This includes containers that were started in detached mode, as well as containers that were started in attached mode (i.e., containers that are currently attached to the terminal).

The docker ps -q command is used to display only the container IDs of the running containers, without any other information. This command will show you the same output as docker ps, but without the headers and other details.

The docker ps -aq command is used to display the IDs of all the containers on your system, whether they are running or not. This includes containers that were started in detached mode, as well as containers that were started in attached mode.

In summary, even when you run a container in detached mode, you can still view information about it using the docker ps command. This is because the container is still running in the background, even though it’s not attached to your terminal. The docker ps -q command will show you the same output as docker ps, but without the headers and other details, while the docker ps -aq command will show you the IDs of all the containers on your system.

BONUS TIP:

Write a command to display information about all containers, both running and stopped.

docker container ls -a --format "container {{.Names}} is in state {{.State}} and has ID {{.ID}}"

I hope you’ve enjoyed reading this blog as much as I’ve enjoyed writing it Now, if you’re feeling inspired to practice and learn, don’t just sit there like a bump on a log! Get out there and start flexing those brain muscles! Learn a new language, try a new skill, or just learn a few new jokes to impress your friends.

And remember, sharing is caring! Don’t keep all that newfound knowledge to yourself, share it with your friends, family, and coworkers. Who knows, you might even inspire them to start learning too! And if not, at least you’ll have someone to tell your new jokes to.

So go forth and learn, my friends! And always remember, knowledge is power, but a good sense of humor is even more powerful.

Conclusion :
Hey there, fellow data enthusiasts! If you’re hooked on my articles and can’t get enough of my witty data humor, then you’re in luck! Here are three ways you can stay connected with me:

A. Follow me on LinkedIn and join my network of awesome data professionals. You’ll never miss a beat when it comes to my latest stories, tips, and tricks.

B. Subscribe to my newsletter, the ultimate insider’s guide to all things data engineering and data visualization. You’ll get exclusive access to new stories, and you can even text me to ask all the burning questions you’ve been dying to know.

C. Become a referred member, and get ready to indulge in an endless buffet of data knowledge. You’ll never have to worry about hitting your “maximum number of stories for the month” limit again, and you’ll get to read everything that I (and thousands of other top data writers) have to say about the newest technology available.

So what are you waiting for? Let’s get connected and start exploring the exciting world of data together! Oh, and don’t forget to bring the coffee — it’s the secret ingredient to unlocking the full potential of your data brainpower. Cheers!

So come on, let’s dive deep into the wonderful world of data together! Check out my website at vizartpandey.com, connect with me on LinkedIn at linkedin.com/in/rajvivan, or shoot me an email at [email protected]. Can’t wait to hear from you!

Gefällt dir dieser Beitrag?

Kaufe Rajeev Pandey einen Kaffee

Mehr von Rajeev Pandey