简体   繁体   中英

Docker Alpine nginx 403 Forbidden error for swagger-ui-builder

This happened on Ubuntu 16.04 in VirtualBox on Windows 10, with docker version 1.12.1, and swagger-ui version 2.2.2 .

I was trying to build and run Swagger UI in a docker container, following the instructions on their site :

docker build -t swagger-ui-builder .

docker run -p 127.0.0.1:8080:8080 swagger-ui-builder

The instruction says that now I should be able to view the swagger-ui running, however, when I opened 127.0.0.1:8080 I only got this page back:

<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.8.1</center>
</body>
</html>

This is the content of the Dockerfile:

FROM alpine:3.3

MAINTAINER Roman Tarnavski

RUN apk add --update nginx

COPY nginx.conf /etc/nginx/
ADD ./dist/ /usr/share/nginx/html

EXPOSE 8080

CMD nginx -g 'daemon off;'

I found similar posts on stackoverflow, but none of them helped me solve this problem. What am I doing wrong and how to fix this?

The problem was caused by a permission requirement: the www-data user/group did not have access to website's directory and files.

This problem was explained in the accepted answer to this post: Nginx 403 forbidden for all files

To solve this, the following lines have to be added to the Dockerfile:

RUN set -x ; \
  addgroup -g 82 -S www-data ; \
  adduser -u 82 -D -S -G www-data www-data && exit 0 ; exit 1

RUN chown -R www-data:www-data /usr/share/nginx/html/*
RUN chmod -R 0755 /usr/share/nginx/html/*

The upper part of the commands are explained in this gist: https://gist.github.com/briceburg/47131d8caf235334b6114954a6e64922

The user/group www-data has to be added first, before the permission can be set for it. The snippet notes that 82 is the standard uid/gid for "www-data" in Alpine

The lower part of the commands is the solution to a similar question in another forum: https://www.digitalocean.com/community/questions/nginx-403-forbidden--2

So the fixed Dockerfile would look like this:

FROM alpine:3.3

MAINTAINER Roman Tarnavski

RUN apk add --update nginx

COPY nginx.conf /etc/nginx/
ADD ./dist/ /usr/share/nginx/html

RUN set -x ; \
  addgroup -g 82 -S www-data ; \
  adduser -u 82 -D -S -G www-data www-data && exit 0 ; exit 1

RUN chown -R www-data:www-data /usr/share/nginx/html/*
RUN chmod -R 0755 /usr/share/nginx/html/*

EXPOSE 8080

CMD nginx -g 'daemon off;'

Now if I rebuild and rerun swagger-ui-builder, the website shows up correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM