Failed to deploy multi-containers docker to ElasticBeanstalk

Viewed 460

I was trying to deploy multi containers docker into Elastic Beanstalk. But it doesn't work. I tried running on local by using the docker-compose it is working.

Below is my Dockerrun.aws.json and docker-compose.yml

docker-compose.yml

version: '3'

services:
   nginx:
      image: nginx:alphine
      restart: always
      ports:
      - 80:80
      volumes:
      - ./nginx/conf.d:/etc/nginx/conf.d
      depends_on:
      - sbapi
   db:
      image: mysql:latest
      environment:
      - MYSQL_ROOT_PASSWORD=P@ssw0rd
      - MYSQL_DATABASE=sbapi
      - MYSQL_USER=user
      - MYSQL_PASSWORD=password
      ports:
      - 3306:3306
      restart: always
      volumes:
      - mysql-data:/var/lib/mysql
   sbapi:
      image: sbapi
      build:
         context: ./
         dockerfile: Dockerfile
      ports:
      - 8080:8080
      restart: always
      volumes:
      - ./app:/app
      depends_on:
      - db
      
volumes:
  mysql-data:

Dockerrun.aws.json

 {
   "AWSEBDockerrunVersion":2,
   "containerDefinitions":[
      {
         "name":"db",
         "hostname": "db",
         "image":"mysql:latest",
         "essential":true,
         "memory":512,
         "environment": [
            {
                "name": "MYSQL_ROOT_PASSWORD",
                "value": "P@ssw0rd"
            },
            {
                "name": "MYSQL_DATABASE",
                "value": "sbapi"
            },
            {
                "name": "MYSQL_USER",
                "value": "user"
            },
            {
                "name": "MYSQL_PASSWORD",
                "value": "password"
            }
         ],
         "portMappings":[
            {
               "hostPort":3306,
               "containerPort":3306
            }
         ],
         "mountPoints":[
            {
               "containerPath":"/var/lib/mysql",
               "sourceVolume":"mysql-data"
            }
         ]
      },
      {
         "name":"sbapi",
         "hostname": "sbapi",
         "essential":true,
         "image":"xxxxxxxxxxxxxxxx.dkr.ecr.ap-southeast-1.amazonaws.com/sbapi",
         "memory":512,
         "portMappings":[
            {
               "hostPort":8080,
               "containerPort":8080
            }
         ],
         "links": [
            "db"
         ]
      },
      {
         "name":"nginx",
         "image":"nginx:alpine",
         "essential":true,
         "memory": 128,
         "portMappings":[
            {
               "hostPort":80,
               "containerPort":80
            }
         ],
         "mountPoints":[
            {
               "containerPath":"/etc/nginx/conf.d",
               "sourceVolume":"nginx-data"
            }
         ],
         "links":[
            "sbapi"
         ]
      }
   ],
   "family": "",
   "volumes":[
      {
         "host":{},
         "name":"mysql-data"
      },
      {
         "name": "nginx-data",
         "host": {
            "sourcePath": "/var/app/current/nginx/conf.d"
         }
      }
   ]
}

This is my nginx.stouterr.log. Not sure will it will auto shutdown.

/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf differs from the packaged version
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2021/08/26 00:21:11 [notice] 1#1: using the "epoll" event method
2021/08/26 00:21:11 [notice] 1#1: nginx/1.21.1
2021/08/26 00:21:11 [notice] 1#1: built by gcc 8.3.0 (Debian 8.3.0-6) 
2021/08/26 00:21:11 [notice] 1#1: OS: Linux 4.14.238-125.422.amzn1.x86_64
2021/08/26 00:21:11 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 133489:133489
2021/08/26 00:21:11 [notice] 1#1: start worker processes
2021/08/26 00:21:11 [notice] 1#1: start worker process 30
2021/08/26 00:21:11 [notice] 1#1: start worker process 31
2021/08/26 00:21:11 [notice] 1#1: signal 3 (SIGQUIT) received, shutting down
2021/08/26 00:21:11 [notice] 30#30: gracefully shutting down
2021/08/26 00:21:11 [notice] 30#30: exiting
2021/08/26 00:21:11 [notice] 30#30: exit
2021/08/26 00:21:11 [notice] 31#31: gracefully shutting down
2021/08/26 00:21:11 [notice] 31#31: exiting
2021/08/26 00:21:11 [notice] 31#31: exit
2021/08/26 00:21:11 [notice] 1#1: signal 17 (SIGCHLD) received from 30
2021/08/26 00:21:11 [notice] 1#1: worker process 30 exited with code 0
2021/08/26 00:21:11 [notice] 1#1: worker process 31 exited with code 0
2021/08/26 00:21:11 [notice] 1#1: exit

I checked the ECS, at first, 3 containers are running. But later on, all status changed to stopped and it spawns another new task again.

I also get the error "connection refused" to connect the MySQL from my spring boot application.

Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[na:na]
    at java.base/java.lang.reflect.Constructor.newInstance(Unknown Source) ~[na:na]
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) ~[mysql-connector-java-8.0.23.jar!/:8.0.23]
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105) ~[mysql-connector-java-8.0.23.jar!/:8.0.23]
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151) ~[mysql-connector-java-8.0.23.jar!/:8.0.23]
    at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167) ~[mysql-connector-java-8.0.23.jar!/:8.0.23]
    at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:89) ~[mysql-connector-java-8.0.23.jar!/:8.0.23]
    at com.mysql.cj.NativeSession.connect(NativeSession.java:144) ~[mysql-connector-java-8.0.23.jar!/:8.0.23]
    at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:953) ~[mysql-connector-java-8.0.23.jar!/:8.0.23]
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:823) ~[mysql-connector-java-8.0.23.jar!/:8.0.23]
    ... 58 common frames omitted
Caused by: java.net.ConnectException: Connection refused (Connection refused)
    at java.base/java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:na]
    at java.base/java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) ~[na:na]
    at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) ~[na:na]
    at java.base/java.net.AbstractPlainSocketImpl.connect(Unknown Source) ~[na:na]
    at java.base/java.net.SocksSocketImpl.connect(Unknown Source) ~[na:na]
    at java.base/java.net.Socket.connect(Unknown Source) ~[na:na]
    at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155) ~[mysql-connector-java-8.0.23.jar!/:8.0.23]
    at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:63) ~[mysql-connector-java-8.0.23.jar!/:8.0.23]
    ... 61 common frames omitted

This is my application.properties

spring.datasource.url=jdbc:mysql://db:3306/sbapi?allowPublicKeyRetrieval=true&useSSL=false
spring.datasource.username=user
spring.datasource.password=password

server.port = 8080

I was able to access the API when running docker-compose up from local. However, when deploying to Elastic Beanstalk, it doesn't work. I'm not sure which part goes wrong, and I was curious why the ECS will stop itself and respawn new task again and again?

2 Answers

EB's Docker platform supports docker-compose.yml as explained in Docker environment with Docker Compose:

Create a docker-compose.yml file to deploy a Docker image from a hosted repository to Elastic Beanstalk. No other files are required if all your deployments are sourced from images in public repositories.

So you may need Dockerrun.aws.json v3 only to enable access to private docker registry.

  1. Make sure that your bind address is correct (hostname of IP address whichever you plan to use)
  2. Ensure that the JDBC port number is correct
  3. Ensure the security groups are open for the associated ports
  4. And surely, please validate the username and password for database
Related