DEV Community

Cover image for Manage NGINX configurations inside Docker container
Vasyl Kutsyk
Vasyl Kutsyk

Posted on

Manage NGINX configurations inside Docker container

Originaly posted here

I believe in any product there is a time when you have to configure a proxy server. Will it be to expose UI applications or just redirect requests to different APIs it depends, but time will come. Iโ€™ve made a simple example that will help you configure or investigate your NGINX docker container with configuration change on the fly.

Donโ€™t do this in your production environment.


For the beginning, we need to understand how our NGINX docker image looks like.

Dockerfile

FROM nginx:stable-alpine
RUN rm -rf /usr/share/nginx/html/*
COPY ./dist/app /usr/share/nginx/html
Enter fullscreen mode Exit fullscreen mode

In the same directory that you created your Dockerfile, create folders directory dist/app and put some dummy index.html

Here is an example of index.html:

<!DOCTYPE html>
<html>
<body>
<h2>An Unordered HTML List</h2>
<ul>
  <li>Coffee</li>
  <li>Tea</li>
  <li>Milk</li>
</ul>
<h2>An Ordered HTML List</h2>
<ol>
  <li>Coffee</li>
  <li>Tea</li>
  <li>Milk</li>
</ol>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Build your image:

docker build -t nginx:latest .

As the result, you should have your image ready to be started:

REPOSITORY          TAG                 IMAGE ID            
nginx               latest              .....
Enter fullscreen mode Exit fullscreen mode

Run your docker image:
docker run -d -p 8080:80 nginx

Validate that everything is working smooth:

test-vm@vm-test:~/dockertest$ curl http://localhost:8080
<!DOCTYPE html>
<html>
<body>
<h2>An Unordered HTML List</h2>
<ul>
  <li>Coffee</li>
  <li>Tea</li>
  <li>Milk</li>
</ul>
<h2>An Ordered HTML List</h2>
<ol>
  <li>Coffee</li>
  <li>Tea</li>
  <li>Milk</li>
</ol>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Now you should enter into your container, to do that get a container id.

test-vm@vm-test:~/dockertest$ docker ps
CONTAINER ID   IMAGE  COMMAND  CREATED   STATUS  PORTS     NAMES                           
022c55a80088   nginx  "/d.โ€ฆ"   7 m ago   Up 7 m  8080      busy
test-vm@vm-test:~/dockertest$ docker exec -it 022c55a80088 /bin/sh
/ #
Enter fullscreen mode Exit fullscreen mode

Voila, we are in the container and can change everything on the fly.

The configuration file itself is located in the default directory:
/etc/nginx/nginx.conf

You can go there, modify configurations and easily validate them:

/ # nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Enter fullscreen mode Exit fullscreen mode

Validation of file is good, but to apply it, we need to restart our nginx service:

/ # nginx -s reload
2021/11/10 12:01:50 [notice] 46#46: signal process started
Enter fullscreen mode Exit fullscreen mode

And thatโ€™s it, now testing of configuration change for your NGINX service in Docker container is easier and no need for rebuilding on every change.


Thanks for reading!
If you have an interesting experience with live changes in Docker containers or you are interested in another topic, please add comments and upvote ๐Ÿ‘. Iโ€™m interested in the dialog.

Top comments (0)