Search code examples
dockernginxflaskvagrant

Unable to change nginx.conf file


I have a vagrant-box running CentOS6.7 on which I am running docker containers. I intend to run flask applications served by nginx inside the container. I had to make certain changes inside the nginx.conf file to serve my application (app1) by nginx. This may seem a little strange, but I am not able to change nginx.conf file inside /etc/nginx/conf.d/nginx.conf

Here is what I did:

Method1: Change in Dockerfile

My Dockerfile looks like this:

FROM tiangolo/uwsgi-nginx-flask:flask
COPY ./app /app
COPY ./changes/nginx.conf /etc/nginx/conf.d/nginx.conf
COPY ./changes/nginx.conf /app/

./changes/nginx.conf looks like this:

server {
    location /app1/ {
        try_files $uri @app;
    }
    location @app {
        include uwsgi_params;
        uwsgi_pass unix:///tmp/uwsgi.sock;
    }
    location /static {
        alias /app/static;
    }
}

Note the change in location in above server block from location / to location /app1/

After the image is built and I run the docker container, I exec into the running container

sudo docker exec -ti CONTAINER_ID /bin/bash

cat /app/nginx.conf shows presence of updated nginx.conf file (location changes from / to /app1/

BUT cat /etc/nginx/conf.d/nginx.conf still shows the old conf file (location is still /) I thought maybe the second COPY line is not getting executed successfully and docker isn't throwing error on console (sudo?). So, I changed the conf file manually and did a docker commit - the second approach mentioned below.

Method2: Docker commit

After the docker container was up and running, I used exec to login into the container using

[vagrant@localhost]$ sudo docker exec -ti CONTAINER_ID /bin/bash

[root@CONTAINER_ID]# vi /etc/nginx/conf.d/nginx.conf

Changing the file to reflect below:

server {
    location /app1/ {
        try_files $uri @app;
    }
    location @app {
        include uwsgi_params;
        uwsgi_pass unix:///tmp/uwsgi.sock;
    }
    location /static {
        alias /app/static;
    }
}

Saved the file wq! and exit the container. After that I did sudo docker commit CONTAINER_ID my_new_image

Starting a new container and re-logging into container running on my_new_image still gives below nginx.conf file inside /etc/nginx/conf.d/nginx.conf:

server {
    location / {
        try_files $uri @app;
    }
    location @app {
        include uwsgi_params;
        uwsgi_pass unix:///tmp/uwsgi.sock;
    }
    location /static {
        alias /app/static;
    }
}

I can tell that the my_new_image has some changes because it is larger in size than tiangolo/uwsgi-nginx-flask-docker because I had installed vim to edit the file. But somehow file changes are not persisting inside /etc/nginx/conf.d/nginx.conf

What am I doing wrong?

UPDATE Github repo: https://github.com/VimanyuAgg/flask-nginx.git


Solution

  • As it turns out, the parent image has already added a specific file into the image, which is read-only. This means you sadly can't change or overwrite the file.

    A hacky solution to this is to mount the /etc/nginx/conf.d folder into a directory on your host (let's say /usr/local/share/nginxconf) and change the file there, which is in another namespace and different permissions (afaik). It worked on my end.

    docker create -v /usr/local/share/nginxconf:/etc/nginx/conf.d --name flask-test IMAGE_NAME
    
    vim /usr/local/share/nginxconf/nginx.conf
    

    Another, fancier solution would be to take another parent image, which doesn't add the file beforehand. But I don't know what dependencies you need for your app, so it might be a painful search. You could of course also create your own (which would also give you some training in writing Dockerfiles).

    References to writing a successful Dockerfile:

    Dockerfile Reference

    Dockerfile Best Practices

    Handy tutorial to dockerize PostreSQL by Docker