Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access container name in Nginx configuration file

In Docker, I created a new network with docker network create usta_network command and referenced this network in service declaration in docker-compose.yml file.

It is okay, I'm able to ping services with container's name right now, but how can I reference a container's name inside Nginx Upstream declaretion? Which is best or better practice to bring access between containers ?

docker.compose for nginx

version: '2'

services:
   nginx-main:
      image: nginx:latest
      ports:
         - "80:80"
      volumes:
         - ./nginx.conf:/etc/nginx/nginx.conf:ro
         - /var/log/nginx:/var/log/nginx
      container_name: nginx_main
networks:
   default:
      external:
         name: usta_network
./nginx.conf

user nginx;

http {

    server {
       listen 80;
       index index.php index.html index.htm;
       server_name example.org www.example.org;

       location / {

         proxy_set_header Host               $host;
         proxy_set_header X-Real-IP          $remote_addr;
         proxy_set_header X-Forwarded-For    $proxy_add_x_forwarded_for;
         proxy_pass http://rahmanusta_upstream;
       }
    }

    upstream rahmanusta_upstream {

      server rahmanusta_wp:6565;

    }
}

docker-compose for mysql + wordpress

version: '2'

services:
   mysql-db:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: pass
       MYSQL_DATABASE: mysql_db
       MYSQL_USER: root
       MYSQL_PASSWORD: pass
     container_name: mysql_db

   rahmanusta-wp:
     depends_on:
       - mysql-db
     image: wordpress:latest
     ports:
       - "6565:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_PASSWORD: pass
     container_name: rahmanusta_wp
volumes:
    db_data:
networks:
   default:
      external:
         name: usta_network

docker network inspect usta_network

[
    {
        "Name": "usta_network",
        "Id": "b971429c1ddcfa791bdfff0a6f9463ab9cfb9ae04ba6a3aa60ce08ca11d0a5ab",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.20.0.0/16",
                    "Gateway": "172.20.0.1/16"
                }
            ]
        },
        "Internal": false,
        "Containers": {
            "16cc89ca548fe90fb8ed9431d8e9633c341373344304ec3320acee1a81090709": {
                "Name": "rahmanusta_wp",
                "EndpointID": "d310cbf9617958226d36d5f9e0d804c113093097669b1d5ae8bed90d31dfca2f",
                "MacAddress": "02:42:ac:14:00:03",
                "IPv4Address": "172.20.0.3/16",
                "IPv6Address": ""
            },
            "5b0f7a7d95b23b6e69b17f85ebff5d652bdbcba305755169c6b9f78199fbf346": {
                "Name": "mysql_db",
                "EndpointID": "d2c304b450ce96d309ef51319aa42336bb742d99db095e98e129e547aee820c3",
                "MacAddress": "02:42:ac:14:00:02",
                "IPv4Address": "172.20.0.2/16",
                "IPv6Address": ""
            },
            "addf3c475fcfd5566b3d1c4b4a951f78978c07265211746f79711f38c5cd9649": {
                "Name": "nginx_main",
                "EndpointID": "3f4106184dbe805c509011c3dc42c673c85fcf2eb08441a5ecf24b9c5c68e2d4",
                "MacAddress": "02:42:ac:14:00:04",
                "IPv4Address": "172.20.0.4/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]

Thanks.

like image 503
Rahman Usta Avatar asked Sep 15 '25 05:09

Rahman Usta


1 Answers

In your docker-compose.yml, you'll name each of the services with their relevant name. Let's say I've created a jenkins instance behind my nginx docker container named jenkins1.

In my http section of the nginx.conf file I name the upstream server:

upstream jenkins {
  server jenkins1:8080;
}

In the server block then I can call this as below;

location / {
      proxy_pass https://jenkins;
    }

This will direct all traffic that hits your nginx container port to port 8080 on the jenkins1 service container.

EDIT Based on updated question

In your docker-compose.yml, use external links to link the two containers from outside the .yml file. Your nginx service section should read;

services:
   nginx-main:
      image: nginx:latest
      ports:
         - "80:80"
      volumes:
         - ./nginx.conf:/etc/nginx/nginx.conf:ro
         - /var/log/nginx:/var/log/nginx
      external_links:
         - rahmanusta_wp
         - mysql_db
      container_name: nginx_main
like image 123
Domhnaill Byrne Avatar answered Sep 17 '25 18:09

Domhnaill Byrne