简体   繁体   中英

docker-compose: how to map few ip to few containers

I have one host machine which have two ip addresses, also I have two nginx containers. How to map first ip to first nginx, and second ip to second nginx?

I'll assume that by IP you actually mean a socket, eg your nginx1 container will listen to the IP address #1 (let's say 1.2.3.1 ) on port 8080 and your nginx2 container will listen to your IP address #2 (let's say 1.2.3.2 ) on port 8081.

The option --publish will fulfill your need. It is used with docker run as so:

docker run -d --publish=1.2.3.1:8080:80 -name nginx1 nginx

This command will start the image nginx as a container named nginx1 in background mode and the socket 1.2.3.1:8080 will be bound to the port 80 of the container. As for nginx2 : docker run -d --publish=1.2.3.2:8081:80 -name nginx2 nginx .


To combine it with docker compose you'll just need to add these options in the .yml file and you're done!

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