1

I am new to docker and I am struggling to have the below scenario working: I have a working webapp image that uses alpine with tomcat 9 as a base image and a added my web app along with a series of user libraries.

Then I have a database image with mysql base, which I imported the database required by my web app to work.

If I run those images separately, they work fine, I have 100% access to them from the host system, no problem at all.

Now, I need them to communicate between themselves, it sounded to me this would be the easy part, but I am failing to make it work.

This is my composer.yml:

version: "3"
networks:
  uno:
services:
  mysql:
    image: mysql
    ports:
      - "3307:3306"
    networks:
      - uno
  web:
    image: unoerp9
    ports:
      - "8888:8080"
    depends_on:
      - mysql
    links:
      - mysql
    networks:
      - uno

The container starts ok: Check image here

But after some seconds, some erros appear on the log, and the mysql container stops:

mysql_1 | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/bck.sql

mysql_1 | mysql: [Warning] Using a password on the command line interface can be insecure.

mysql_1 | mbind: Operation not permitted

mysql_1 | mbind: Operation not permitted

mysql_1 | ERROR 1822 (HY000) at line 17806: Failed to add the foreign key constraint. Missing index for constraint 'fn_comissao_pagto_grupo_ibfk_1' in the referenced table 'vd_grupo_colaborador'

db_mysql_1 exited with code 1

Since there are erros with the mysql container, the webapp container trigger erros related to not being able to connect to the database, which is completely expected, so I am ommiting those.

Also, as the images work fine separately, I don't think it is some error related to how I built them, it must be something I am missing in my composer file. The expected result is to have both images running and communicating while I am able to access the web image through the browser (localhost:8888/) and the database (localhost:3307).

Any help?

0

You can simplify your compose file considerably:

version: "3"
services:
  mysql:
    image: mysql:5.7
    volumes:
       - mysql-data:/var/lib/mysql
  web:
    image: unoerp9
    ports:
      - "8888:8080"

There is a network created automatically for every compose file and all containers are part of it. Within this network all ports of the containers are exposed. That means you can connect from your tomcat the database with mysql/my_database_name, you can omit the port there too because it is the default 3306.

I believe your MySQL error arises because the default image mysql:latest is version 8, which is probably not the same as the production system. Above I chose the tag 5.7 because it specifies the version to some degree so you wont get compatibility issues, but still take advantage of security updates from for example 5.7.22 (which @Mohsen suggested) to 5.7.23 that is the newest version at the moment.

I also added a volume to store the database data (in case that's helpful), so changes you make to the database are kept between restarts (docker-compose down) when the containers are deleted.

  • Hi Brom, I worked during the weekend on the problem, could'nt reply before. And you are almost right. We have some servers running production with mysql8, what I was not informed by the team is that for those servers, the authentication method for the root (or any user), needs to be changed to legacy, otherwise the application fails to connect. Also, I discovered through an older question here on server faultwhat you mentioned about the "ip" from the container, using mysql (or the container name) works perfectly. – Estevao Santiago Aug 13 '18 at 12:28
0

You need to add a dependency for the web image on the mysql image by adding these lines

depends_on: - mysql

You can link the web image with the database image using:

links: - mysql

So you can use mysql as a hostname to connect to the database.

Try to use a specific mysql image such as mysql:5.7.22

This error mysql_1 | ERROR 1822 (HY000) at line 17806: Failed to add the foreign key constraint. Missing index for constraint 'fn_comissao_pagto_grupo_ibfk_1' in the referenced table 'vd_grupo_colaborador' is related to your mysql schema adn we cannot help with it without showing us the source code, if you can share it that would be great.

  • OK, I was googling and I found the depends_on you just informed, that fixed the issue of the mysql container terminating, and I am able to connect to the database and the webapp from the host. Also the errro with the constraint didn't appear anymore (the db is a copy of a production and it works fine, I have no idea why it is triggered in the docker), so let us forget about it. But the webapps is still unable to connect to the database. I have set the tomcat properties file do localhost:3307/my_database_name, but I am guessing localhost is not a valid option yet? I updated my yml file. – Estevao Santiago Aug 10 '18 at 19:30
  • I've entered the web container bash, installed mysql-client and tried to connect to the mysql container using localhost ports 3307 and then on 3306 with no luck. I then tried to inspect my mysql container and use the ip from "IPAddress": to connect, and another fail. As a side note, it worked when I tried to connect to a mysql on the web... just did the test to make sure the web container was able to connect to mysql servers. – Estevao Santiago Aug 10 '18 at 19:43
  • When you are inside the webapp container, localhost refers to the container not your host, there are two ways to connect from webapp to the mysql container: 1- Connect to the host OS using the IP address of the docker bridge which is usually 172.17.16.1. 2- If you add the links option I told you about it you can connect using mysql as the hostname, set tomcat properties file to mysql:3306/my_database_name and it should work. I hope this helps you. – Mohsen Ibrahim Aug 11 '18 at 9:09
  • Linking containers is deprecated, see here: docs.docker.com/compose/compose-file/#links Because docker-compose creates a network by default, in which you can access every container by its name, there is no need to use it any more. – Alwinius Aug 12 '18 at 0:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.