1.3. Container Lifecycle

Detached containers

So far, we’ve been running containers with our terminal attached to their standard input (stdin). In practice, however, most containers run in the background as detached processes.

Running a Docker container in “detached” mode (background) can be done with -d:

docker run -e MARIADB_ROOT_PASSWORD=my-secret-pw -d mariadb:12.0

Now, instead of seeing container logs, only the container ID is displayed. Verify by listing running containers:

docker ps

Now that we have (too) many database containers running, we need to know how to delete them:

Deleting a container

There are two ways to stop a container, we start with the recommended way. You first have to stop the container using its name or ID. You see both in your terminal when you executed docker ps

To stop a container use the command:

docker stop <container>

After that, check the new state with

docker ps

This should show only one container running:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
699e82ed8f1f        mariadb:12.0        "docker-entrypoint..."   9 minutes ago       Up 9 minutes        3306/tcp            jolly_bardeen

We just exited the container “gracefully”, but as an alternative you can also kill a container with the docker kill <container> command. This stops the container immediately by using the KILL signal.

You may recognize that the container upbeat_blackwell is not present in the container list anymore. That is because docker ps only shows running containers, but as always you have a parameter that helps:

docker ps --all
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                         PORTS               NAMES
CONTAINER ID   IMAGE               COMMAND                  CREATED          STATUS                      PORTS      NAMES
699e82ed8f1f   mariadb:12.0        "docker-entrypoint.s…"   4 minutes ago    Up 4 minutes                3306/tcp   jolly_bardeen
7cb31f821233   mariadb:12.0        "docker-entrypoint.s…"   10 minutes ago   Exited (0) 32 seconds ago              upbeat_blackwell
8f500e827376   mariadb:12.0        "docker-entrypoint.s…"   12 minutes ago   Exited (1) 12 minutes ago              confident_wright
01417368d5b6   hello-world:linux   "/hello"                 13 minutes ago   Exited (0) 13 minutes ago              vigorous_dijkstra
321f2265b76e   hello-world         "/hello"                 14 minutes ago   Exited (0) 14 minutes ago              stoic_shockley
67d79f95c712   hello-world         "/hello"                 About an hour ago   Exited (0) About an hour ago                       upbeat_boyd

Now that the upbeat_blackwell container is stopped delete it:

docker rm <container>

Now the container has disappeared from the list:

docker ps --all

You can also delete the other stopped containers if you like.

Naming a container

Unlike the CONTAINER ID, the NAME is something we can manipulate. The name is handy, not only for starting/connecting/stopping/destroying a container, but also for networking (which we will see in a later lab).

To set a name, add the --name parameter to Docker’s run command:

docker run --name mariadb-container -e MARIADB_ROOT_PASSWORD=my-secret-pw -d mariadb:12.0

As always, to check if this has really worked out, look at the container list:

docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
6f08ac657320        mariadb:12.0        "docker-entrypoint..."   58 seconds ago      Up 57 seconds       3306/tcp            mariadb-container
699e82ed8f1f        mariadb:12.0        "docker-entrypoint..."   24 minutes ago      Up 24 minutes       3306/tcp            jolly_bardeen

Instead of accessing the database from inside the container like in the last lab, we access it from outside using a local mysqlclient.

This is a bit tricky. First find out the IP address of your docker container. Therefore, use this command:

docker inspect mariadb-container  -f '{{ range.NetworkSettings.Networks }}{{ .IPAddress }}{{ end }}'

docker inspect <container> shows you details about a running container in JSON format (run it yourself and take a look at it). We filtered the json to only get the IP address of the container.
We could also have filtered the output with grep: docker inspect mariadb-container | grep IPAddress but our solution is more elegant 😊.

Once you have the IP (in your example 172.17.0.2) connect to it:

mysql -h172.17.0.2 -uroot -pmy-secret-pw 

If everthings works, exit mysql-client

exit;
🤔 Can you imagine another way to access the database?

Instead of entering the container from outside with mysql, we could also directly run mysql inside the container: docker exec -it mariadb-container mariadb -uroot -pmy-secret-pw