Assumptions and what I want to achieve
I want to run a simple LAMP application on ECS Fargate.
Problems and error messages I'm experiencing
I can't access the Mariadb container from the PHP (Laravel) container.
(Same error with php artisan migrate on ECS Exec)
In this article, it seems to be possible to communicate with localhost in the same task, but I am having trouble accessing it.
Linking Container in AWS Fargate error
SQLSTATE[HY000] [2002] No such file or directory (SQL: ...
Infrastructure definitions.
Both Laravel and MariaDB containers are managed by ECR with a built Dockerfile.
*Currently, there are a lot of things to tweak, but I'm aiming to get it working first, so some of them are superfluous.
Laravel-Dockerfile
FROM php:7.4.24-apache
#install all the system dependencies and enable PHP modules
RUN apt-get update \
&& apt-get install -y zlib1g-dev \
&& apt-get install -y zip unzip \
&& apt-get -y install libzip-dev libonig-dev \
&& docker-php-ext-install pdo_mysql mysqli zip \
&& docker-php-ext-enable mysqli \
&& a2enmod rewrite
ENV TZ=Asia/Tokyo
RUN apt-get update && apt-get install -y \
busybox-static \
&& apt-get clean
#install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin/ --filename=composer
ENV COMPOSER_ALLOW_SUPERUSER 1
ENV COMPOSER_HOME /composer
ENV PATH $PATH:/composer/vendor/bin
#set our application folder as an environment variable
ENV APP_HOME /var/www/html
#change uid and gid of apache to docker user uid/gid
RUN usermod -u 1000 www-data && groupmod -g 1000 www-data
COPY ./php/vhost.conf /etc/apache2/conf-enabled/vhost.conf
# enable apache module rewrite
RUN a2enmod rewrite
#copy source files and run composer
COPY . $APP_HOME
RUN mkdir bootstrap/sessions
#RUN mkdir storage/framework/sessions && \
# mkdir storage/framework/views && \
# mkdir storage/framework/cache
# install all PHP dependencies
RUN composer install --no-dev --no-interaction
#change ownership of our applications
RUN chown -R www-data:www-data $APP_HOME
# chmod -R 0777 bootstrap && \
# chmod -R 0777 storage
RUN mv .env.production .env
EXPOSE 80
MariaDB-Dockerfile
FROM mariadb:10.4
ENV MYSQL_USER=dbuser \
MYSQL_PASSWORD=**** \
MYSQL_DATABASE=simplememo \
MYSQL_ROOT_PASSWORD=****
COPY ./init.sql /docker-entrypoint-initdb.d
EXPOSE 3306
Task definition (ECS)
{
"ipcMode": null,
"executionRoleArn": "arn:aws:iam::995962138333:role/ecsTaskExecusionRole",
"containerDefinitions": [
{
"dnsSearchDomains": null,
"environmentFiles": null,
"logConfiguration": {
"logDriver": "awslogs",
"secretOptions": null,
"options": {
"awslogs-group": "/ecs/fargate-laravel",
"awslogs-region": "ap-northeast-1",
"awslogs-stream-prefix": "ecs"
}
},
"entryPoint": null,
"portMappings": [
{
"hostPort": 80,
"protocol": "tcp",
"containerPort": 80
}
],
"command": null,
"linuxParameters": null,
"cpu": 0,
"environment": [
{
"name": "DB_DATABASE",
"value": "simplememo"
},
{
"name": "DB_HOST",
"value": "localhost"
},
{
"name": "DB_PASSWORD",
"value": "simplememodbuse"
},
{
"name": "DB_USER",
"value": "dbuser"
}
],
"resourceRequirements": null,
"ulimits": null,
"dnsServers": null,
"mountPoints": [],
"workingDirectory": null,
"secrets": null,
"dockerSecurityOptions": null,
"memory": null,
"memoryReservation": 127,
"volumesFrom": [],
"stopTimeout": null,
"image": "995962138333.dkr.ecr.ap-northeast-1.amazonaws.com/laravelecs:latest",
"startTimeout": null,
"firelensConfiguration": null,
"dependsOn": null,
"disableNetworking": null,
"interactive": null,
"healthCheck": null,
"essential": true,
"links": null,
"hostname": null,
"extraHosts": null,
"pseudoTerminal": null,
"user": null,
"readonlyRootFilesystem": null,
"dockerLabels": null,
"systemControls": null,
"privileged": null,
"name": "laravel"
},
{
"dnsSearchDomains": null,
"environmentFiles": null,
"logConfiguration": {
"logDriver": "awslogs",
"secretOptions": null,
"options": {
"awslogs-group": "/ecs/fargate-laravel",
"awslogs-region": "ap-northeast-1",
"awslogs-stream-prefix": "ecs"
}
},
"entryPoint": null,
"portMappings": [
{
"hostPort": 3306,
"protocol": "tcp",
"containerPort": 3306
}
],
"command": null,
"linuxParameters": null,
"cpu": 0,
"environment": [],
"resourceRequirements": null,
"ulimits": null,
"dnsServers": null,
"mountPoints": [],
"workingDirectory": null,
"secrets": null,
"dockerSecurityOptions": null,
"memory": null,
"memoryReservation": 128,
"volumesFrom": [],
"stopTimeout": null,
"image": "995962138333.dkr.ecr.ap-northeast-1.amazonaws.com/mariadb:latest",
"startTimeout": null,
"firelensConfiguration": null,
"dependsOn": null,
"disableNetworking": null,
"interactive": null,
"healthCheck": null,
"essential": true,
"links": null,
"hostname": null,
"extraHosts": null,
"pseudoTerminal": null,
"user": null,
"readonlyRootFilesystem": null,
"dockerLabels": null,
"systemControls": null,
"privileged": null,
"name": "mariadb"
}
],
"placementConstraints": [],
"memory": "512",
"taskRoleArn": "arn:aws:iam::995962138333:role/ecsTaskExecusionRole",
"compatibilities": [
"EC2",
"FARGATE"
],
"taskDefinitionArn": "arn:aws:ecs:ap-northeast-1:995962138333:task-definition/fargate-laravel:4",
"family": "fargate-laravel",
"requiresAttributes": [
{
"targetId": null,
"targetType": null,
"value": null,
"name": "com.amazonaws.ecs.capability.logging-driver.awslogs"
},
{
"targetId": null,
"targetType": null,
"value": null,
"name": "ecs.capability.execution-role-awslogs"
},
{
"targetId": null,
"targetType": null,
"value": null,
"name": "com.amazonaws.ecs.capability.ecr-auth"
},
{
"targetId": null,
"targetType": null,
"value": null,
"name": "com.amazonaws.ecs.capability.docker-remote-api.1.19"
},
{
"targetId": null,
"targetType": null,
"value": null,
"name": "com.amazonaws.ecs.capability.docker-remote-api.1.21"
},
{
"targetId": null,
"targetType": null,
"value": null,
"name": "com.amazonaws.ecs.capability.task-iam-role"
},
{
"targetId": null,
"targetType": null,
"value": null,
"name": "ecs.capability.execution-role-ecr-pull"
},
{
"targetId": null,
"targetType": null,
"value": null,
"name": "com.amazonaws.ecs.capability.docker-remote-api.1.18"
},
{
"targetId": null,
"targetType": null,
"value": null,
"name": "ecs.capability.task-eni"
}
],
"pidMode": null,
"requiresCompatibilities": [
"FARGATE"
],
"networkMode": "awsvpc",
"cpu": "256",
"revision": 4,
"status": "ACTIVE",
"inferenceAccelerators": null,
"proxyConfiguration": null,
"volumes": []
}
What I tried
Change the hostname of the DB connection to 127.0.0.1. EXPOSE 3306 in the Dockerfile of MariaDB. Execute the MariaDB container (Fargate) and check the database → simplememo database exists.
Additional information (FW/tool versions, etc.)
ECS cluster: Fargate based Fargate platform Ver: 1.4.0 PHP: 7.4 Laravel: 8.4 MariaDB: 10.4
Thank you for reading to the end.
If you have any hints or tips, I would be very grateful.
Note: I was able to confirm that MariaDB is able to Listen.
From the Laravel container, I found that it is listening on localhost:3306.
Also, DB_HOST=0.0.0.0 did not work. I would appreciate any suggestions, even if they are trivial.