28

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?

6
  • did you try contacting amazon support? did you find a solution for this?
    – ramonamis
    Feb 5, 2018 at 15:47
  • No I have no solution and do not have Amazon support. Feb 6, 2018 at 23:48
  • @AntonZherdev Did you find a solution for this matter? I'm in the same situation, and cannot find a suitable solution. Thanks! Nov 13, 2020 at 13:34
  • @StéphanePéchard No. I think it just does not work. Nov 17, 2020 at 4:20
  • 1
    Can you explain why the containers can't run using different ports? If they are part of the same task, on Fargate, they are treated as running on the same machine, or at least the same "localhost" network which is supposed to be super fast. If they need to run together, then different ports seem like the simplest solution. Else they need to be in two tasks and you buy into the extra setup of discovery, etc. Nov 17, 2020 at 18:02

3 Answers 3

8

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.

3
  • 1
    How can it help to run two containers which use the same port in one system? Feb 24, 2018 at 20:44
  • 3
    I have different containers using the same port. I want to start them in one system because they will interact with each other and cannot exist separately. I do not need a load balancer because I will need only one instance of every container. Mar 7, 2018 at 2:00
  • This saved me countless hours. Thanks!
    – azizj
    Oct 22, 2018 at 1:22
5

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).

-3

I had a similar issue in asp.net and was able to resolve it by creating only one container and have webapplications in different folders.

From a docker file my entry point is a powershell script.

FROM mcr.microsoft.com/dotnet/framework/aspnet:4.8-windowsservercore-ltsc2019 AS final
EXPOSE 80

WORKDIR /aspnet-startup
COPY Startup.ps1 .

WORKDIR /inetpub/wwwroot
COPY Web1 ./Web1
COPY Web2 ./Web2

ENTRYPOINT ["powershell.exe", "c:\\aspnet-startup\\Startup.ps1"]

Inside the Startup.ps1, I created an app pool and a web application.

# Create WebApp Pool
New-WebAppPool -Name "Web1"
New-WebAppPool -Name "Web2"

# Create WebApplication
New-WebApplication -Name "Web1" -Site "Default Web Site" -PhysicalPath "C:\inetpub\wwwroot\Web1" -ApplicationPool "Web1"
New-WebApplication -Name "Web2" -Site "Default Web Site" -PhysicalPath "C:\inetpub\wwwroot\Web2" -ApplicationPool "Web2"

C:\ServiceMonitor.exe w3svc
1
  • Your answer is relative to ASP.Net/IIS but the question is about hosting docker containers in AWS ECS Fargate Aug 9 at 12:11

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.