Mysql connect host change every time in the docker container,why?
up vote
0
down vote
favorite
In the docker container.I try to login the host mysql server. First the host ip is changed,so confused for me.But second login success. Anyone can explain this strange happening?
I login ip is 192.168.100.164,but the error info shows ip 172.18.0.4,which is the container localhost.

More info:
root@b67c39311dbb:~/project# netstat -nr
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 172.18.0.1 0.0.0.0 UG 0 0 0 eth0
172.18.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0
root@b67c39311dbb:~/project# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.18.0.4 netmask 255.255.0.0 broadcast 172.18.255.255
ether 02:42:ac:12:00:04 txqueuelen 0 (Ethernet)
RX packets 2099 bytes 2414555 (2.4 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1752 bytes 132863 (132.8 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 35 bytes 3216 (3.2 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 35 bytes 3216 (3.2 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
mysql docker
add a comment |
up vote
0
down vote
favorite
In the docker container.I try to login the host mysql server. First the host ip is changed,so confused for me.But second login success. Anyone can explain this strange happening?
I login ip is 192.168.100.164,but the error info shows ip 172.18.0.4,which is the container localhost.

More info:
root@b67c39311dbb:~/project# netstat -nr
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 172.18.0.1 0.0.0.0 UG 0 0 0 eth0
172.18.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0
root@b67c39311dbb:~/project# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.18.0.4 netmask 255.255.0.0 broadcast 172.18.255.255
ether 02:42:ac:12:00:04 txqueuelen 0 (Ethernet)
RX packets 2099 bytes 2414555 (2.4 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1752 bytes 132863 (132.8 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 35 bytes 3216 (3.2 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 35 bytes 3216 (3.2 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
mysql docker
why do you need to use the dynamic IPs assigned by docker daemon if you can map the container 3306 port to your 127.0.0.1 and always use 127.0.0.1?
– Alexey
Oct 25 at 10:27
No, you misunderstand what I said before. 192.168.100.164 is my host machine ip , and in the container which running on host machine, the command: ping 192.168.100.164 work well, but mysql -u develop -h 192.168.100.164 -p raise error: ERROR(1045)...Access denied for user 'develop'@'172.18.0.4' sometimes. The ip 172.18.0.4 in the error is container ip,not I set.
– xander-ye
Nov 6 at 9:19
why not tryingmysql -u develop -h 127.0.0.1?
– Alexey
Nov 7 at 7:19
HEHE... the mysql server which I want logining is on my host machine,not on the container. you know,I am in the docker container now.
– xander-ye
Nov 7 at 8:33
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
In the docker container.I try to login the host mysql server. First the host ip is changed,so confused for me.But second login success. Anyone can explain this strange happening?
I login ip is 192.168.100.164,but the error info shows ip 172.18.0.4,which is the container localhost.

More info:
root@b67c39311dbb:~/project# netstat -nr
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 172.18.0.1 0.0.0.0 UG 0 0 0 eth0
172.18.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0
root@b67c39311dbb:~/project# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.18.0.4 netmask 255.255.0.0 broadcast 172.18.255.255
ether 02:42:ac:12:00:04 txqueuelen 0 (Ethernet)
RX packets 2099 bytes 2414555 (2.4 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1752 bytes 132863 (132.8 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 35 bytes 3216 (3.2 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 35 bytes 3216 (3.2 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
mysql docker
In the docker container.I try to login the host mysql server. First the host ip is changed,so confused for me.But second login success. Anyone can explain this strange happening?
I login ip is 192.168.100.164,but the error info shows ip 172.18.0.4,which is the container localhost.

More info:
root@b67c39311dbb:~/project# netstat -nr
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 172.18.0.1 0.0.0.0 UG 0 0 0 eth0
172.18.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0
root@b67c39311dbb:~/project# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.18.0.4 netmask 255.255.0.0 broadcast 172.18.255.255
ether 02:42:ac:12:00:04 txqueuelen 0 (Ethernet)
RX packets 2099 bytes 2414555 (2.4 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1752 bytes 132863 (132.8 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 35 bytes 3216 (3.2 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 35 bytes 3216 (3.2 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
mysql docker
mysql docker
edited Oct 25 at 9:29
asked Oct 25 at 8:38
xander-ye
6813
6813
why do you need to use the dynamic IPs assigned by docker daemon if you can map the container 3306 port to your 127.0.0.1 and always use 127.0.0.1?
– Alexey
Oct 25 at 10:27
No, you misunderstand what I said before. 192.168.100.164 is my host machine ip , and in the container which running on host machine, the command: ping 192.168.100.164 work well, but mysql -u develop -h 192.168.100.164 -p raise error: ERROR(1045)...Access denied for user 'develop'@'172.18.0.4' sometimes. The ip 172.18.0.4 in the error is container ip,not I set.
– xander-ye
Nov 6 at 9:19
why not tryingmysql -u develop -h 127.0.0.1?
– Alexey
Nov 7 at 7:19
HEHE... the mysql server which I want logining is on my host machine,not on the container. you know,I am in the docker container now.
– xander-ye
Nov 7 at 8:33
add a comment |
why do you need to use the dynamic IPs assigned by docker daemon if you can map the container 3306 port to your 127.0.0.1 and always use 127.0.0.1?
– Alexey
Oct 25 at 10:27
No, you misunderstand what I said before. 192.168.100.164 is my host machine ip , and in the container which running on host machine, the command: ping 192.168.100.164 work well, but mysql -u develop -h 192.168.100.164 -p raise error: ERROR(1045)...Access denied for user 'develop'@'172.18.0.4' sometimes. The ip 172.18.0.4 in the error is container ip,not I set.
– xander-ye
Nov 6 at 9:19
why not tryingmysql -u develop -h 127.0.0.1?
– Alexey
Nov 7 at 7:19
HEHE... the mysql server which I want logining is on my host machine,not on the container. you know,I am in the docker container now.
– xander-ye
Nov 7 at 8:33
why do you need to use the dynamic IPs assigned by docker daemon if you can map the container 3306 port to your 127.0.0.1 and always use 127.0.0.1?
– Alexey
Oct 25 at 10:27
why do you need to use the dynamic IPs assigned by docker daemon if you can map the container 3306 port to your 127.0.0.1 and always use 127.0.0.1?
– Alexey
Oct 25 at 10:27
No, you misunderstand what I said before. 192.168.100.164 is my host machine ip , and in the container which running on host machine, the command: ping 192.168.100.164 work well, but mysql -u develop -h 192.168.100.164 -p raise error: ERROR(1045)...Access denied for user 'develop'@'172.18.0.4' sometimes. The ip 172.18.0.4 in the error is container ip,not I set.
– xander-ye
Nov 6 at 9:19
No, you misunderstand what I said before. 192.168.100.164 is my host machine ip , and in the container which running on host machine, the command: ping 192.168.100.164 work well, but mysql -u develop -h 192.168.100.164 -p raise error: ERROR(1045)...Access denied for user 'develop'@'172.18.0.4' sometimes. The ip 172.18.0.4 in the error is container ip,not I set.
– xander-ye
Nov 6 at 9:19
why not trying
mysql -u develop -h 127.0.0.1 ?– Alexey
Nov 7 at 7:19
why not trying
mysql -u develop -h 127.0.0.1 ?– Alexey
Nov 7 at 7:19
HEHE... the mysql server which I want logining is on my host machine,not on the container. you know,I am in the docker container now.
– xander-ye
Nov 7 at 8:33
HEHE... the mysql server which I want logining is on my host machine,not on the container. you know,I am in the docker container now.
– xander-ye
Nov 7 at 8:33
add a comment |
3 Answers
3
active
oldest
votes
up vote
0
down vote
Try adding --add-host="localhost:192.168.100.164" when launching docker run. But this is not a good practice to my mind. You should move your mysql database to another container and create a network between them
How does this solve the problem? According to the screenshot, the connection between the client container and the server is possible, but only on the second attempt
– Nico Haase
Nov 8 at 10:57
The connection was working on the first attempt as well. If you see the first attempt error message from mysql, it tells him that the user doesn't have access from the container host, but the connection was ok. Who knows what happened to the access rights to allow connections from172.18.0.4or%fordevelopuser between the attempts? What I recommended was running mysql inside another container, not on the host machine. But even if the author wants to do so I suggested mapping host into the container network with fixed ip
– Alexey
Nov 8 at 11:02
add a comment |
up vote
0
down vote
That is true, when we start docker container it is get their own ip for container. You need to map host port with docker container. then, when you try to connect host port it redirect to myssql docker container. Please look at https://docs.docker.com/config/containers/container-networking/
How does this solve the problem? According to the screenshot, the connection between the client container and the server is possible, but only on the second attempt
– Nico Haase
Nov 8 at 10:57
add a comment |
up vote
0
down vote
I'd suggest you to create a docker bridged network and then create your container using the --add-host as suggested by Alexey.
In a simple script:
DOCKER_NET_NAME='the_docker_network_name'
DOCKER_GATEWAY='172.100.0.1'
docker network create
--driver bridge
--subnet=172.100.0.0/16
--gateway=$DOCKER_GATEWAY
$DOCKER_NET_NAME
docker run -d
--add-host db.onthehost:$DOCKER_GATEWAY
--restart=always
--network=$DOCKER_NET_NAME
--name=magicContainerName
yourImage:latest
EDIT: creating a network will also simplify the communication among containers (if you plan to have it in the future) since you'll be able to use the container names instead of their IP.
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Try adding --add-host="localhost:192.168.100.164" when launching docker run. But this is not a good practice to my mind. You should move your mysql database to another container and create a network between them
How does this solve the problem? According to the screenshot, the connection between the client container and the server is possible, but only on the second attempt
– Nico Haase
Nov 8 at 10:57
The connection was working on the first attempt as well. If you see the first attempt error message from mysql, it tells him that the user doesn't have access from the container host, but the connection was ok. Who knows what happened to the access rights to allow connections from172.18.0.4or%fordevelopuser between the attempts? What I recommended was running mysql inside another container, not on the host machine. But even if the author wants to do so I suggested mapping host into the container network with fixed ip
– Alexey
Nov 8 at 11:02
add a comment |
up vote
0
down vote
Try adding --add-host="localhost:192.168.100.164" when launching docker run. But this is not a good practice to my mind. You should move your mysql database to another container and create a network between them
How does this solve the problem? According to the screenshot, the connection between the client container and the server is possible, but only on the second attempt
– Nico Haase
Nov 8 at 10:57
The connection was working on the first attempt as well. If you see the first attempt error message from mysql, it tells him that the user doesn't have access from the container host, but the connection was ok. Who knows what happened to the access rights to allow connections from172.18.0.4or%fordevelopuser between the attempts? What I recommended was running mysql inside another container, not on the host machine. But even if the author wants to do so I suggested mapping host into the container network with fixed ip
– Alexey
Nov 8 at 11:02
add a comment |
up vote
0
down vote
up vote
0
down vote
Try adding --add-host="localhost:192.168.100.164" when launching docker run. But this is not a good practice to my mind. You should move your mysql database to another container and create a network between them
Try adding --add-host="localhost:192.168.100.164" when launching docker run. But this is not a good practice to my mind. You should move your mysql database to another container and create a network between them
answered Nov 8 at 9:23
Alexey
1,78742036
1,78742036
How does this solve the problem? According to the screenshot, the connection between the client container and the server is possible, but only on the second attempt
– Nico Haase
Nov 8 at 10:57
The connection was working on the first attempt as well. If you see the first attempt error message from mysql, it tells him that the user doesn't have access from the container host, but the connection was ok. Who knows what happened to the access rights to allow connections from172.18.0.4or%fordevelopuser between the attempts? What I recommended was running mysql inside another container, not on the host machine. But even if the author wants to do so I suggested mapping host into the container network with fixed ip
– Alexey
Nov 8 at 11:02
add a comment |
How does this solve the problem? According to the screenshot, the connection between the client container and the server is possible, but only on the second attempt
– Nico Haase
Nov 8 at 10:57
The connection was working on the first attempt as well. If you see the first attempt error message from mysql, it tells him that the user doesn't have access from the container host, but the connection was ok. Who knows what happened to the access rights to allow connections from172.18.0.4or%fordevelopuser between the attempts? What I recommended was running mysql inside another container, not on the host machine. But even if the author wants to do so I suggested mapping host into the container network with fixed ip
– Alexey
Nov 8 at 11:02
How does this solve the problem? According to the screenshot, the connection between the client container and the server is possible, but only on the second attempt
– Nico Haase
Nov 8 at 10:57
How does this solve the problem? According to the screenshot, the connection between the client container and the server is possible, but only on the second attempt
– Nico Haase
Nov 8 at 10:57
The connection was working on the first attempt as well. If you see the first attempt error message from mysql, it tells him that the user doesn't have access from the container host, but the connection was ok. Who knows what happened to the access rights to allow connections from
172.18.0.4 or % for develop user between the attempts? What I recommended was running mysql inside another container, not on the host machine. But even if the author wants to do so I suggested mapping host into the container network with fixed ip– Alexey
Nov 8 at 11:02
The connection was working on the first attempt as well. If you see the first attempt error message from mysql, it tells him that the user doesn't have access from the container host, but the connection was ok. Who knows what happened to the access rights to allow connections from
172.18.0.4 or % for develop user between the attempts? What I recommended was running mysql inside another container, not on the host machine. But even if the author wants to do so I suggested mapping host into the container network with fixed ip– Alexey
Nov 8 at 11:02
add a comment |
up vote
0
down vote
That is true, when we start docker container it is get their own ip for container. You need to map host port with docker container. then, when you try to connect host port it redirect to myssql docker container. Please look at https://docs.docker.com/config/containers/container-networking/
How does this solve the problem? According to the screenshot, the connection between the client container and the server is possible, but only on the second attempt
– Nico Haase
Nov 8 at 10:57
add a comment |
up vote
0
down vote
That is true, when we start docker container it is get their own ip for container. You need to map host port with docker container. then, when you try to connect host port it redirect to myssql docker container. Please look at https://docs.docker.com/config/containers/container-networking/
How does this solve the problem? According to the screenshot, the connection between the client container and the server is possible, but only on the second attempt
– Nico Haase
Nov 8 at 10:57
add a comment |
up vote
0
down vote
up vote
0
down vote
That is true, when we start docker container it is get their own ip for container. You need to map host port with docker container. then, when you try to connect host port it redirect to myssql docker container. Please look at https://docs.docker.com/config/containers/container-networking/
That is true, when we start docker container it is get their own ip for container. You need to map host port with docker container. then, when you try to connect host port it redirect to myssql docker container. Please look at https://docs.docker.com/config/containers/container-networking/
answered Nov 8 at 10:07
WGSSAMINTHA
567
567
How does this solve the problem? According to the screenshot, the connection between the client container and the server is possible, but only on the second attempt
– Nico Haase
Nov 8 at 10:57
add a comment |
How does this solve the problem? According to the screenshot, the connection between the client container and the server is possible, but only on the second attempt
– Nico Haase
Nov 8 at 10:57
How does this solve the problem? According to the screenshot, the connection between the client container and the server is possible, but only on the second attempt
– Nico Haase
Nov 8 at 10:57
How does this solve the problem? According to the screenshot, the connection between the client container and the server is possible, but only on the second attempt
– Nico Haase
Nov 8 at 10:57
add a comment |
up vote
0
down vote
I'd suggest you to create a docker bridged network and then create your container using the --add-host as suggested by Alexey.
In a simple script:
DOCKER_NET_NAME='the_docker_network_name'
DOCKER_GATEWAY='172.100.0.1'
docker network create
--driver bridge
--subnet=172.100.0.0/16
--gateway=$DOCKER_GATEWAY
$DOCKER_NET_NAME
docker run -d
--add-host db.onthehost:$DOCKER_GATEWAY
--restart=always
--network=$DOCKER_NET_NAME
--name=magicContainerName
yourImage:latest
EDIT: creating a network will also simplify the communication among containers (if you plan to have it in the future) since you'll be able to use the container names instead of their IP.
add a comment |
up vote
0
down vote
I'd suggest you to create a docker bridged network and then create your container using the --add-host as suggested by Alexey.
In a simple script:
DOCKER_NET_NAME='the_docker_network_name'
DOCKER_GATEWAY='172.100.0.1'
docker network create
--driver bridge
--subnet=172.100.0.0/16
--gateway=$DOCKER_GATEWAY
$DOCKER_NET_NAME
docker run -d
--add-host db.onthehost:$DOCKER_GATEWAY
--restart=always
--network=$DOCKER_NET_NAME
--name=magicContainerName
yourImage:latest
EDIT: creating a network will also simplify the communication among containers (if you plan to have it in the future) since you'll be able to use the container names instead of their IP.
add a comment |
up vote
0
down vote
up vote
0
down vote
I'd suggest you to create a docker bridged network and then create your container using the --add-host as suggested by Alexey.
In a simple script:
DOCKER_NET_NAME='the_docker_network_name'
DOCKER_GATEWAY='172.100.0.1'
docker network create
--driver bridge
--subnet=172.100.0.0/16
--gateway=$DOCKER_GATEWAY
$DOCKER_NET_NAME
docker run -d
--add-host db.onthehost:$DOCKER_GATEWAY
--restart=always
--network=$DOCKER_NET_NAME
--name=magicContainerName
yourImage:latest
EDIT: creating a network will also simplify the communication among containers (if you plan to have it in the future) since you'll be able to use the container names instead of their IP.
I'd suggest you to create a docker bridged network and then create your container using the --add-host as suggested by Alexey.
In a simple script:
DOCKER_NET_NAME='the_docker_network_name'
DOCKER_GATEWAY='172.100.0.1'
docker network create
--driver bridge
--subnet=172.100.0.0/16
--gateway=$DOCKER_GATEWAY
$DOCKER_NET_NAME
docker run -d
--add-host db.onthehost:$DOCKER_GATEWAY
--restart=always
--network=$DOCKER_NET_NAME
--name=magicContainerName
yourImage:latest
EDIT: creating a network will also simplify the communication among containers (if you plan to have it in the future) since you'll be able to use the container names instead of their IP.
edited Nov 8 at 10:59
answered Nov 8 at 10:54
Stefano
1,031615
1,031615
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52984913%2fmysql-connect-host-change-every-time-in-the-docker-container-why%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
why do you need to use the dynamic IPs assigned by docker daemon if you can map the container 3306 port to your 127.0.0.1 and always use 127.0.0.1?
– Alexey
Oct 25 at 10:27
No, you misunderstand what I said before. 192.168.100.164 is my host machine ip , and in the container which running on host machine, the command: ping 192.168.100.164 work well, but mysql -u develop -h 192.168.100.164 -p raise error: ERROR(1045)...Access denied for user 'develop'@'172.18.0.4' sometimes. The ip 172.18.0.4 in the error is container ip,not I set.
– xander-ye
Nov 6 at 9:19
why not trying
mysql -u develop -h 127.0.0.1?– Alexey
Nov 7 at 7:19
HEHE... the mysql server which I want logining is on my host machine,not on the container. you know,I am in the docker container now.
– xander-ye
Nov 7 at 8:33