Reputation: 295
I have an application , say an api , running as docker container on ec2. As this container has dependency on the ec2 host ( port mapping) , i am unable to run more than one instance in a same machine.
I want to achieve a state where i have 5 hosts which are fronted with an elb and i should be able to run more than 1 instance of the container in all these hosts.
I am aware that i can achieve this state using ECS.
Can i do this with Ec2 , ELB and Docker?
-Thanks
Upvotes: 4
Views: 1595
Reputation: 146630
There are multiple ways to do it
Use different port for each docker container
docker run -d -p 81:80 yourimage
docker run -d -p 82:80 yourimage
docker run -d -p 83:80 yourimage
docker run -d -p 84:80 yourimage
docker run -d -p 85:80 yourimage
And then you can put a nginx load balancer on port 80. See more details on http://nginx.org/en/docs/http/load_balancing.html
You need to use a config like below
http {
upstream myapp1 {
server 127.0.0.1:81;
server 127.0.0.1:82;
server 127.0.0.1:83;
server 127.0.0.1:84;
server 127.0.0.1:85;
}
server {
listen 80;
location / {
proxy_pass http://myapp1;
}
}
}
Use single node docker swarm manager
You can setup a single node docker swarm using below command
docker swarm init
And then on that machine run the below command
docker service create --name=myapp --replicase=5 -p 80:80 yourimage
This will create 5 load balanced container and publish them on port 80.
No ELB needed in both cases. And I doubt ELB works on same host + multi port scenario
Upvotes: 4