July 23, 2020

[Note] Nginx in Docker


Prepare host directory.  Will have docs/config on host:
$ mkdir /data/httpd
$ mkdir /data/httpd/htdocs
$ mkdir /data/httpd/conf.d
$ mkdir /data/httpd/logs

Found port 80 was in use by lighttpd,
kkim@cherry:/data/httpd/conf$ sudo ss -tulpn | grep :80
tcp    LISTEN  0       1024         *      users:(("lighttpd",pid=2797,fd=4))                                            
tcp    LISTEN  0       500        *      users:(("mono",pid=1615,fd=5))                                                
tcp    LISTEN  0       100                       *:8080                 *:*      users:(("java",pid=1703,fd=39))                                               
tcp    LISTEN  0       1        [::ffff:]:8005                 *:*      users:(("java",pid=2533,fd=61))                      

Check if I can remove it,
$ apt-cache rdepends --installed lighttpd

...I see SSL and other dependencies, can't remove it. 

Just disable it then
$ sudo service lighttpd stop
$ sudo systemctl disable lighttpd

Get Nginx docker image
$ docker pull nginx

To get copy of nginx configuration file, create temp container:
$ docker run --name tmp-nginx-container -d nginx

Wait until container comes up completely.
$ docker logs -f tmp-nginx-container

Copy config file from the container to host
$ docker cp tmp-nginx-container:/etc/nginx/nginx.conf /data/httpd/nginx.conf
$ docker cp tmp-nginx-container:/etc/nginx/conf.d/ /data/httpd/conf.d/

Take a pick, if you want
$ docker exec -ti tmp-nginx-container bash

Remove the temporary container
$ docker rm -f tmp-nginx-container

Run the container,
$ docker run --name nginx                               \
    -v /data/httpd/htdocs:/usr/share/nginx/html:ro      \
    -v /data/httpd/nginx.conf:/etc/nginx/nginx.conf:ro  \
    -v /data/httpd/conf.d:/etc/nginx/conf.d:ro          \
    -v /data/httpd/logs:/var/log/nginx                  \
    -p 80:80 -d nginx

No comments: