Report this

What is the reason for this report?

Dockerized NGINX with Traefik

Posted on July 29, 2020

Hello, I’m totally new to Linux and web hosting. I’ve been trying to setup a nginx static website on my Ubuntu 20.4 droplet. By following a tutorial, I set up docker, and Traefik using a traefik.yml like this:

api:
  dashboard: true

entryPoints:
  http:
    address: ":80"
  https:
    address: ":443"

providers:
  docker:
    endpoint: "unix:///var/run/docker.sock"
    exposedByDefault: false

certificatesResolvers:
  http:
    acme:
      email: email@email.com
      storage: acme.json
      httpChallenge:
        entryPoint: http

And then docker-compose this file:

version: '3'

services:
  traefik:
    image: traefik:v2.0
    container_name: traefik
    restart: unless-stopped
    security_opt:
      - no-new-privileges:true
    networks:
      - proxy
    ports:
      - 80:80
      - 443:443
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - ./traefik.yml:/traefik.yml:ro
      - ./acme.json:/acme.json
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.traefik.entrypoints=http"
      - "traefik.http.routers.traefik.rule=Host(`monitor.domain.com`)"
      - "traefik.http.middlewares.traefik-auth.basicauth.users=admin:secret_password"
      - "traefik.http.middlewares.traefik-https-redirect.redirectscheme.scheme=https"
      - "traefik.http.routers.traefik.middlewares=traefik-https-redirect"
      - "traefik.http.routers.traefik-secure.entrypoints=https"
      - "traefik.http.routers.traefik-secure.rule=Host(`monitor.domain.com`)"
      - "traefik.http.routers.traefik-secure.middlewares=traefik-auth"
      - "traefik.http.routers.traefik-secure.tls=true"
      - "traefik.http.routers.traefik-secure.tls.certresolver=http"
      - "traefik.http.routers.traefik-secure.service=api@internal"

networks:
  proxy:
    external: true

I successfully set up a portainer:

version: '3'

services:
  portainer:
    image: portainer/portainer:latest
    container_name: portainer
    restart: unless-stopped
    security_opt:
      - no-new-privileges:true
    networks:
      - proxy
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - ./data:/data
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.portainer.entrypoints=http"
      - "traefik.http.routers.portainer.rule=Host(`manage.domain.com`)"
      - "traefik.http.middlewares.portainer-https-redirect.redirectscheme.scheme=https"
      - "traefik.http.routers.portainer.middlewares=portainer-https-redirect"
      - "traefik.http.routers.portainer-secure.entrypoints=https"
      - "traefik.http.routers.portainer-secure.rule=Host(`manage.domain.com`)"
      - "traefik.http.routers.portainer-secure.tls=true"
      - "traefik.http.routers.portainer-secure.tls.certresolver=http"
      - "traefik.http.routers.portainer-secure.service=portainer"
      - "traefik.http.services.portainer.loadbalancer.server.port=9000"
      - "traefik.docker.network=proxy"

networks:
  proxy:
    external: true

So far so good, and everything secured by Let’s Encrypt. Then I tried a bunch of different stuff to deploy a nginx container on a subdomain wiki.domain.com (I did already create that subdomain on my DNS), but not much success. Everything works with a simple command like: $ docker run --name some-nginx -d -p 8888:80 nginx But then I can only access through myip:8888. I’ve tried doing a few different docker-compose similar to the portainer one, but to no success.

version: '3'

services:
  wiki:
    image: nginx:latest
    container_name: wiki
    networks:
      - proxy
    ports:
      - 8888:80
    volumes:
      - "./data:/usr/share/nginx/html:ro"
      - "./nginx.conf:/etc/nginx/nginx.conf:ro"
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.wiki.entrypoints=http"
      - "traefik.http.routers.wiki.rule=Host(`wiki.domain.com`)"
      - "traefik.http.middlewares.wiki-https-redirect.redirectscheme.scheme=https"
      - "traefik.http.routers.wiki.middlewares=wiki-https-redirect"
      - "traefik.http.routers.wiki-secure.entrypoints=https"
      - "traefik.http.routers.wiki-secure.rule=Host(`wiki.domain.com`)"
      - "traefik.http.routers.wiki-secure.tls=true"
      - "traefik.http.routers.wiki-secure.tls.certresolver=http"
      - "traefik.http.routers.wiki-secure.service=wiki"
      - "traefik.http.services.wiki.loadbalancer.server.port=8888"
      - "traefik.docker.network=proxy"
    command: [nginx-debug, '-g', 'daemon off;']

networks:
  proxy:
    external: true

Anyone can tell me how to use trafik and nginx to map my website to wiki.domain.com? After I manage do that, I plan on doing a little wiki-like thingy on angularjs, just for learning.



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Amazingly this question is the top result on Google for “nginx traefik docker” - so I thought I’d post the answer here after fighting with this for far too long…

Final piece of the puzzle found here https://forums.docker.com/t/nginx-php-fpm-traefik-bad-gateway-504/100361/7

Changing the version “3” to “3.7” immediately fixes the nginx bad gate error. You also need to remove the ports, as they override traefik’s config.

version: '3.7'
services:

  #Nginx Service
  webserver:
    image: nginx:alpine
    container_name: webserver
    restart: unless-stopped
    tty: true
    volumes:
      - ./:/var/www
      - ./nginx/conf.d/:/etc/nginx/conf.d/
    networks:
      - app-network
      - traefik
    labels:
      - traefik.http.routers.webserver.rule=Host(`app.domain.tld`)
      - traefik.http.routers.webserver.tls=true
      - traefik.http.routers.webserver.tls.certresolver=lets-encrypt
      - traefik.port=80

#Docker Networks
networks:
  app-network:
    driver: bridge
  traefik:
    external:
      true

From your description, it seems you have Traefik set up correctly for SSL termination and as a reverse proxy for your containers, including Portainer. However, you’re encountering issues while trying to set up an Nginx container to serve content on wiki.domain.com.

Let’s go through your docker-compose.yml for the Nginx container and make a few adjustments:

  1. Remove Ports Mapping for Nginx Service: Since Traefik handles incoming requests and routes them to the correct container, you don’t need to expose Nginx to a host port (like 8888). Traefik will manage the routing internally within the Docker network.

  2. Ensure the Correct Network is Used: It’s good that you’re attaching the Nginx container to the proxy network, which should be the same network Traefik is using to discover services.

  3. Volumes Configuration: Make sure the paths for Nginx HTML content and configuration are correct and the files exist.

Based on these points, here’s the updated docker-compose.yml for your Nginx container:

version: '3'

services:
  wiki:
    image: nginx:latest
    container_name: wiki
    networks:
      - proxy
    volumes:
      - "./data:/usr/share/nginx/html:ro"
      - "./nginx.conf:/etc/nginx/nginx.conf:ro"
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.wiki.entrypoints=http"
      - "traefik.http.routers.wiki.rule=Host(`wiki.domain.com`)"
      - "traefik.http.middlewares.wiki-https-redirect.redirectscheme.scheme=https"
      - "traefik.http.routers.wiki.middlewares=wiki-https-redirect"
      - "traefik.http.routers.wiki-secure.entrypoints=https"
      - "traefik.http.routers.wiki-secure.rule=Host(`wiki.domain.com`)"
      - "traefik.http.routers.wiki-secure.tls=true"
      - "traefik.http.routers.wiki-secure.tls.certresolver=http"
      - "traefik.http.routers.wiki-secure.service=wiki"
      - "traefik.http.services.wiki.loadbalancer.server.port=80"
      - "traefik.docker.network=proxy"

networks:
  proxy:
    external: true

A few notes on this:

  • Network: Ensure that proxy is the same external network that Traefik is using.
  • Port in Service Definition: The load balancer port should match the internal port Nginx is listening on inside the container (usually port 80).
  • Volumes: Check if the paths specified in volumes are correct. For instance, ./data should be the directory where your static HTML files for the wiki are located, and ./nginx.conf should be the path to your custom Nginx configuration file, if you have one. Ensure the Nginx configuration is compatible with your setup.

After making these changes, deploy your Nginx container using docker-compose up -d and check if wiki.domain.com is serving the content as expected.

Remember that DNS propagation may take some time if you’ve recently pointed wiki.domain.com to the server’s IP. Also, make sure your domain registrar or DNS provider has an A record for wiki.domain.com pointing to the server where Traefik is running.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.