27

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?

Anton Zherdev
  • 674
  • 1
  • 8
  • 10
  • did you try contacting amazon support? did you find a solution for this? – ramonamis Feb 05 '18 at 15:47
  • No I have no solution and do not have Amazon support. – Anton Zherdev Feb 06 '18 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! – Stéphane Péchard Nov 13 '20 at 13:34
  • @StéphanePéchard No. I think it just does not work. – Anton Zherdev Nov 17 '20 at 04: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. – Brendon Whateley Nov 17 '20 at 18:02
  • @BrendonWhateley Sorry, I do not remember the exact problem I was solving since it was more than 2 years ago. However, the reason to run them as one task is that they were small and closely related to each other. The configuration of the containers was done in the way that they utilized the same port and it was not easy to change. I just wanted to change the port for one of the containers. Docker has a simple port mapping for that, Fargate does not unless you use a load balancer which is not needed if you plan to run just one instance. – Anton Zherdev Nov 20 '20 at 06:41

4 Answers4

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.

Danny G
  • 3,660
  • 4
  • 38
  • 50
Sunil Shakya
  • 8,097
  • 2
  • 17
  • 20
  • 1
    How can it help to run two containers which use the same port in one system? – Anton Zherdev Feb 24 '18 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. – Anton Zherdev Mar 07 '18 at 02:00
  • This saved me countless hours. Thanks! – azizj Oct 22 '18 at 01: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).

Nick K9
  • 3,885
  • 1
  • 29
  • 62
-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
-7

You will have to switch to ec2 based ecs instead of fargate. Also you can run on different port and use service discovery feature in fargate to communicate each other. Might need code changes.

rashidcmb
  • 727
  • 3
  • 8
  • 21