Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting forbidden error while using nginx inside docker

My Docker file contents are

FROM nginx
COPY /src /usr/share/nginx/html

My docker compose file contents are

version: '2'
services:
  app:
    build: .
    image: app:1.0.0
    volumes:
    - ./src:/usr/share/nginx/html
    ports:
    - "8080:80"

I'm getting following error when run docker-compose

*1 directory index of "/usr/share/nginx/html/" is forbidden

I tried giving permission to directory -> /usr/share/nginx/html it didn't work.

I want to sync host directory with docker container.

like image 396
Chinmay Kulkarni Avatar asked Mar 13 '18 11:03

Chinmay Kulkarni


People also ask

How do I fix 403 Forbidden nginx?

However, if the specified index files are not in the directory, Nginx will return 403 forbidden error. One way to resolve this issue is to add the index file specified in the configuration file or add the available index file to the config file.

What causes 403 Forbidden nginx?

Causes of 403 Forbidden Often, HTTP 403 forbidden errors are caused by an access misconfiguration on the client-side, which means you can usually resolve the issue yourself. A common cause of these errors is the file or folder permission settings, which control who can read, write, and execute the file or folder.

Can I run nginx in Docker?

Running NGINX Plus in a Docker ContainerDocker can also be used with NGINX Plus. The difference between using Docker with NGINX Open Source is that you first need to create an NGINX Plus image, because as a commercial offering NGINX Plus is not available at Docker Hub.

Should nginx run inside Docker?

If nginx is running in a container then your site is going to be 100% dead to the world while Docker isn't running. Users will get a connection error. When nginx is installed directly on your host you can serve a 503 maintenance page that doesn't depend on Docker or any containers running.


1 Answers

I was facing the same issue so the posting answer might help some else or some else looking for the same problem

I am running nginx official image.

docker run --rm --name some-nginx -p 8080:80 -v /test/html5-youtube.js/examples/:/usr/share/nginx/html -it nginx

I was getting this error.

[error] 8#8: *3 directory index of "/usr/share/nginx/html/" is *forbidden*, client: 172.17.0.1, server: localhost, request: "GET / HTTP/1.1", host: "localhost:8080"

So when I check the directory permission it was wrong.

enter image description here

Check the user in nginx.conf in my case it is nginx so change file permission.

cd /usr/share/nginx/html
chown nginx:nginx ./*

and then I was able to get a response from the server.

172.17.0.1 - - [30/Oct/2018:09:04:52 +0000] "GET /html5-youtube.js HTTP/1.1" 404 571 "http://localhost:8080/player.html" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36" "-"

Update:

Or add the permission at build time if you are not using bind volume.

RUN chown nginx:nginx /usr/share/nginx/html/*
like image 76
Adiii Avatar answered Sep 23 '22 18:09

Adiii