Adding Local Addresses in Nginx Proxy Manager Using Docker Container Domains in Docker Compose

Tiempo de lectura: 2 minutos

Reading time: 2 minutes

When implementing Nginx Proxy Manager in Docker or Docker Compose, it is important to ensure that it accesses the implemented containers/services using a local address to avoid going through the public network if the connection is not SSL internally. This way, we prevent unencrypted traffic from going outside.

The first thing we need to do is to connect the Docker containers in the Docker Compose file by adding links:

version: "3.1"

services:
  miservicio_mysql:
    image: biarms/mysql:5.7
    env_file:
       - ./Dockerfile/mysql.env
    container_name: mysql_container
    command: --max_allowed_packet=67108864
    environment:
        MYSQL_DATABASE: "db"
        MYSQL_USER: "user"
        MYSQL_PASSWORD: "pass_user"
          # Password for root access
        MYSQL_ROOT_PASSWORD: "pass_root"
    volumes:
      # Mount a volume for MySQL to preserve database data
      - ./mysql:/var/lib/mysql
    expose:
      - 3306
    ports:
      - 3306:3306
    networks:
      - docker-network
  miservicio_php:
    build: 
      context: ./Dockerfile
      dockerfile: php_file
    env_file:
       - ./Dockerfile/php.env
    container_name: php_container
    volumes:
      - ./www/:/var/www/html
      - ./logs/apache:/var/log/apache2
    expose:
      - 80
      - 443
    ports:
      - 8000:80
      - 442:443
    links: 
      - miservicio_mysql
    networks:
      - docker-network
  adminer:
    image: adminer
    restart: always
    ports:
      - 8080:8080
    links: 
      - miservicio_mysql:db
    networks:
      - docker-network
  nginxproxymanager:
    image: 'jc21/nginx-proxy-manager:latest'
    restart: unless-stopped
    ports:
      # These ports are in the format :
      - '80:80' # Public HTTP Port
      - '443:443' # Public HTTPS Port
      - '81:81' # Admin Web Port
    # Default user:
    # Email: admin@example.com
    # Password: changeme

    volumes:
      - ./config/proxymanager/data:/data
      - ./config/letsencrypt:/etc/letsencrypt
    networks:
      - docker-network
    links: 
      - miservicio_mysql
      - miservicio_php
      - adminer

networks:
  docker-network:
    driver: bridge

As you can see in the example, I have added links in the nginxproxymanager container:

 - miservicio_mysql
 - miservicio_php
 - adminer

This allows nginxproxymanager to access these containers locally.

Additionally, I have connected them through a Docker network named docker-network.

Now, in Nginx Proxy Manager, log in and go to the proxies:

Now, add or edit one and instead of using the IP, use the name of the container (either the one specified in the root or within the container_name tag):

And most importantly, remember to use the original service port in your Docker, not the exposed one. In other words, use the port on the right side within the ports section of your Docker Compose containers:

 ports:
      - 8000:80
      - 442:443

Leave a Comment