Search code examples
amazon-web-servicesdockeraws-fargateamazon-ecr

How to make a Docker image run in Fargate


I have a Dockerised node server that I can create locally and when I press 'play' via the Docker desktop app it will begin showing on my localhost browser.

I am trying to get that same Dockerised node server to work on Fargate. Firstly I've pushed to an AWS ECR repo, started up Fargate and added clusters, services and tasks.

My question is how do I get Fargate to do the equivalent of 'play' the Docker image so it will start up and start serving from the Fargate server?


Solution

  • What I think you're looking for are "tasks", which require you to create a task definition and then go to the "Task" tab of your ECS Cluster and click "Run New Task"

    The entirety of the steps are:

    1. Create ECR Repo and push your image into it (optional, the image could be in a publicly available repository elsewhere)
    2. Create an ECS Cluster
    3. Create an IAM Task Execution Role (Maybe optional but recommended, I think you only need this if you pull from ECR or want to write container STDOUT to cloudwatch logs). I believe this is created automatically when you create a task definition in the console.
    4. Create an IAM Task Role if your container needs AWS permissions (optional).
    5. Create a ECS Task Definition that describes your container specification, including what the URI for the image is: AWS ECR, Docker Hub, Quay.io, etc. Also including environment variables and the CPU/memory required (these two values are linked and certain combinations may not be allowed, such as 512M of memory and 4 cores).

    Then you can "Just Push Play" by clicking on the "Run New Task" button on the "Tasks" tab of your ECS Cluster. You'll have to configure a few run-time parameters, but then it will just run until the process exits or the task is deleted.

    Additionally, Cloudwatch Events can trigger these tasks on a schedule or in response to certain events, and it's a one-liner from the CLI to trigger this task.

    If you want a container or set of containers that are always running (such as a web site that always need to be serving visitors), you can use an ECS Service instead of a task, and then you can take advantage of auto-scaling and replacing failed containers.