Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set port to run static website as nginx docker container?

I'm trying to run a static website using docker and nginx. I'm using jwilder/nginx-proxy as a reverse proxy on my ubuntu machine. But calling https://www.example.com returns me a 502 error. I think it is a problem with setting the port correctly, but I do not see my mistake.

This is my Dockerfile...

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

EXPOSE 3499

...and this is how I configured my 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
like image 484
user3142695 Avatar asked Dec 14 '22 11:12

user3142695


1 Answers

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;'"

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

listen ${NGINX_PORT};

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;
    }
}

And mount this file in your docker-compose.yml

web:
  image: nginx
  volumes:
   - ./default.conf:/etc/nginx/conf.d/default.conf
  ports:
   - "8080:3499"

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

like image 189
Tarun Lalwani Avatar answered Dec 15 '22 23:12

Tarun Lalwani