title: App container cannot connect to mysql container in the same network
body: <p>I have a problem with the docker and containerized apps not connecting to mysql database that is running in a container within the same docker network. My <code>docker-compose.yml</code> looks like this:</p>
  <pre><code>version: '3.8'
  
  services:
  
    mysqlserver:
      image: mysql:5.7.24
      container_name: mysql_app
      networks:
        - somenetwork
      ports:
        - &quot;5647:3306&quot;
      environment:
        MYSQL_DATABASE: database
        MYSQL_USER: USER
        MYSQL_PASSWORD: PASS
        MYSQL_ROOT_PASSWORD: PASS
      command:
        - --max_connections=1000
      restart: always
      volumes:
       - persistent:/var/lib/mysql
  
    app:
      image: app/appserver:latest
      container_name: app_server
      networks:
        - somenetwork
      ports:
        - &quot;8080:8080&quot;
      depends_on:
        - mysqlserver
      environment:
        SPRING_DATASOURCE_URL: jdbc:mysql://mysqlserver:5647/database?verifyServerCertificate=false&amp;useSSL=false&amp;requireSSL=false&amp;autoReconnect=true
  
  networks:
    somenetwork:
      name:
        somenetwork_share_net
  
  volumes:
    persistent:
  </code></pre>
  <p>And looking at the logs from the app container, I found errors like this:</p>
  <blockquote>
  <p>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.</p>
  </blockquote>
  <p>and</p>
  <blockquote>
  <p>Caused by: java.net.ConnectException: Connection refused (Connection refused)
  at java.base/java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:na]</p>
  </blockquote>
  <p>Is there something wrong with my <code>docker-compose.yml</code>? Why the app container cannot connect</p>