In this Lab, we’ll install Docker Nginx image and start it as a systemd service.

We’ll do it in a CentOS 7.2 virtual machine :

[root@nginx ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)

Docker has been already installed and running :

[root@nginx ~]# rpm -qa docker-engine
[root@nginx ~]# systemctl status docker | grep Active
   Active: active (running) since Thu 2016-08-18 20:00:11 CEST; 17min ago

Install Nginx image :

[root@nginx ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx

51f5c6a04d83: Pull complete
a3ed95caeb02: Pull complete
51d229e136d0: Pull complete
bcd41daec8cc: Pull complete
Digest: sha256:0fe6413f3e30fcc5920bc8fa769280975b10b1c26721de956e1428b9e2f29d04
Status: Downloaded newer image for nginx:latest

To edit Nginx configuration files

We have no way to edit those files within the container,

So, in a terminal session, launch the container in interactive mode :

[root@nginx ~]# docker run --name my-nginx -p 80:80 -p 443:443 -ti --rm nginx

Open an other session to copy the directory :

  • Create a directory where you want to have this directory :
[root@nginx ~]# mkdir -p /docker/my-nginx
  • Then copy files :
root@nginx ~]# docker cp my-nginx:/etc/nginx /docker/my-nginx
  • We have thoses files copied :
[root@nginx ~]# tree /docker/my-nginx/nginx
├── conf.d
│   └── default.conf
├── fastcgi_params
├── koi-utf
├── koi-win
├── mime.types
├── modules -> /usr/lib/nginx/modules
├── nginx.conf
├── scgi_params
├── uwsgi_params
└── win-utf

1 directory, 10 files

Of course, we can edit configuration files, here for example, we had an comment line :

[root@nginx ~]# head -1 /docker/my-nginx/nginx/conf.d/default.conf
# We can edit this configuration file outside Nginx docker container !

To store the WEB content

We’ll do another copy,

Do the copy :

[root@nginx ~]# docker cp my-nginx:/usr/share/nginx/html /docker/my-nginx

We have now :

[root@nginx ~]# tree /docker/my-nginx/html
├── 50x.html
└── index.html

0 directories, 2 files

For example, we modified “index.html”.

Check this new container configuration

Stop the Nginx container (press Control C), and launch it again with volume definitions :

[root@nginx ~]# docker run --name my-nginx -p 80:80 -p 443:443 -v /docker/my-nginx/nginx:/etc/nginx -v /docker/my-nginx/html:/usr/share/nginx/html -ti --rm nginx

Open a WEB browser :

nginx docker






OK, we see the overridden “index.html” page.

Display the configuration file (open a “bash” session to this container) :

[root@nginx ~]# docker exec -ti my-nginx bash
root@1f2737653981:/# head -1 /etc/nginx/conf.d/default.conf
# We can edit this configuration file outside Nginx docker container !
root@1f2737653981:/# exit

To launch this Docker container using “systemd”

Stop the container and edit a service configuration file :

[root@nginx ~]# cat /etc/systemd/system/my-nginx.service
Description=NginX service

ExecStartPre=-/usr/bin/docker kill my-nginx
ExecStartPre=-/usr/bin/docker rm my-nginx
ExecStart=/usr/bin/docker run --name my-nginx \
                              -p 80:80 -p 443:443 \
                              -v /docker/my-nginx/nginx:/etc/nginx \
                              -v /docker/my-nginx/html:/usr/share/nginx/html \

ExecStop=-/usr/bin/docker stop my-nginx


Now, you can start this container as a service :

  • A “systemd” service :
[root@nginx ~]# systemctl status my-nginx
● my-nginx.service - NginX service
   Loaded: loaded (/etc/systemd/system/my-nginx.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
  • You can start it :
[root@nginx ~]# systemctl start my-nginx
  • It works :
[root@nginx ~]# systemctl status my-nginx | grep Active
   Active: active (running) since Thu 2016-08-18 22:39:38 CEST; 45s ago
[root@nginx ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                      NAMES
2b4472336688        nginx               "nginx -g 'daemon off"   2 minutes ago       Up 2 minutes>80/tcp,>443/tcp   my-nginx

You want to get this container restarted after a reboot

No problem, what is you “run level” :

[root@nginx ~]# systemctl get-default

Enable this service :

[root@nginx ~]# systemctl enable my-nginx
Created symlink from /etc/systemd/system/ to /etc/systemd/system/my-nginx.service.

Modify “index.html” welcome page, and reboot your machine. Using a WEB browser, you will see :

nginx docker1








PDF24    Send article as PDF   

2 thoughts on “systemd & Docker containers, Nginx for example

Leave a Reply

Your email address will not be published. Required fields are marked *