R Lopez
R Lopez

Reputation: 199

403 with nginx for file located in binded volume with docker

I am trying to use my nginx server on docker but I cannot use the files / folder if they belong to my volume. Problem, the goal of my test is to keep a volume between the file in my computer and the container. I have searched during 3 days and tried a lot of solution but no effects...( useradd, chmod, chown, www_data, etc.....) I don't understand how is it possible to use ngnix, a volume and docker? The only solution actually for me is to copy the folder of my volume in another folder, and so I can chown the folder and use NGIX. There is no official solution on the web and I am surprised because for me using docker with a volume binded with his container would be the basic for a daily work. If someone has managed to implement it, I would be very happy if you could share you code. I need to understand what I am missing.

FYI I am working with a VM.

Thanks !

Upvotes: 1

Views: 553

Answers (1)

Juan Fontes
Juan Fontes

Reputation: 908

I think you are not passing the right path in the volume option. There are a few ways to do it, you can pass the full path or you can use the $(pwd) if you are using a Linux machine. Let's say you are on /home/my-user/code/nginx/ and your HTML files are on html folder.

You can use:

$ docker run --name my-nginx -v /home/my-user/code/nginx/html/:/usr/share/nginx/html:ro -p 8080:80 -d nginx
or
$ docker run --name my-nginx -v ~/code/nginx/html/:/usr/share/nginx/html:ro -p 8080:80 -d nginx
or
$ docker run --name my-nginx -v $(pwd)/html/:/usr/share/nginx/html:ro -p 8080:80 -d nginx

I've created an index.html file inside the html folder, after the docker run, I was able to open it:

$ echo "hello world" >> html/index.html
$ docker run --name my-nginx -v $(pwd)/html/:/usr/share/nginx/html:ro -p 8080:80 -d nginx
$ curl localhost:8080
hello world

You can also create a Dockerfile, but you would need to use COPY command. I'll give a simple example that's working, but you should improve this by using a version and etc..

Dockerfile:

FROM nginx
COPY ./html /usr/share/nginx/html

...

$ docker build -t my-nginx:0.0.1 .
$ docker run -d -p 8080:80 my-nginx:0.0.1  
$ curl localhost:8080
hello world

You can also use docker-compose. By the way, those examples are just to give you some idea of how it works.

Upvotes: 1

Related Questions