Docker - run command

Docker

In this tutorial we will learn more about docker run command.

Table of Content

docker run DOCKER_IMAGE_NAME To run a docker container

We use the docker run command to run a docker container.

In the following example we are running nginx container.

➜ docker run nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2023/03/05 13:15:15 [notice] 1#1: using the "epoll" event method
2023/03/05 13:15:15 [notice] 1#1: nginx/1.23.3
2023/03/05 13:15:15 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2023/03/05 13:15:15 [notice] 1#1: OS: Linux 5.15.49-linuxkit
2023/03/05 13:15:15 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2023/03/05 13:15:15 [notice] 1#1: start worker processes
2023/03/05 13:15:15 [notice] 1#1: start worker process 29
2023/03/05 13:15:15 [notice] 1#1: start worker process 30
2023/03/05 13:15:15 [notice] 1#1: start worker process 31
2023/03/05 13:15:15 [notice] 1#1: start worker process 32
2023/03/05 13:15:15 [notice] 1#1: start worker process 33
2023/03/05 13:15:15 [notice] 1#1: start worker process 34

This runs the docker container in attached mode and hence it takes over the terminal and we cannot run any other command.

In order to stop this process we can press Ctrl+C.

We will see the following if we press Ctrl+C

^C2023/03/05 13:17:37 [notice] 1#1: signal 2 (SIGINT) received, exiting
2023/03/05 13:17:37 [notice] 29#29: exiting
2023/03/05 13:17:37 [notice] 29#29: exit
2023/03/05 13:17:37 [notice] 31#31: exiting
2023/03/05 13:17:37 [notice] 30#30: exiting
2023/03/05 13:17:37 [notice] 30#30: exit
2023/03/05 13:17:37 [notice] 31#31: exit
2023/03/05 13:17:37 [notice] 32#32: exiting
2023/03/05 13:17:37 [notice] 33#33: exiting
2023/03/05 13:17:37 [notice] 34#34: exiting
2023/03/05 13:17:37 [notice] 32#32: exit
2023/03/05 13:17:37 [notice] 34#34: exit
2023/03/05 13:17:37 [notice] 33#33: exit
2023/03/05 13:17:37 [notice] 1#1: signal 17 (SIGCHLD) received from 31
2023/03/05 13:17:37 [notice] 1#1: worker process 29 exited with code 0
2023/03/05 13:17:37 [notice] 1#1: worker process 30 exited with code 0
2023/03/05 13:17:37 [notice] 1#1: worker process 31 exited with code 0
2023/03/05 13:17:37 [notice] 1#1: signal 29 (SIGIO) received
2023/03/05 13:17:37 [notice] 1#1: signal 17 (SIGCHLD) received from 30
2023/03/05 13:17:37 [notice] 1#1: signal 17 (SIGCHLD) received from 34
2023/03/05 13:17:37 [notice] 1#1: worker process 32 exited with code 0
2023/03/05 13:17:37 [notice] 1#1: worker process 33 exited with code 0
2023/03/05 13:17:37 [notice] 1#1: worker process 34 exited with code 0
2023/03/05 13:17:37 [notice] 1#1: exit

If we list all the process that are running or stopped we will get the nginx container in that list.

docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS                      PORTS     NAMES
679d9ee01d9e   nginx     "/docker-entrypoint.…"   3 minutes ago   Exited (0) 51 seconds ago             hungry_shtern

docker run -d DOCKER_IMAGE_NAME To run a docker container in detach mode

We use the docker run -d command to run a docker container in detach mode. By using the -d option we run the container in the background.

docker run -d nginx
f176b1217d22634822deb8ab2789f0a627b95ebed7a733d10fa0c4459b9c0a5e

docker run --name CUSTOM_NAME DOCKER_IMAGE_NAME To run a docker container with a custom name

We use the --name option to set a custom name to our docker container.

In the following example we are giving our Redis container a custom name.

➜ docker run -d --name my-redis redis
82413d38a401dc498d7a472f320b48b9d994b44309a1fd5f5e299c730956a352


➜ docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS      NAMES
82413d38a401   redis     "docker-entrypoint.s…"   19 seconds ago   Up 18 seconds   6379/tcp   my-redis

docker run -e ENV_VARIABLE=ENV_VALUE DOCKER_IMAGE_NAME To run a docker container with environment variable

We use the -e option to set environment variables when we want to run the docker container.

In the following example we are running mysql docker container with environment variable -e MYSQL_ROOT_PASSWORD=root1234

docker run -e MYSQL_ROOT_PASSWORD=root1234 -d mysql

docker run DOCKER_IMAGE_NAME:TAG To use a specific version of docker image to run the docker container

We use the :TAG to use a specific version of the docker image to run the docker container.

In the following example we are going to run Redis version 6.

➜ docker run redis:6

Unable to find image 'redis:6' locally
6: Pulling from library/redis
3f9582a2cbe7: Pull complete
241c2d338588: Pull complete
89515d93a23e: Pull complete
fa652736229a: Pull complete
bec4ea297d1a: Pull complete
79ebc8e077a8: Pull complete
Digest: sha256:2331b05cb2b4f0504e8cff8ede58d77ea291f47c2e4522e50fae50b0db46757a
Status: Downloaded newer image for redis:6
1:C 05 Mar 2023 13:30:13.884 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 05 Mar 2023 13:30:13.884 # Redis version=6.2.11, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 05 Mar 2023 13:30:13.884 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 05 Mar 2023 13:30:13.885 * monotonic clock: POSIX clock_gettime
1:M 05 Mar 2023 13:30:13.885 * Running mode=standalone, port=6379.
1:M 05 Mar 2023 13:30:13.886 # Server initialized
1:M 05 Mar 2023 13:30:13.886 * Ready to accept connections

Note! If a TAG is not specified then docker picks the latest version (tag name: latest) and runs the container.

docker run -p HOST_PORT:CONTAINER_PORT DOCKER_IMAGE_NAME To run a docker container and map host port to container port

Lets say we have a docker image of redis

Now lets say we want to access redis docker container from port 9000 of the host. To achieve this we would run the following command.

docker run -p 9000:6379 -d redis

This will map port 9000 of the host to port 6379 of the docker container.


       USER
        |
        |
        V
      +----+
+-----|9000|-----------------+
|     +----+                 |
|       |                    |
|       |                    |
|       |                    |
|       V                    |
|     +----+                 |
| +---|6379|--+              |
| |   +----+  |              |
| |           |              |
| | docker    |              |
| | container |              |
| +-----------+              |
|                            |
|                            |
|        docker host         |
+----------------------------+

By port forwarding we can run multiple redis docker containers (instances) of the same redis docker image and map them to different host ports.

➜  docker run -d -p 9000:6379 redis
521951e4b4d3260ecb05c64f63c9cf4caa22f50943e187337b8e15b9ccd0d9fa


➜  docker run -d -p 9001:6379 redis
96311f91dd32835f38528258183b193771ea37cd68f0faef1f7b7923db8fa605


➜  docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                    NAMES
96311f91dd32   redis     "docker-entrypoint.s…"   27 seconds ago   Up 26 seconds   0.0.0.0:9001->6379/tcp   modest_meninsky
521951e4b4d3   redis     "docker-entrypoint.s…"   31 seconds ago   Up 30 seconds   0.0.0.0:9000->6379/tcp   flamboyant_montalcini

      +----+           +----+
+-----|9000|-----------|9001|-------+
|     +----+           +----+       |
|       |                |          |
|       |                |          |
|       |                |          |
|       V                V          |
|     +----+           +----+       |
| +---|6379|----+  +---|6379|----+  |
| |   +----+    |  |   +----+    |  |
| |             |  |             |  |
| | docker      |  | docker      |  |
| | container_1 |  | container_2 |  |
| +-------------+  +-------------+  |
|                                   |
|                                   |
|        docker host                |
+-----------------------------------+

docker run -v HOST_PATH:CONTAINER_PATH DOCKER_IMAGE_NAME To run a docker container and map host storage location to container storage location

When we run a docker container, all the data is saved inside the container at specified location as per the application the docker container is running.

For example, mysql is configured to save all its data in the /var/lib/mysql folder inside the docker container.

If the container is deleted then all the data gets lost. If we want to save the data outside the docker container at a specific location on the host then we can use the -v option and map a folder on the host machine to the container folder like the following.

docker run -v /opt/data/mysql:/var/lib/mysql -d mysql