2

When using docker-compose, nginx isn't showing files like other images might.

For example with Mysql, the below code will save the data created at /var/lib/mysql to the local machine at ./volumes/db/data

./volumes/db/data:/var/lib/mysql

Another example, with Wordpress, the below code will save the data created at /var/www/html/wp-content/uploads to the local machine at ./volumes/uploads/data

./volumes/uploads/data:/var/www/html/wp-content/uploads

This is not working with nginx though so no matter what I change /some/nginx/path to, it never appears at ./volumes/nginx/data

./volumes/nginx/data:/some/nginx/path

Does nginx work differently in this regard?


Update

Using a named volume with the following configurations solved this problem:

  • In the services section of the docker-compose file, I changed ./volumes/nginx/data:/some/nginx/path to nginx_data:/some/nginx/path

  • And then my volumes section reads as follows


volumes:
  nginx_data:
    driver: local
    driver_opts:
      o: bind
      device: ${PWD}/volumes/nginx/data
wfaye
  • 268
  • 1
  • 2
  • 10

2 Answers2

0

There should be no difference, a volume is mounting a local directory to a directory in the container. Either you are not mounting correctly or you are mounting an incorrect path inside the nginx container (one which nginx does not use).

Based on the offical nginx docker image docs on https://docs.docker.com/samples/library/nginx/ you should mount to /usr/share/nginx/html

$ docker run --name some-nginx -v /some/content:/usr/share/nginx/html:ro -d nginx

In additonal I would include full paths in your docker-compose.yaml:

volumes:
  - /full_path/volumes/nginx/data:/usr/share/nginx/html

If this is still not working you should exec into the container and confirm that the directory is mounted:

$ docker exec -it <container_name> sh
$ df -h | grep nginx

# write data, confirm you see it on the docker host's directory
$ cd /usr/share/nginx/html
$ touch foo

# on docker host
$ ls /full_path/volumes/nginx/data/foo

If any of this is failing I would look at docker logs to see if there was an issue mounting the directory, perhaps a path or permission issue.

$ docker logs <container_name>

--- UPDATE ---

I ran everything you are using and it just worked:

$ cat Dockerfile
FROM nginx
RUN touch /usr/share/nginx/html/test1234 && ls /usr/share/nginx/html/


$ docker build -t nginx-image-test .; docker run -p 8889:80 --name some-nginx -v /full_path/test:/usr/share/nginx/html:rw -d nginx-image-test; ls ./test;
...

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
33ccbea6c1c1        nginx-image-test    "nginx -g 'daemon of…"   4 minutes ago       Up 4 minutes        0.0.0.0:8889->80/tcp   some-nginx

$ cat test/index.html
hello world

$ curl -i http://localhost:8889
HTTP/1.1 200 OK
Server: nginx/1.15.3
Date: Thu, 06 Sep 2018 15:35:43 GMT
Content-Type: text/html
Content-Length: 12
Last-Modified: Thu, 06 Sep 2018 15:31:11 GMT
Connection: keep-alive
ETag: "5b91483f-c"
Accept-Ranges: bytes

hello world

-- UPDATE 2 -- Awesome you figured it out, this post seems to explain why: docker data volume vs mounted host directory

The host directory is, by its nature, host-dependent. For this reason, you can’t mount a host directory from Dockerfile because built images should be portable. A host directory wouldn’t be available on all potential hosts.

If you have some persistent data that you want to share between containers, or want to use from non-persistent containers, it’s best to create a named Data Volume Container, and then to mount the data from it.

d g
  • 1,594
  • 13
  • 13
  • Thanks, this helps to solve my question, but I realize that I have a slightly different issue than what my question asked. I have data that I want to be on the container at /usr/share/nginx/html:rw, but this data disappears when the volume mounts. to contrast again with the wordpress image, i can place data at /usr/src/wordpress/ and it will appear at /var/www/html/ when wordpress mounts the volume. does nginx have a similar folder where i can place files so that they get mounted to /usr/share/nginx/html ? – wfaye Sep 06 '18 at 13:49
  • Did you confirm that your have a mount point within the container and that you can write to it in within the container and read on the host? Next troubeshooting steps would depend on how that all works out. Also are you using the official niginx image? I would simply try a docker run with your volumes as params and see if that works, avoiding docker-compose and any other external stuff. – d g Sep 06 '18 at 14:15
  • Yes, I can confirm that the mount point is writable on the contatiner and readable on the host. Also, yes to using the official nginx image. i'm getting the same results with docker run too. this all is good, but i want to go further (but maybe this is the wrong approach) -- there are certain files that get downloaded using the RUN command, and once i mount to that folder, the files disappear. in the wordpress version, i can download the files to /usr/src/wordpress and they later appear at /var/www/html so i wanted to do the same with nginx – wfaye Sep 06 '18 at 14:28
  • it requires a dockerfile, here it is -- Dockerfile -- FROM nginx RUN touch /usr/share/nginx/html/test1234 && ls /usr/share/nginx/html/ -- COMMAND LINE -- docker build -t nginx-image-test .; docker run --name some-nginx -v ~/test:/usr/share/nginx/html:rw -d nginx-image-test; ls ~/test; -- EXPLANATION -- you will notice that ~/test is empty and test1234 is not there. it got mounted on top of the original /usr/share/nginx/html. with the wordpress image, you can place test1234 somewhere else, so that when /usr/share/nginx/html mounts at ~/test, then you will see test1234 – wfaye Sep 06 '18 at 15:15
  • I reviewed the comment, but I'm not seeing that the file test1234 is existing on your host. So I'd correctly expect test/index.html to be there, but the test1234 that get's created in the dockerfile is not present – wfaye Sep 06 '18 at 15:45
  • 1
    Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/179562/discussion-between-d-g-and-wfaye). – d g Sep 06 '18 at 16:06
  • I finally solved this problem using named volumes, but perhaps you can provide more insight? I'm going to update the question – wfaye Sep 06 '18 at 16:39
-1

I ran across this issue as well when trying to create a volume to place my own nginx configurations into etc/nginx. While I never found out the real issue I think it has to do with how nginx is built from the Dockerfile.

I solved the problem by using my own Dockerfile to extend the original and copy over the configuration files at build time. Hopefully this helps.

FROM nginx:1.15.2

COPY ./nginx.conf /etc/nginx/nginx.conf
COPY ./global /etc/nginx/global
COPY ./conf.d /etc/nginx/conf.d
Aaron
  • 179
  • 9