简体   繁体   中英

Instantiate multiple docker-compose pipelines with unique IP addresses

I am trying to replicate a docker-compose pipeline. This pipeline orchestrates a bunch of containers and finally maps port 80 on one of the containers to port 8000 on the host for external communication.

How can I create multiple instances of the same docker-compose pipeline such that each instance gets its own IP address that I can use on some other machine to access the corresponding instance of the pipeline over port 8000 ?

Here is a visual representation of what I am trying to achieve.

电子问题图

Note that both the instances use the same docker-compose.yml and expose the same port. However the distinction is in them being assigned unique IP addresses. Can I instruct a docker-compose pipeline to launch on a specific network only? or always create a new network before deployment such that each instance gets a new ip address and by virtue of that abstraction port collision does not occur.

Also, is this a good approach? If not, what is the best strategy to access multiple instances of the same docker-compose pipeline in a dynamic manner such that I don't have to manually modify each docker-compose.yml file to map an unused port?

How about:

cat <<EOF
version: "3"
  services:
    web1:
      image: python:2.7-alpine
      command: ["python", "-m", "SimpleHTTPServer", "8080"]
    web2:
      image: python:2.7-alpine
      command: ["python", "-m", "SimpleHTTPServer", "8080"]
EOF

Then get the virtual addresses via eg:

docker ps | grep compose_ | cut -f1 -d' ' | while read s; do docker inspect $s | egrep "IPAddr.*[0-9]"; done
                "IPAddress": "172.28.0.3",
                "IPAddress": "172.28.0.2",

Then confirm that both addresses respond on the same port:

$ curl -sv 172.28.0.2:8080
...
* Connected to 172.28.0.3 (172.28.0.3) port 8080 (#0)
...
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html>
...

$ curl -s 172.28.0.3:8080
...

You can use custom networks to isolate the containers in specific ways, but even with default behavior, you can create multiple instances of this compose stack, and you will always get unique virtual addresses you can selectively hit from your client. You can put a reverse proxy in front if you want to route external client traffic to a particular backend.

You can have multiple instances by overriding the project name for each instance with $COMPOSE_PROJECT_NAME or -peg:

docker-compose -p mytest1 up -d
docker-compose -p mytest2 up -d

Hope that helps.

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