others-how to install nginx using docker ?

1. Purpose

In this post, I will demonstrate how to install nginx in docker.

2. Solution

2.1 Pull nginx docker image

docker pull nginx

2.2 Start nginx docker container

Then you can start the nginx docker container as follows:

docker run --name mynginx1 -p 80:80 -d nginx

Here are the explanations for the options of docker run command:

  • we name the container with name --name mynginx1
  • we use -p 80:80 to do port mapping, mapping from host’s port 80 to container’s port 80
  • we use -d to run the container as detached mode:

Here are some information about the detached mode and the foreground mode:

To start a container in detached mode, you use -d=true or just -d option. By design, containers started in detached mode exit when the root process used to run the container exits, unless you also specify the –rm option. If you use -d with –rm, the container is removed when it exits or when the daemon exits, whichever happens first.

In foreground mode (the default when -d is not specified), docker run can start the process in the container and attach the console to the process’s standard input, output, and standard error. It can even pretend to be a TTY (this is what most command line executables expect) and pass along signals.

2.3 Map the configuration file from host to container

If you want to map the nginx config file from host to container, you can do as follows:

First, copy the config directory from container to host:

docker cp mynginx1:/etc/nginx .

The above command copy from container mynginx1’s /etc/nginx directory to current host’s working directory.

Then we got this directory on host:

[root@mx nginx]# ll nginx
drwxr-xr-x 2 root root 4096 11月  9 16:17 conf.d
-rw-r--r-- 1 root root 1007 10月 19 15:56 fastcgi_params
-rw-r--r-- 1 root root 5349 10月 19 15:56 mime.types
lrwxrwxrwx 1 root root   22 10月 19 17:32 modules -> /usr/lib/nginx/modules
-rw-r--r-- 1 root root  648 10月 19 17:32 nginx.conf
-rw-r--r-- 1 root root  636 10月 19 15:56 scgi_params
-rw-r--r-- 1 root root  664 10月 19 15:56 uwsgi_params

[root@mx nginx]# mv nginx conf

You can see that all the config files and directories are now on the host’s current working path. And we renamed the nginx directory to conf directory because it only holds all the config files of nginx.

Then we can start the nginx container again:

docker stop mynginx1

docker rm mynginx1

docker container run \
  --rm \
  --name mynginx1 \
  --volume "$PWD/conf":/etc/nginx \
  -p 80:80 \
  -d \
  nginx

Here we use the --volume "$PWD/conf":/etc/nginx to map from host’s ./conf directory to the container’s /etc/nginx directory, then if you stop and remove the container, the config files are not touched, they can be reused in the new nginx container again.

2.4 Check nginx container is running

We can check it’s running:

[root@mx nginx]# docker ps
CONTAINER ID   IMAGE               COMMAND                  CREATED          STATUS                    PORTS NAMES
691a241bb6fe   nginx               "/docker-entrypoint.…"   13 seconds ago   Up 12 seconds             0.0.0.0:80->80/tcp, :::80->80/tcp


[root@mx nginx]# docker logs 69
/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: IPv6 listen already enabled
/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
2022/11/09 08:29:41 [notice] 1#1: using the "epoll" event method
2022/11/09 08:29:41 [notice] 1#1: nginx/1.23.2
2022/11/09 08:29:41 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2022/11/09 08:29:41 [notice] 1#1: OS: Linux 3.10.0-1160.76.1.el7.x86_64
2022/11/09 08:29:41 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2022/11/09 08:29:41 [notice] 1#1: start worker processes
2022/11/09 08:29:41 [notice] 1#1: start worker process 22

it works!



3. Summary

In this post, I demonstrated how to deploy nginx using docker container. That’s it, thanks for your reading.