Unable to change nginx.conf file

Vim*_*nyu 4 nginx flask vagrant docker

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/
Run Code Online (Sandbox Code Playgroud)

./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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

我可以说my_new_image有一些变化,因为它的大小比tiangolo / uwsgi-nginx-flask-docker大,因为我已经安装了vim来编辑文件。但是不知何故文件更改不会持久保存在/etc/nginx/conf.d/nginx.conf中

我究竟做错了什么?

更新 Github存储库:https : //github.com/VimanyuAgg/flask-nginx.git

sam*_*rog 6

事实证明,父映像已经在映像中添加了一个特定文件,该文件是只读的。这意味着您很难更改或覆盖文件。

一个解决方案是将/etc/nginx/conf.d文件夹安装到主机上的目录中(例如/usr/local/share/nginxconf),然后在该目录中更改文件,该文件位于另一个名称空间和不同的权限(afaik)中。它对我有效。

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

vim /usr/local/share/nginxconf/nginx.conf
Run Code Online (Sandbox Code Playgroud)

另一个更好的解决方案是获取另一个父映像,该映像不会事先添加文件。但是我不知道您的应用需要什么依赖关系,因此这可能是一个痛苦的搜索。您当然也可以创建自己的(这也将给您一些编写Dockerfile的培训)。

编写成功的Dockerfile的参考:

Dockerfile参考

Dockerfile最佳实践

方便的教程,通过Docker Docker化PostreSQL