简体   繁体   中英

docker-compose scale with nginx and without environment variable

I use docker-compose to describe the deployment of one of my application. The application is composed of a

  • mongodb database,
  • a nodejs application
  • a nginx front end the static file of nodejs.

If i scale the nodejs application, i would like nginx autoscale to the three application.

Recently i use the following code snippet :

https://gist.github.com/cmoore4/4659db35ec9432a70bca

This is based on the fact that some environment variable are created on link, and change when new server are present.

But now with the version 2 of the docker-compse file and the new link system of docker, the environment variable doesn't exist anymore.

How my nginx can now detect the scaling of my application ?

version: '2'
services:
    nodejs:
        build:
          context: ./
          dockerfile: Dockerfile.nodejs
        image: docker.shadoware.org/passprotect-server:1.0.0
        expose:
            - 3000
        links:
            - mongodb
        environment:
            - MONGODB_HOST=mongodb://mongodb:27017/passprotect
            - NODE_ENV=production
            - DEBUG=App:*
    nginx:
        image: docker.shadoware.org/nginx:1.2
        links:
            - nodejs
        environment:
            - APPLICATION_HOST=nodejs
            - APPLICATION_PORT=3000
    mongodb:
        image: docker.shadoware.org/database/mongodb:3.2.7

Documentation states here that:

Containers for the linked service will be reachable at a hostname identical to the alias, or the service name if no alias was specified.

So I believe that you could just set your services names in that nginx conf file like:

upstream myservice {
    yourservice1;
    yourservice2;
}

as they would be exported as host entries in /etc/hosts for each container.

But if you really want to have that host:port information as environment variables you could write a script to parse that docker-compose.yml and define an .env file , or doing it manually.

UPDATE:

You can get that port information from outside the container, this will return you the ports

docker inspect --format='{{range $p, $conf := .NetworkSettings.Ports}} {{$p}} -> {{(index $conf 0).HostPort}} {{end}}' your_container_id

But if you want to do it from the inside of a containers then what you want is a service discovery system like zookeeper

There's a long feature request thread in docker's repo , about that.

One workaround solution caught my attention. You could try building your own nginx image based on that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM