如何设置端口以将静态网站作为Nginx Docker容器运行?

use*_*695 3 nginx gitlab docker dockerfile

我正在尝试使用docker和nginx运行静态网站。我在我的ubuntu机器上使用jwilder / nginx-proxy作为反向代理。但是致电https://www.example.com会返回502错误。我认为正确设置端口是一个问题,但是我看不到我的错误。

这是我的Dockerfile ...

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

EXPOSE 3499
Run Code Online (Sandbox Code Playgroud)

...这就是我配置gitlab ci的方式:

build:
  stage: build
  image: docker:stable
  script:
    - docker build -t ${DOCKER_CONTAINER}:latest .

production:
  stage: release
  image: docker:stable
  variables:
    GIT_STRATEGY: none
  script:
    - docker run
      --name ${DOCKER_CONTAINER}
      --detach
      --restart=always
      -e VIRTUAL_HOST=www.example.com
      -e LETSENCRYPT_HOST=www.example.com
      -p 3499
      ${DOCKER_CONTAINER}:latest
  environment:
    name: production
    url: https://www.example.com
Run Code Online (Sandbox Code Playgroud)

Tar*_*ani 9

The problem is that are assuming just using EXPOSE 3499 changes the port on your nginx config. The nginx is still running on port 80 within the container.

EXPOSE is to say your intentions, that the image does intend to expose 3499. But the server inside needs to be configured to make sure the port it listens on is the same.

Since you are using the official docker nginx image, you can read it's documentation below

https://hub.docker.com/_/nginx

Using environment variables in nginx configuration

Out-of-the-box, nginx doesn't support environment variables inside most configuration blocks. But envsubst may be used as a workaround if you need to generate your nginx configuration dynamically before nginx starts.

Here is an example using docker-compose.yml:

web:
  image: nginx
  volumes:
   - ./mysite.template:/etc/nginx/conf.d/mysite.template
  ports:
   - "8080:80"
  environment:
   - NGINX_HOST=foobar.com
   - NGINX_PORT=80
  command: /bin/bash -c "envsubst < /etc/nginx/conf.d/mysite.template > /etc/nginx/conf.d/default.conf && exec nginx -g 'daemon off;'"
Run Code Online (Sandbox Code Playgroud)

The mysite.template file may then contain variable references like this:

listen ${NGINX_PORT};
Run Code Online (Sandbox Code Playgroud)

So as you see, you need to do some work in the image to make sure your nginx actually listens on 3499

Update: 23rd July 2019

In case you don't want to do it using environment variable. Then you can overwrite the file using docker-compose

So you will keep a local file default.conf where you will change the contents

server {
    listen       3499;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
}
Run Code Online (Sandbox Code Playgroud)

And mount this file in your docker-compose.yml

web:
  image: nginx
  volumes:
   - ./default.conf:/etc/nginx/conf.d/default.conf
  ports:
   - "8080:3499"
Run Code Online (Sandbox Code Playgroud)

And that will make sure that the file is correct. You can even copy that in the Dockerfile, if you don't want to override at run-time