You can create Docker containers out of Docker container images. While we will discuss container images and their architecture in the following chapters, an excellent way to visualize them is as a copy of all files, application libraries, and dependencies comprising your application environment, similar to a virtual machine image.
To run a Docker container, we can use the docker run command, which has the following structure:
$ docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG…]
Let’s look at the docker run command and its variations using working examples.
In its simplest form, you can use docker run by simply typing the following:
$ docker run hello-world
Unable to find image ‘hello-world:latest’ locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:e7c70bb24b462baa86c102610182e3efcb12a04854e8c582 838d92970a09f323
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
…
As you may recall, we used this command when we installed Docker. Here, I have purposefully omitted tag, options, command, and arguments. We will cover it with multiple examples to show its actual use cases.
As we didn’t supply tag, Docker automatically assumed the tag as latest, so if you look at the command output, you will see that Docker is pulling the hello-world:latest image from Docker Hub.
Now, let’s look at an example with a specific version tag.
Running containers from versioned images
We can run nginx:1.18.0 using the following command:
$ docker run nginx:1.18.0
Unable to find image ‘nginx:1.18.0’ locally
1.18.0: Pulling from library/nginx
852e50cd189d: Pull complete
48b8657f2521: Pull complete
b4f4d57f1a55: Pull complete
d8fbe49a7d55: Pull complete
04e4a40fabc9: Pull complete
Digest: sha256:2104430ec73de095df553d0c7c2593813e01716a48d66f 85a3dc439e050919b3
Status: Downloaded newer image for nginx:1.18.0
/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: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: 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: Configuration complete; ready for start up
Note that the prompt will be stuck after this. There is a reason for this: nginx is a long- running process, also known as a daemon. Since NGINX is a web server that needs to listen to HTTP requests continuously, it should never stop. In the case of the hello-world application, its only job was to print the message and exit. NGINX has a different purpose altogether.
Now, no one would keep a Bash session open for a web server to run, so there has to be some way to run it in the background. You can run containers in the detached mode for that. We’ll have a look at this in the next section.
Running Docker containers in the background
To run a Docker container in the background as a daemon, you can use docker run in detached mode using the -d flag:
$ docker run -d nginx:1.18.0
beb5dfd529c9f001539c555a18e7b76ad5d73b95dc48e8a35aecd7471ea938fc
As you can see, it just prints a random ID and provides control back to the shell.