The Plan
I want my tomcat server to be able to connect to my MySQL server both in separate containers.
The Problem
Tomcat cannot connect to MySQL
I used some of the details from the wordpress tutorial about setting up a link with the mysql container and created the link to the MySQL.
Although the tomcat and mysql spin up just fine I can't seem to get tomcat to be able to connect to MySQL, the settings work on my local machine perfectly fine.
I've attempted to use --net: "host"
as well although that does not work with Tomcat as it throws a severe error.
Previous answers
I noticed on this post a load of possible fixes for the error although I don't believe any of these would translate to my problem as I believe this is a docker problem not a host one.
docker-compose.yml
web:
image: tomcat:7.0
container_name: tomcat-container
ports:
- "80:8080"
hostname: docker-tomcat
volumes:
- /home/webapps:/usr/local/tomcat/webapps
links:
- db
db:
image: mysql
container_name: mysql-container
environment:
MYSQL_ROOT_PASSWORD: Mysqlpassword1
MYSQL_DATABASE: tracker
volumes:
- /home/mysqlDB:/var/lib/mysql
This is my Context.xml from tomcat.
<Context>
<Resource
name="jdbc/tracker" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
url="jdbc:mysql://localhost:3306/tracker?useSSL=false"
driverClassName="com.mysql.jdbc.Driver"
username="root" password="mysqladmin1"
/>
<Resource
name="jdbc/jenkins" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
url="jdbc:mysql://localhost:3306/jenkins?useSSL=false"
driverClassName="com.mysql.jdbc.Driver"
username="root" password="mysqladmin1"
/>
</Context>
The error code.
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:1549)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1388)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
at databaseConnections.SQLDatabaseConnection.tableExists(SQLDatabaseConnection.java:131)
at databaseConnections.JiraSQLDatabaseConnection.<init>(JiraSQLDatabaseConnection.java:50)
For example, to connect to the MySQL server from the ‘tomcat’ container, set the database address as mysql:3306. To deploy the app, you can call docker-compose up, and Docker Compose will automatically build the two images according to docker-compose.yml and run these two images on two containers concurrently.
Create a separate file called docker-compose.yml at the root of the project. This file describes the whole stack of the application, where includes both the web server and MySQL. For our Tomcat application, it builds the Docker image based on the Dockerfile at the same directory. A separate image of MySQL is also built.
If you need to set up a database quickly and without using up too many resources, deploying MySQL in a container is a fast and efficient solution. This is only appropriate for small and medium-sized applications. Enterprise-level applications would not find a MySQL Docker container sufficient for their workload.
Also, by setting container_name, the container name is fixed, and no longer dependent on the compose project name. Docker Compose automatically adds a network-alias for the service name (i.e., the container may be named myproject_mysql, but if the service is named mysql, other containers can still access it under the name of that service ( mysql ).
As you're linking db as "db", you cannot use localhost to join you database. you should "db"
jdbc:mysql://db:3306/tracker?useSSL=false
In your container, localhost design your tomcat container, not your host. MySQL container has his own network.
Futhermore, if you don't like "db" name, you can name link it with different name
For exemple:
links:
- db:container-mysql
In this case, inside you tomcat container, you could use
jdbc:mysql://container-mysql:3306/tracker?useSSL=false
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With