Computer scienceSystem administration and DevOpsDockerBasic commands

Basic operations with a container

7 minutes read

In this topic, we will consider commands to manage Docker containers. We will learn the most basic options: how to create, start, stop, list, remove Docker containers and how to execute commands in them.

docker container create

When you enter the docker create command, Docker creates a container, but the container isn't running yet. At this stage, the container will receive a unique ID and a name.

Let's look at this command in action. The syntax for it is as follows: docker container create <image_name>. The output of this command is a hash code (ID) of the container:

$ docker container create hello-world

5a30b370e9dfc147f5438380d60ff4b1c43869a752f2ef481b6cf0adb33dae83

Now, to check if our container is successfully created, we can use the ls subcommand.

docker container ls

To list all docker containers, use the docker container ls -a command. The result will look like this:

$ docker container ls -a

CONTAINER ID   IMAGE         COMMAND    CREATED         STATUS    PORTS     NAMES
c6c7b4c22dfa   hello-world   "/hello"   3 seconds ago   Created             quizzical_murdock

Do not forget to add -a option, otherwise, you will see only running containers, but your currently created one is not running yet.

We have successfully created our container. Now it's time to start it.

docker container start

The start subcommand should not be confused with the run subcommand. The start subcommand can be applied only for already created containers. After executing this command, the ID of the container will be printed again:

$ docker container start c6c7b4c22dfa

c6c7b4c22dfa

Now our container is running. But if we list all the containers again, we will see a result like this.

$ sudo docker container ls -a

CONTAINER ID   IMAGE         COMMAND    CREATED         STATUS                     PORTS     NAMES
c6c7b4c22dfa   hello-world   "/hello"   8 minutes ago   Exited (0) 5 minutes ago             quizzical_murdock

As you can see, our container status is "Exited (0) 5 minutes ago". This is because the process in the container is already finished. In short, the container of the hello-world image runs only one command, so a container exits when the command is done.

In the next section, we will learn how to run a container with a foreground process and execute commands in it.

docker container exec

Simply put, we use the exec parameter to execute commands in a running container.

To launch a program in a running container, you should enter the container ID (you may also use the container name) next to exec, and then specify the program's name.

Let's use the Ubuntu image (a Linux operating system) for our docker container and start it. Here we added the -t and -d parameters. Thanks to these parameters, we can run this container as a background process and interact with it later still.

$ docker container run -t -d ubuntu
9faa5154097e1d2ecc77ffd3e70752cdd984467623bab83532db7d316997bacb

Let's take a look at the STATUS column of the container we run using the ls subcommand, it's up:

$ docker container ls -a
CONTAINER ID   IMAGE          COMMAND    CREATED          STATUS                      PORTS     NAMES
9faa5154097e   ubuntu         "bash"     7 seconds ago    Up 7 seconds                          jovial_shannon

It's time to use the exec subcommand. Next to this command, we paste the ID of the container and then provide the ls subcommand next to it. You can see the folders in the directory of the Ubuntu operating system:

$ docker container exec 9faa5154097e ls
bin
boot
dev
...

docker container stop

We use the stop subcommand to stop one or more currently running containers. After entering this command, the process will receive the SIGTERM signal, but it may take some time for the main process to stop.

When using this command, we enter the ID of the container next to the stop subcommand to stop a container.

$ docker container stop 9faa5154097e

9faa5154097e

docker container rm

You can use the rm subcommand to remove one or more Docker containers. To remove containers we need to provide their IDs after the rm subcommand. We can use the ls subcommand to find needed IDs.

Knowing the container's ID, what we need to do is quite simple:

$ docker container rm 9faa5154097e

9faa5154097e

This will output the ID of the container we deleted. Finally, if we list the docker containers, the output will look like this.

$ docker container ls -a

CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

As you can see, our container no longer exists.

Conclusion

In this topic, we created a container using the hello-world image. We have examined in detail the commands to create, list, and delete containers. We've also explored how to delete the container and run other commands right in a running container.

To wrap up:

  1. You should use docker container create <image_name> to create a new Docker container.

  2. You may use docker container ls to list docker containers that are running. You may also add the -a flag to list all Docker containers.

  3. To start a Docker container you should enter docker container start <container_id>.

  4. The exec subcommand is used to execute commands in a running container.

  5. The stop and rm subcommands are used to stop and remove one or more containers. You should use docker container stop <container_id> and docker container rm <container_id> respectively.

56 learners liked this piece of theory. 0 didn't like it. What about you?
Report a typo