In this post, we will look at Docker basic commands. We will explore these commands and also see their usage.

In case you are a beginner to Docker, I would strongly suggest you to first understand the basics of Docker.

Otherwise, you can simply continue reading this post.

Docker Basic Command # 1 – Run a Container

One of the first basic Docker command you will use is the docker run command. This command takes the image name as input. Below is how we execute this command.

docker run hello-world

Here, hello-world is the Docker image name. We get the below response on executing this command.

docker basic commands run

Basically, the hello-world Docker image is pulled from the Docker Hub and then the Hello message is printed in the console.

Docker Basic Command # 2 – Override the Default Command

A Docker image is usually built with a default command. When a container is created using the Docker image, the default command is executed. However, it is possible for us to override the default command.

Below is how we can do it using the docker run command.

docker run <image_name> <command>

As an example, we override the default command of the busybox image as below. Here, the command is echo hi there.

docker run busybox echo hi there

Basically, here the default command is overridden. Instead, the command echo hi there is executed when the container starts up.

Docker Basic Command # 3 – Listing Running Containers

Another basic Docker command is listing all running containers. Basically, at a given point of time there might be multiple running containers in our Docker host. And using this command, we can list all of them.

To see it in action, we can first run a container using the busybox image as below.

docker run busybox ping google.com

Basically, this command will start a new container running busybox image. Also, we have overridden the default command. We run ping google.com that will enable the container to continue running for some time.

Now, to list running Docker containers, we can simply execute the below command.

docker ps
docker basic command list containers

Many times, we also want to see list of all the containers we have run. Basically, even containers have are no longer running. To do so, we can execute the below command.

docker ps -all

Docker Basic Commands # 4 – Creating and starting a container

The Docker run command we saw earlier is actually a combination of two commands. The Docker create and the Docker start command.

However, we can also execute these commands separately as below.

docker create hello-world

This command will output an image id.

9ace76c60c993fe4ea178895559c5e5a6bec762f0432aa1155ec2bd02ccb3a88

Using this id, we can execute the Docker start command as below.

docker start -a 9ace76c60c993fe4ea178895559c5e5a6bec762f0432aa1155ec2bd02ccb3a88

This will basically print the Hello message in console.

Docker Basic Commands # 5 – Restarting stopped containers

We can also restart stopped Docker containers. Let’s see the process to do the same.

We first create a new container using busybox image as below. Basically, we are overriding the default command

docker run busybox echo hi there

This will simply print the message ‘hi there’ in the console and exit. However, to restart this container, we need the id of the container. And we can get the id using the below command.

docker ps -all

This will list all the stopped containers from which we can get the id of the busybox container.

Now, we can simply start that container using the below command.

docker start -a c9216492cc3a

Here, c9216492cc3a is the container id that we got the from the docker ps command. On executing the above command, the container will start and execute the echo hi there command. The output will be shown in the console.

Docker Basic Commands # 6 – Retrieving Container Logs

Logs are an important aspect for developers to find out what’s happening in an application. It is the same with Docker. Basically, we can execute a command to simply check the logs for a Docker container.

To check the logs, below is the command.

docker logs <container_id>

If the container has generated any logs during its run time, those will be displayed in the console.

Docker Basic Commands # 7 – Stop a Container

An important and basic Docker command is to stop a running container. With Docker, we get two options for the same.

The first option is the docker stop command. Below is an example of the command.

docker stop <container_id>

Basically, this command issues a SIGTERM to the process running within a container asking it to shutdown. Typically, a grace period is provided in this case for the container to close down its activities and then shut down.

However, there is also the docker kill command.

docker kill <container_id>

This command simply terminates the running container without any kind of warning or grace period. In other words, if any process is running within the container, it will also terminate.

Basically, the kill command can be used in case a container is stuck and the docker stop command is not able to terminate it gracefully. Usually, it is better to go with docker stop command.

Docker Basic Commands # 8 – Removing Stopped Containers

Another one of Docker basic commands is required to remove stopped containers from the Docker host. The stopped containers consume storage and also clutter the list of containers. If there is no need to use old container, we can remove those containers from the Docker host.

To do so, we can execute the below command.

docker system prune

Note that this command deletes all dangling images as well as build cache. This means that if you run some of the old images again, they may have to be re-downloaded from Docker hub.

Conclusion

In this post, we looked at some Docker basic commands. We also saw their usage and typical use-cases.

There are many other commands available in the Docker world. However, we will discuss those in some other posts.

Categories: BlogDocker

Saurabh Dashora

Saurabh is a Software Architect with over 12 years of experience. He has worked on large-scale distributed systems across various domains and organizations. He is also a passionate Technical Writer and loves sharing knowledge in the community.

0 Comments

Leave a Reply

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