'AWS ECS Fargate and port mapping
I have two containers and they expose the same port. I want to run them in the same task as they are part of the same system. But I cannot do this with Fargate because there are no port mapping and the host port should be the same as container port for the awsvpc network mode (only supported by Fargate).
It's an essential feature of Docker and it's strange that it seems to be not supported by Fargate. Is there really no way to do this or I'm missing something?
Solution 1:[1]
Use application load balancer to your service and set your custom port in target group and host port should be set the same as container port. This is our tested solution.
Solution 2:[2]
The simplest way to solve this problem is to make the port of your Docker container configurable and then pass it to the container as an environment variable. Example:
Dockerfile
FROM python:3.10-slim-bullseye
ENV PORT 5000
# Do all your container setup
# ...
EXPOSE $PORT
ENTRYPOINT path/to/entrypoint.sh
In entrypoint.sh
, you need to set the app itself to use the port provided in the PORT
environment var.
Then in your task definition, set the port mapping to a different port per container and provide the port as an env var:
{
// ...
"portMappings": [
{
"hostPort": 5001,
"protocol": "tcp",
"containerPort": 5001
}
],
// ...
"environment": [
{
"name": "PORT",
"value": "5001"
},
]
}
If you don't override the port via an environment variable, it will use the default port declared in the Dockerfile (in this case, 5000).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Danny G |
Solution 2 |