Understand the Docker Container Commands

Introduction

In this article, you’ll understand the Docker Container Commands and you’ll learn how to run containers in detached mode, how to specify Docker container name, and how to use the docker ps and docker inspect commands.

In the previous article, we have learned how Run Our First Hello World Docker Container in the foreground. But in most cases, containers are actually running in the background.

We can start a Docker container in detached mode with the -d option. So the container starts up and runs in the background. That means, we can start up the container and could use the console after startup for other commands. Let’s see this in action.

We will keep using our BusyBox image. In order to keep the container running in the background, we can run the Linux sleep command to suspend execution for a while.

container detached mode

After the execution, Docker returns us with the long container ID.

container ID in docker container

Now, the Docker container should be running in the background. So, Let’s verify that using the docker ps command.

We can find out all the running docker containers in our local box by using the Docker ps command.

Docker ps command

As you see the above example, The container we started up is currently running. the Docker ps command displays some container information such as container ID, image name, and the command we run.

The container ID displayed here is the short container ID which is the prefix of the long container ID.

docker run

What if we want to display all the containers in the local box including ones that have stopped. So, We can add the -a option at the end of the docker ps command.

stop container

Docker also shows all the containers that we have previously run.

Remove the Containers

If we do not intend to keep the container, we can add -rm option in the end so that Docker will automatically remove the container when the container exits.

Here, below example

docker remove

Let’s sleep for 1 second. Then container runs for 1 second, then it exits. here we run docker ps -a to list all the containers.

docker remove command

We can also specify the name of the Docker container we want to run. If we don’t specify the container name, Docker will automatically generate a container name when we run the docker run command.

Docker Inspect Command

Let’s see another simple Docker command, docker inspect. So, the docker inspect will display low-level information on a container or image Let’s see this below example.

Let’s start up a new container in the detached mode and Docker returns us the container ID.

Then let’s run docker inspect, copy and paste the container ID and execute the command.

docker inspect

As you see, docker inspect renders the result in a JSON array. It displays the IP address and Mac address of this container. docker inspect also outputs some useful low-level information such as the image ID and log path.

docker inspect data

Docker Port Mapping and Docker Log Command

here, we will understand the Docker port mapping and docker log command. So We’ll be using a new Docker image: the Tomcat image.

Tomcat is an open-source web server that executes Java servlets. Let’s check the Docker hub page.

tomcat docker image

As you see, the tomcat page by default runs on port 8080.

tomcat image

We can expose a port inside the container and map the port to another port on the host by -p option.

In this way, the Tomcat web server can be accessed by using the host URL and the mapped port. If you are running Docker on Linux, the host here refers to the localhost.

If you are running Docker on Windows or Mac with Docker machine, the host here refers to the Linux virtual machine running Docker.

The format for -p option is host port and another is container port. Let’s see the below example.

Here, we open up the Docker quick start terminal, create the new container with tomcat image.

tomcat docker image

Now, the image is downloaded and the container is up running. We can access the Tomcat server through a web browser.

we would run containers in the background. In the previous article, we have learned -d will allow us to run containers in detached mode.

Let’s try it out here. Let’s go back to our previous container run command and add -d option.

docker tomcat image

Docker returns us the long container ID. And the container should be running in the background. We can check out the logs of the running containers by docker logs command. then, type the docker log and the container ID.

It shows the container logs.

I hope you have been enjoyed this article. We’ll get into more details about this when we’ll execute a command inside the container in the next article.

Leave a Reply

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