I have been watching this before.
Moving Wordpress site to Docker: Error establishing DB connection
My Linux is centos VM, I try to create WordPress and MySQL(5.6~8).
It will be created but I can not connect DB.
This is my command (using swarm).
docker swarm init --advertise-addr=192.168.50.31
docker swarm join --token -secret token- 192.168.50.31:2377
docker network create -d overlay demo
docker service create --name wordpress
-p 80:80 --env WORDPRESS_DB_PASSWORD=root
--env WORDPRESS_DB_HOST=mysql
--network demo wordpress
docker service create --name mysql
--env MYSQL_ROOT_PASSWORD=root --env
MYSQL_DATABASE=wordpress --network demo
--mount
type=volume,source=mysql-data,destination=/var/lib/mysql mysql:5.6
I try to search it why. But did not find the answer.
Related
I'd like to test webtrees PHP docker. They suggest connecting to a mysql docker using --link mysql:db, as follows:
docker run -d -p 80:80 --name webtrees --link mysql:db -v /webtrees/data:/var/www/html/data -v /webtrees/media:/var/www/html/media -e DISABLE_SSL=TRUE -e PORT=80 --restart always dtjs48jkt/webtrees
Their README says:
The image does not contain a MySQL database. Instead you have to use a separate MySQL instance. For example you could use the
MySQL Docker Image. Using the --link parameter a direct connection to
the database in an other container could be established. If you use
the --link parameter it is sufficient to set as database hostname db
and port 3306. The database user must have all access rights to
create the necessary database and tables.
However the webtrees container cannot access the mysql server. How I correctly link these two docker containers?
I tried using the official mysql docker image as follows:
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -e MYSQL_DATABASE=webtrees -e MYSQL_USER=my_user -e MYSQL_PASSWORD=my_pwd -d mysql:5.5
but then I don't know how to link webtrees docker container with the mysql container.
Also, how can I use the mysql client? the documentation gives this example, but I don't understand what are the correct parameters for netowrk and -h:
$ docker run -it --network some-network --rm mysql mysql -hsome-mysql -uexample-user -p
Regarding your first question, the --link option for docker run is deprecated according to the documentation, so I wouldn't recommend using it.
With the amount of configuration required, I'd recommend setting up a docker-compose.yml instead. I set up the configuration you require like this:
version: '3.0'
services:
webtrees:
image: dtjs48jkt/webtrees
restart: always
ports:
- 80:80
environment:
- DISABLE_SSL=TRUE
- PORT=80
volumes:
- /webtrees/data:/var/www/html/data
- /webtrees/media:/var/www/html/media
networks:
- my-network
mysql:
image: mysql:5.5
environment:
- MYSQL_ROOT_PASSWORD=my-secret-pw
- MYSQL_DATABASE=webtrees
- MYSQL_USER=my_user
- MYSQL_PASSWORD=my_pwd
networks:
- my-network
networks:
my-network:
To run the containers, use:
docker-compose up --detach
What this will do is spin up a mysql container and a webtrees container according to the configuration you specified in your question with a network called my-network.
In the web interface of web trees on http://localhost/ you can make it connect to the mysql container with the following configuration, so it will connect to it through the docker network:
Since the service name in the docker-compose.yml is mysql, the required hostname is mysql.
Basically you need to have all the containers (mysql DB server, mysql client and application) in the same Docker network. By default they are not. Alternatively, --link can be used to link them (as shown in webtrees run example), but it's considred as legacy feature and network should be used instead of that.
So what you need to do:
Create custom Docker network:
docker network create user-network
Run mysql server in that network. Name should be db, because webtrees relies on that hostname for DB:
docker run --name db --network user-network -e MYSQL_ROOT_PASSWORD=my-secret-pw -e MYSQL_DATABASE=webtrees -e MYSQL_USER=my_user -e MYSQL_PASSWORD=my_pwd -d mysql:5.5
Run mysql client in the same network:
docker run -it --network user-network --rm mysql mysql -hdb -umy_user -p
Finally you can run an app in the same network:
docker run -d -p 80:80 --name webtrees --network user-network -v /webtrees/data:/var/www/html/data -v /webtrees/media:/var/www/html/media -e DISABLE_SSL=TRUE -e PORT=80 --restart always dtjs48jkt/webtrees
After that web app should be accessible from your browser under http://localhost/
I pulled mysql docker image and run container with command:
docker run --name myapp-mysql -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=mydb -p 3306:3306 -d mysql:latest
At this point springBoot locally is working. It is connected with mysql:
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=root
Now I want springBoot app also to be on separate docker container, on the same server. For that I'm using Dockerfile:
FROM openjdk:8-jdk-alpine
ADD target/myapp.jar myapp.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "myapp.jar"]
I'm starting this image with:
docker run --name myapp -p 8080:8080 -it myapp:latest
Now springBoot container cannot find myslq container.
I try:
docker run --name myapp -p 8080:8080 --link=myapp-mysql:mysql -it myapp:latest
But again it cannot connect to mysql container. How can I connect springBoot container to mysql container?
I also tried to change application.properties to:
spring.datasource.url=jdbc:mysql://myapp-mysql:3306/mydb
spring.datasource.username=root
spring.datasource.password=root
and start springBoot with:
docker run --name myapp -p 8080:8080 --link=myapp-mysql:mysql -it myapp:latest
Again it cannot connect to db:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: 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.
In order to link the containers, I will suggest look into docker-compose.
If you don't want to use docker-compose then,
First of all, In your springboot properties file, change the url to:
spring.datasource.url=jdbc:mysql://[container-name]:3306/mydb
In your case,
docker run --name myapp-mysql -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=mydb -p 3306:3306 -d mysql:latest
container-name: myapp-mysql
then try to run both containers.
if it doesn't work then look into docker networks & put the containers in same network.
Docker Network Creation
I just created a mysql docker container using
docker run -p 3310:3310 --restart always --env MYSQL_ROOT_HOST=% --name=ipca-mysql -e MYSQL_ROOT_PASSWORD=admin -d mysql/mysql-server:5.7
I am able to connect to my database using docker exec -ti ipca-mysql bash and mysql -u root -p
But when I tried to connect to my database at localhost:3310 using mysql workbench I get:
lost connection to mysql server at 'reading initial communication
packet' system error 0
Any idea what is this issue?
MySQL container start using this command it's worked.
docker run -p 3310:3306 --restart always --env MYSQL_ROOT_HOST=% --name=ipca-mysql -e MYSQL_ROOT_PASSWORD=admin -d mysql/mysql-server:5.7
You could use this docker command:
docker run --name mysql-tacs -e MYSQL_ROOT_PASSWORD=root -d -p 3310:3306 mysql
In the link below there is a video which shows step by step the creation of mysql server with docker until the connection in its server using MySQL Workbench.
https://www.youtube.com/watch?v=ZZwvhMvUJiw
Here is the picture of what I am doing:
I have local Go API code, I have built it into a docker image.
I have pulled MySQL docker image from docker.
I have DB in MySQL and Go API is accessing mysql.
Everything worked fine till my Go API was local and mysql was docker container. Now I have built local Go code as docker image and when I try to run this image using docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password -d mysql:5.5 , Docker container starts and exits immediately.
I tried Docker Start -a Container-ID to start container again, I get this error 'dial tcp 127.0.0.1:3306: getsockopt: connection refused'.
When I searched about this error I got this input - "After setting bind-address: 127.0.0.1 in mysql server config I was able to get the installation working with host localhost:3306."
But I am not aware how to set bind-address.
Any inputs regarding this will be helpful.
Thanks.
You should use Docker's network service discovery feature.
Put both your containers onto the same network and then they can discover eachother via DNS.
For example:
docker network create mynet
docker run --net mynet --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password -d mysql:5.5
docker run --net mynet --name app -p 80:80 -d myappimage:latest
Both of these containers will be able to resolve 'mysql' or 'app' to the ip that each respective container has on the 'mynet' network. Configure your application to connect to mysql at 'mysql:3306'.
I am trying to connect a container with tomcat to a container with mysql in order to deploy an app. I have reproduced the same into a vagrant machine (tomcat 6, mysql 5.6) but now I want to do it using docker containers.
I used this answer here with some extra additions.
For the mysql container I run:
sudo docker run --name mysql -e MYSQL_USER=root -e MYSQL_ROOT_PASSWORD=admin -e MYSQL_DATABASE=my_database_name -d mysql:5.6
For the Tomcat container
docker run -d -p 8080:8080 --name tomcat --link mysql:mysql -v $PWD/webapp:/usr/local/tomcat/webapps tomcat:6
Also I have the folder webapp/with_my_app.war. So far so good.
This specific app needs some config in the WEB-INF/context.xml file, so I run
docker exec -it tomcat bash
Then I updated and installed vim and finally edit the context.xml file and restarted tomcat.
But I cannot access my app at localhost:8080/with_my_app
What am I missing?
Include --cap-add SYS_PTRACE while running tomcat container.
docker run -d -p 8080:8080 --cap-add SYS_PTRACE ......