DCA – 04 – Running Containers

Jarret B

Well-Known Member
Staff member
Joined
May 22, 2017
Messages
332
Reaction score
356
Credits
11,260
The information in this article is basic knowledge you need to know when dealing with Docker and running containers. I have covered some of this already, but I want to make sure we cover it and it you understand it.

More information will come later, of course, but these are the basics. Anytime you use Docker, these commands are used and you need to know them backwards and forwards.

Running an Image

When you run an image, it starts a container. Easy right? There are multiple ways to start an image.

The first will start a container and run the default command.

docker run <repository>:<tag>

An example would be 'docker run ubuntu:latest', if you have the image downloaded locally. If you run this, then nothing will happen, or appear so. The container will start and then shut down. Why? It started, then ran the default command. For a Linux OS, the default command is 'bash'. When 'bash' has nothing to do, in this case, it closes and the container shuts down. To have the container stay open, we need to be in an interactive mode so you can type in commands for 'bash' to run.

So, for an interactive mode, we need two parameters. The first parameter is '-i' for interactive and the second is '-t' for a terminal. So the command would then be:

docker run -it ubuntu:latest

When using the interactive parameters, you should now be at a command prompt within the container, as shown in Figure 1.

Figure 01.JPG

FIGURE 1

Now, we can be in the container and run it until we type 'exit' to leave the container and shut it down.

If we leave the container running and open a second terminal, we can use the command 'docker ps' to see the list of running containers. We can see an example in Figure 2. Here, you can see the default command is 'bash' and the container name is 'unruffled_banzai'.

Figure 02.JPG

FIGURE 2

Instead of letting Docker give a random name to the container, you can give the container your own name when you start it. To give it a name, you use the parameter '--name' followed by the name you want to use. For example:

docker run -it --name ubuntu ubuntu:latest

Now, if you close the container and start it again with the same name, or another image with the same name, you will get a message about a name conflict. You either need to use a different name, or remove the existing image.

To remove the image, you need to see what has been closed first. To see what containers have been closed, use the command:

docker ps -a

The command will display a list of all the closed containers. If you want to restart a container, then you can use the command:

docker start -ia <container_name>

For example, looking back at Figure 2, once I closed the container, I could restart it with the command 'docker start -ia unruffled_banzai'. The parameters 'ia' allow you to attach to the container interactively. If you do not need interactivity, the drop the parameters.

Now, you may ask, why do I need to restart a container in this manner. Let's say you added something to the container. For example, let's say I started Ubuntu in a container and added a program such as 'htop', or anything. When I update the container, it does not update the original image. So, if I want to keep any updates or changes, then I need to keep the new image in the closed list and start it. We'll update images later.

I'll cover removing closed containers momentarily.

Another option for running an image is to put it in the background in detached mode. In this way, we can access it remotely. For example, we can use the 'httpd' image. When it runs, we do not need to open it interactively to keep it running like we do with Ubuntu. So, to run a command in detached mode, use the following command:

docker run -d httpd

If you run 'docker ps', you'll see that the 'httpd' container is running. In a bit, we'll look at attaching to the container interactively.

Removing Closed Containers

When you run the command 'docker ps -a', you'll see all the containers that have been closed. You may not want to have these around, so you'll want to delete them.

You can delete them one at a time with the command 'docker rm <container_name>'. For example, from the previous example, we would use 'docker rm unruffled_banzai'.

You can remove the containers one at a time, or remove them all with the command 'docker rm `docker ps -a -q`'. The back quote is the one below the Escape key. Just be sure there isn't a container you want to keep before you delete them.

NOTE: This only deletes the created containers and not the original images.

It is possible to have a container automatically removed when it is closed. To do this, add the parameter '--rm' to the 'docker run' command.

Attaching to a Detached Container

Now, we looked at the command 'docker run -d httpd'. So, once the container is running, how can we get into it to do anything with it interactively?

There are two ways to do this. The first is the command:

docker attach <container_name>

Here, the command would be 'docker attach httpd'. The issue here is that when you enter the command 'exit', the container is closed.

The second option is the command:

docker exec -it <container_name> /bin/bash

In this case, it would be something like 'docker exec -it quizzical_jepsen /bin/bash'. It instantly puts you at the command prompt inside the container. Perform whatever tasks you need and then type 'exit'. Running 'docker ps', you can see that the container is still running.

Conclusion

The information covered here is essential to know for using Docker. You may not need it on the DCA Exam, but it is required to know.

Make sure you go over these commands so you understand them completely.
 

Members online


Top