skip to Main Content

Platform info:

- OS: Windows 11 Pro
- OS Edition: 22H2
- OS Version: 22621.674
- Running WSL Distro: Ubuntu 22.04.2 LTS
- Docker Desktop version: 4.22.1 (118664)
- Docker Engine version: v24.0.5

I’ve launched some services through docker compose -f docker-compose.yml up, and I’m able to connect to them through localhost:EXPOSED_PORT on both Ubuntu and my Windows host, so far so good.

The thing is, I need to be able to access each docker-compose service through its own host assigned on nginx and etc/hosts. This is not working, I get an ERR_CONNECTION_CLOSED error.

Here is my docker-compose file:

// docker-compose.yml

version: "3.8"
services:
  moonshine2_webserver_local:
    build:
      context: ${SRC_CODE_FOLDER}
      dockerfile: ${PWD}/back/Dockerfile
    container_name: moonshine2.webserver.local
    domainname: moonshine2.local
    image: moonshine2.webserver.local.image
    restart: always
    ports:
      - "3333:3333"
      - "5850:5850"
    depends_on:
      - moonshine2_mongo_local
      - moonshine2_mongors_initializer
    networks:
      moonshine_docker_local:
        ipv4_address: 10.10.0.3
    links:
      - moonshine2_mongo_local
      - moonshine2_mongo_rs1_local
      - moonshine2_mongo_rs2_local
      - moonshine2_mongo_rs3_local
      - moonshine2_sftp_local:sgfm.moonshine2.local
      - moonshine2_cdn_local
    command: npm run debug_vscode_docker --site=manager --services=manager_dev --unsafe-perm true
    volumes:
      - ${SRC_CODE_FOLDER}:/home/node/app

  moonshine2_webserver_local_front:
    build:
      context: ${SRC_CODE_FOLDER}
      dockerfile: ${PWD}/front/Dockerfile
    container_name: moonshine2.frontend.local
    domainname: moonshine2.local.front
    ports:
      - "3000:3000"
    networks:
      moonshine_docker_local:
        ipv4_address: 10.10.0.7
    links:
      - moonshine2_webserver_local
    command: npm start --unsafe-perm true
    volumes:
      - ${SRC_CODE_FOLDER}:/home/node/app

  moonshine2_mongo_local:
    image: mongo:4.2
    container_name: moonshine2.mongo.local
    domainname: moonshine2.mongo.local
    restart: always
    ports:
      - "27017:27017"
    networks:
      moonshine_docker_local:
        ipv4_address: 10.10.0.2
    volumes:
      - "${VOLUMES_FOLDER}/local_mongo/data/db:/data/db"
    command:
      # Define default command (remove logging driver none to see logs).
      # profile 0 (default): no query logged
      - mongod
      # profile 1: log slow queries
      #- mongod --profile 1 --slowms 2
      # profile 2: log all queries
      #- mongod --profile 2
    logging:
      driver: none
      
  moonshine2_mongors_initializer:
    image: mongo:4.2
    container_name: moonshine2.mongors.initializer
    domainname: moonshine2.mongors.initializer
    restart: always
    networks:
      moonshine_docker_local:
        ipv4_address: 10.10.0.20
    volumes:
      - "${VOLUMES_FOLDER}/local_mongors_initializer/data:/data"
    depends_on:
      - moonshine2_mongo_rs1_local
      - moonshine2_mongo_rs2_local
      - moonshine2_mongo_rs3_local
    entrypoint: bash /data/init_rs.sh
    logging:
      driver: none

  moonshine2_mongo_rs1_local:
    image: mongo:4.2
    container_name: moonshine2.mongors1.local
    domainname: moonshine2.mongors1.local
    restart: always
    ports:
      - "30001:27017"
    networks:
      moonshine_docker_local:
        ipv4_address: 10.10.0.21
    volumes:
      - "${VOLUMES_FOLDER}/local_mongo_rs1/data/db:/data/db"
    command: mongod --replSet msrs
    logging:
      driver: none

  moonshine2_mongo_rs2_local:
    image: mongo:4.2
    container_name: moonshine2.mongors2.local
    domainname: moonshine2.mongors2.local
    restart: always
    ports:
      - "30002:27017"
    networks:
      moonshine_docker_local:
        ipv4_address: 10.10.0.22
    volumes:
      - "${VOLUMES_FOLDER}/local_mongo_rs2/data/db:/data/db"
    command: mongod --replSet msrs
    logging:
      driver: none

  moonshine2_mongo_rs3_local:
    image: mongo:4.2
    container_name: moonshine2.mongors3.local
    domainname: moonshine2.mongors3.local
    restart: always
    ports:
      - "30003:27017"
    networks:
      moonshine_docker_local:
        ipv4_address: 10.10.0.23
    volumes:
      - "${VOLUMES_FOLDER}/local_mongo_rs3/data/db:/data/db"
    command: mongod --replSet msrs
    logging:
      driver: none

  moonshine2_nginx_local:
    image: nginx:1.24.0
    container_name: moonshine2.nginx.local
    restart: always
    links:
      - moonshine2_webserver_local
      - moonshine2_mongo_local
      - moonshine2_mongors_initializer
      - moonshine2_mongo_rs1_local
      - moonshine2_mongo_rs2_local
      - moonshine2_mongo_rs3_local
    volumes:
      - ${VOLUMES_FOLDER}/local_nginx:/etc/nginx/conf.d
    ports:
      - "8081:80"
    networks:
      moonshine_docker_local:
        ipv4_address: 10.10.0.4
    environment:
      - NGINX_HOST=moonshine2.local
      - NGINX_PORT=80
    command: /bin/bash -c "envsubst < /etc/nginx/conf.d/moonshine2.local > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"

  moonshine2_cdn_local:
    image: nginx:1.24.0
    container_name: moonshine2.cdn.local
    restart: always
    volumes:
      - ${VOLUMES_FOLDER}/cdn:/etc/nginx/conf.d
      - ${VOLUMES_FOLDER}/sftp:/www/data/statics/uat/manager/contents/images/uploads
      - ${VOLUMES_FOLDER}/sftp:/www/data/statics/uat/manager/contents/documents/uploads
    ports:
      - "8181:81"
    networks:
      moonshine_docker_local:
        ipv4_address: 10.10.0.6
    environment:
      - NGINX_HOST= moonshine2.cdn.local
      - NGINX_PORT=81
    command: /bin/bash -c "envsubst < /etc/nginx/conf.d/moonshine2.cdn.local > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"
    logging:
      driver: none

  moonshine2_sftp_local:
    image: atmoz/sftp
    container_name: moonshine2.sftp.local
    restart: always
    volumes:
      - ${VOLUMES_FOLDER}/sftp:/home/nodejs/public/manager/contents/images/uploads
      - ${VOLUMES_FOLDER}/sftp:/home/nodejs/public/manager/contents/documents/uploads
    ports:
      - "2222:22"
    networks:
      moonshine_docker_local:
        ipv4_address: 10.10.0.5
    command: nodejs:nodejs:1000
    logging:
      driver: none

networks:
  moonshine_docker_local:
    driver: bridge
    ipam:
      config:
        - subnet: 10.10.0.0/25
          gateway: 10.10.0.1

Here is my nginx conf file:

upstream moonshine2.local {
    server 10.10.0.3:3333;
}

server {

    gzip_types text/plain text/css application/json application/x-javascript
               text/xml application/xml application/xml+rss text/javascript;

    server_name moonshine2.local;
    client_max_body_size 100M;

    location / {
        proxy_pass http://moonshine2.local;
        #include /etc/nginx/proxy_params;
    }    
}

upstream moonshine2.frontend.local {
    server 10.10.0.7:3001;
}
server {
    listen 443 ssl;
    ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers         AES128-SHA:AES256-SHA:RC4-SHA:DES-CBC3-SHA:RC4-MD5;
    ssl_certificate /etc/nginx/conf.d/certs/moonshineUAT.pem;
    ssl_certificate_key /etc/nginx/conf.d/certs/moonshineUAT.key;
    ssl on; 
    gzip_types text/plain text/css application/json application/x-javascript
               text/xml application/xml application/xml+rss text/javascript;
    server_name moonshine2.frontend.local;
    location / {
        proxy_pass http://moonshine2.frontend.local;
        #include /etc/nginx/proxy_params;
    }    
}

upstream moonshine2.mongo.local {
    server 10.10.0.2:27017;
}

server {

    gzip_types text/plain text/css application/json application/x-javascript
               text/xml application/xml application/xml+rss text/javascript;

    server_name moonshine2.mongo.local;

    location / {
        proxy_pass http://moonshine2.mongo.local;
        #include /etc/nginx/proxy_params;
    }
}

upstream moonshine2.mongors.initializer {
    server 10.10.0.20;
}

server {

    gzip_types text/plain text/css application/json application/x-javascript
               text/xml application/xml application/xml+rss text/javascript;

    server_name moonshine2.mongors.initializer;

    location / {
        proxy_pass http://moonshine2.mongors.initializer;
        #include /etc/nginx/proxy_params;
    }
}

upstream moonshine2.mongors1.local {
    server 10.10.0.21:27017;
}

server {
 
    gzip_types text/plain text/css application/json application/x-javascript
               text/xml application/xml application/xml+rss text/javascript;

    server_name moonshine2.mongors1.local;

    location / {
        proxy_pass http://moonshine2.mongors1.local;
        #include /etc/nginx/proxy_params;
    }
}

upstream moonshine2.mongors2.local {
    server 10.10.0.22:27017;
}

server {

    gzip_types text/plain text/css application/json application/x-javascript
               text/xml application/xml application/xml+rss text/javascript;

    server_name moonshine2.mongors2.local;

    location / {
        proxy_pass http://moonshine2.mongors2.local;
        #include /etc/nginx/proxy_params;
    }
}

upstream moonshine2.mongors3.local {
    server 10.10.0.23:27017;
}

server {

    gzip_types text/plain text/css application/json application/x-javascript
               text/xml application/xml application/xml+rss text/javascript;

    server_name moonshine2.mongors3.local;

    location / {
        proxy_pass http://moonshine2.mongors3.local;
        #include /etc/nginx/proxy_params;
    }
}

My etc/hosts file (these entries are present on both Ubuntu and Windows’ etc/hosts files):

10.0.199.205    host.docker.internal
10.0.199.205    gateway.docker.internal

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

# Moonshine2 docker local environment
10.10.0.4 moonshine2.local moonshine2.contents.local moonshine2.frontend.local www.moonshine2.local www.moonshine2.contents.local www.moonshine2.frontend.local
10.10.0.2 moonshine2.mongo.local manager-mongo
10.10.0.5 moonshine2.sftp.local
10.10.1.4 moonshine2.dev moonshine2.mongo.dev moonshine2.contents.dev
10.10.0.6 sgfm.moonshine2.local

# Moonshine2 mongo replica set
10.10.0.20 moonshine2.mongors.initializer
10.10.0.21 moonshine2.mongors1.local
10.10.0.22 moonshine2.mongors2.local
10.10.0.23 moonshine2.mongors3.local

As an additional note, I can’t even hit the nginx IP addresses from curl on Ubuntu, but I am able to inside one of the Docker containers (since they have access to the docker-compose network). The same applies when using curl on the IP address’ host.

curl to the nginx IP address from Ubuntu: Returns curl: (52) Empty reply from server

Why am I not able to hit the Nginx hosts, not even from Ubuntu?

What I’ve tried

I’ve changed the network driver from bridge to host to no avail.

// docker-compose.yml (see above for full file)

networks:
  moonshine_docker_local:
    driver: bridge
    ipam:
      config:
        - subnet: 10.10.0.0/25
          gateway: 10.10.0.1

I’ve tried setting the IP in the ports property of the service to no avail.

// docker-compose.yml (see above for full file)

  moonshine2_nginx_local:
    image: nginx:1.24.0
    container_name: moonshine2.nginx.local
    restart: always
    links:
      - moonshine2_webserver_local
      - moonshine2_mongo_local
      - moonshine2_mongors_initializer
      - moonshine2_mongo_rs1_local
      - moonshine2_mongo_rs2_local
      - moonshine2_mongo_rs3_local
    volumes:
      - ${VOLUMES_FOLDER}/local_nginx:/etc/nginx/conf.d
    ports:
      - "10.10.0.4:8081:80"

2

Answers


  1. It’s a really long question, but if I understand correctly you have an issue with Nginx itself and not the other services.

    First of all, if you intend to use DNS entries for everything, I don’t see any use to the static IPs you have configured for each service. Also, as you are running all of your services in a single Docker-compose file, I would suggest to let Docker engine do the networking for you and drop that network entirely. Docker will automatically do that for you.

    For the external communication with Nginx, the IP you have configured is an internal Docker IP address, not reachable from the outside world. You have configured port 8081 as the entry to Nginx:

    ports:
          - "8081:80"
    

    but that would only be accessible through the IP of the host (I guess the Ubuntu VM).

    For the communication with all of the internal service, you can proxy or stream to the name of the service. For example:

    proxy_pass http://moonshine2_nginx_local;
    

    instead of

    proxy_pass http://moonshine2.nginx.local;
    

    Also, for the frontend service, your Nginx is listening on port 443, but you haven’t exposed that port, so it could only be accessible from the internal Docker network.

    On the Windows host, you only need to be able to reach the Ubuntu host. All of your DNS entries should point to that IP address. Even better, you can change the DNS entries to a format of "${service_name}.moonshine2" and just add an entry for *.moonshine2 to your /etc/hosts.

    Login or Signup to reply.
  2. You don’t need to specify ip for your containers because when you define docker compose file, your services will be in a same network. You can work with the name of the services instead. The only solution for getting simple your project.
    Hope it helps!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search