I want to use the mysql container and create the container according to the description on the hub
docker run --name mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:8
docker run -d --name nx --link mysql:db nginx
Direct use of the basic container failed to connect successfully
~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bf673d61f789 nginx "nginx -g 'daemon ..." 3 seconds ago Up 2 seconds 80/tcp, 443/tcp nx
1ac01c685e9b mysql:8 "docker-entrypoint..." About a minute ago Up About a minute 3306/tcp mysql
Unable to access localhost:3306 in nginx
By commanddocker inspect -f "{{ .HostConfig.Links }}" proxy
[/mysql:/nx/db]
Check that the connection is successful
hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.4 db 1ac01c685e9b mysql
172.17.0.6 8242073e142b
It seems a bit random,
It’s very different from the localhost:3306 I expected
Is there a way to use localhost:3306 after connecting?
Link is to establish a network link between two containers, not to merge the network ports of the two containers together! !
Replace the access
localhost:3306
with the accessdb:3306
to successfully access the database service