The Plan
I want my tomcat server to be able to connect to my MySQL server both in separate containers.
The Problem
If you are using a container already running or run by using separate DockerFile, then you might want to consider using
external_links:
- mysql-container:db
Note that the name mysql-container is the name of your container. If you don't know the name or haven't specified one in the docker-compose.yml you can also find it by running
docker ps
The column with the NAMES is the one you'll need in the external links. The db after : is just an alias and it could be anything you want. It will be your hostname. For example if before dockerizing the database your host was localhost you have to now change it into db.
Lastly, you must also be sure to have both of the containers running in the same network. You can do that by creating a new network as:
docker network create --driver=bridge my-network
Then inside the docker-compose.yml of both the containers, add the following network configuration at the same indentation level as that of service
networks:
default:
external:
name: my-network