Connecting Django container to MySQL container in a single AWS ECS Fargate task definition

27 Views Asked by At

I have a repository here. I am currently struggling with deploying it to AWS ECS. The repository is configured to use Github Actions to build the two containers and then ship them to ECR, then update an ECS task definition to pull the most recent image commit.

I am using ECS Fargate, and am therefore on awsvpc for the network mode.

I tried changing the hostname to localhost. When I do so, I get this error: MySQLdb.OperationalError: (2002, "Can't connect to local server through socket '/run/mysqld/mysqld.sock' (2)")

According to comments so far, setting it to localhost will work. However, I still get this error. I have double checked that my settings.py and .env file have localhost as the DB_HOST.

What I've tried is visible at the repository here

1

There are 1 best solutions below

4
Mark B On

From what I understand, the name of the DB_HOST in the settings.py should be the name of the container when they are in the same task definition.

No that is incorrect. When you are using awsvpc network mode for your containers, which is the only option on Fargate, then the DB_HOST setting should be localhost or 127.0.0.1.