Search code examples
mysqlspring-bootdockerdocker-composeamazon-ecs

Deploy mysql+springboot docker containers in AWS ECS


I am working on simple project with mysql + springboot using docker container and i am able to run in my local without any issue. I tried to bring the same containers to AWS ECS to standup but i am facing multiple problems. I did research multiple documents and blogs but could not get the correct content to make this works.

I used the below to stand up the mysql in my local with the container name of "mysqlcontainer"

docker run --restart always --name mysqlcontainer --net dev-network -v /Users/myuser/Develop/mysql_data/8.0:/var/lib/mysql -p 3306:3306 -d
-e MYSQL_ROOT_PASSWORD=password mysql:8.0

Once mysql stood up then i ran the below command to bring my springboot application. This is simple springboot service which has the CRUD operation with mysql.

docker run -d -p 8061:8061 --name user-mysqlapp --net dev-network
--link mysqlcontainer user-mysql

mysql container name is "mysqlcontainer" which runs on the network "dev-network" and i have used these container and network name in second docker command to stand up the springboot application as both container needs to talk each other within the same network.

here is the docker file for springboot application.

FROM openjdk:8-jdk-alpine 
EXPOSE 8061 ARG
JAR_FILE=target/user-mysql.jar 
ADD ${JAR_FILE} user-mysql.jar
ENTRYPOINT ["java","-jar","user-mysql.jar"]

Here is the datasource that is being used in application.yml within springboot application.

 datasource:
    url:  jdbc:mysql://mysqlcontainer:3306/sample
    username: dummy
    password: password
    driver-class-name:  com.mysql.jdbc.Driver

after both containers are running, i was able to connect the mysql with mysqldeveloper and able to execute the below command.

CREATE USER 'dummy'@'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON * . * TO 'dummy'@'%';
create database sample;
use sample;

I have no issue of executing this setup in my local and i did push the springboot application image to dockerhub to use in AWS ECS.

Problem statement:

If i run the mysql container alone in ECS, i am able to run it and able to connect using mysql developer from my local. but if i try to run springboot app image, its not properly linking with mysql container. I tried in FARGATE instance type with awsvpc network mode where i was unable to give link to mysql container. i did tried to create single taskdefinition where both containers were added and both of them were running successfully but in the spring boot log, it says unable to create the communication with mysql container.

Can someone please share some information to stand up this setup in aws ecs or share some link/tutorial/blog where i can see how this mysql + springboot app setup can stand up in aws ecs? if i am able to stand up these containers then i need to create/attach volume to the persisting the data in aws. Thanks in advance...


Solution

  • I am able to setup this spring boot + mysql setup successfully in ECS. Below are the steps were followed.

    1. Create one task definition in which add mysql container first where provide the necessary env variable to mysql container.
    2. Add another spring boot container in which set the dependency order container as mysql container with condition of "START".
    3. Create new cluster
    4. Create service on this cluster and then add this task definition on the service.

    Since i used fargate type, we can't use link to refer the other container but instead i used 127.0.0.1 ip address in spring boot application yml data source url so that it will refer the local host within the same network. This solves the container linking issue and i am sure we would have some better option but for time being i am using this option.

     datasource:
        url:  jdbc:mysql://127.0.0.1:3306/sample