Background containers are how you'll run most applications. Here's a simple example using MySQL.
-
Run a new MySQL container with the following command.
docker run \
--detach \
--name mydb \
-e MYSQL_ROOT_PASSWORD=my-secret-pw \
mysql:latest
--detach
will run the container in the background.
--name
will name it mydb
.
-e
will use an environment variable to specify the root password (NOTE: This should never be done in production).
-
List the running containers.
docker ps
Notice your container is running.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c720e0482ce7 mysql:latest "docker-entrypoint.sâĶ" About a minute ago Up 55 seconds 3306/tcp, 33060/tcp mydb
-
You can check what's happening in your containers by using a couple of built-in Docker commands: docker logs
and docker top
.
docker logs mydb
This shows the logs from the MySQL Docker container.
<output truncated>
2023-06-20T03:12:49.102168Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2023-06-20T03:12:49.122241Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
2023-06-20T03:12:49.122342Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.33' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL.
Let's look at the processes running inside the container.
docker top mydb
You should see the MySQL daemon (mysqld
) is running in the container.
UID PID PPID C STIME TTY TIME CMD
lxd 4420 4392 0 10:34 ? 00:00:00 mysqld
Although MySQL is running, it is isolated within the container because no network ports have been published to the host. Network traffic cannot reach containers from the host unless ports are explicitly published.
-
List the MySQL version using docker exec
.
docker exec
allows you to run a command inside a container. In this example, we'll use docker container exec
to run the command-line equivalent of mysql --user=root --password=$MYSQL_ROOT_PASSWORD --version
inside our MySQL container.
docker exec -it mydb mysql --user=root --password=$MYSQL_ROOT_PASSWORD --version
You will see the MySQL version number, as well as a handy warning.
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql Ver 8.0.33 for Linux on x86_64 (MySQL Community Server - GPL)
-
You can also use docker exec
to connect to a new shell process inside an already-running container. Executing the command below will give you an interactive shell (sh
) inside your MySQL container.
docker exec -it mydb sh
Notice that your shell prompt has changed. This is because your shell is now connected to the sh
process running inside of your container.
-
Let's check the version number by running the same command again, only this time from within the new shell session in the container.
mysql --user=root --password=$MYSQL_ROOT_PASSWORD --version
Notice the output is the same as before.
-
Type exit
to leave the interactive shell session.
exit