简体   繁体   English

将 docker MySql 连接到本地主机 mysql

[英]Connect docker MySql to localhost mysql

I have used the following command for port mapping我使用以下命令进行端口映射

sudo docker run -d -p 3306:33060 --name App1 APP/framework:app

where 3306 port belongs to localhost machine MySQL and 33060 is the port into a docker container, so communication is happening between 3306:33060 in docker.其中 3306 端口属于 localhost 机器 MySQL 和 33060 是 docker 容器的端口,因此 Z05B6053C41A21430AFD6FC3B156 中的3306:33060之间发生通信I'm getting issue as below:我遇到以下问题:

sudo docker run -d -p 3306:33060 --name App1 APP/framework:app fc1ffe98b2f2e6299a3070be8296d8b530ef4bdb3bd4cfd79d28ffc535a361c1

docker: Error response from daemon: driver failed programming external connectivity on endpoint App1 (30ec933973acf63a48ef9a20b0027af18bd23e1f36cf852e2e3e3758eaa1f843): Error starting userland proxy: listen tcp 0.0.0.0:3306: bind: address already in use. docker: Error response from daemon: driver failed programming external connectivity on endpoint App1 (30ec933973acf63a48ef9a20b0027af18bd23e1f36cf852e2e3e3758eaa1f843): Error starting userland proxy: listen tcp 0.0.0.0:3306: bind: address already in use.

I don't want mysql in docker container to create image.Just want to open random port and map 3306 and host-ip to communication with port in docker ie 33060 I don't want mysql in docker container to create image.Just want to open random port and map 3306 and host-ip to communication with port in docker ie 33060

Can anyone please suggest any way to resolve this that would be appreciated?任何人都可以提出任何解决这个问题的方法吗? Thank you.谢谢你。

Some examples from the docker documentation, before the colon is the container port, and after the colon is the container host port. docker 文档中的一些示例,冒号之前是容器端口,冒号之后是容器主机端口。

-p 8080:80 -p 8080:80
Map TCP port 80 in the container to port 8080 on the Docker host. Map TCP 容器中的80端口到Docker主机上的8080端口。

-p 192.168.1.100:8080:80 -p 192.168.1.100:8080:80
Map TCP port 80 in the container to port 8080 on the Docker host for connections to host IP 192.168.1.100. Map TCP port 80 in the container to port 8080 on the Docker host for connections to host IP 192.168.1.100.

-p 8080:80/udp -p 8080:80/udp
Map UDP port 80 in the container to port 8080 on the Docker host. Map UDP 容器中的80端口到Docker主机上的8080端口。

-p 8080:80/tcp -p 8080:80/udp -p 8080:80/tcp -p 8080:80/udp
Map TCP port 80 in the container to TCP port 8080 on the Docker host, and map UDP port 80 in the container to UDP port 8080 on the Docker host. Map TCP port 80 in the container to TCP port 8080 on the Docker host, and map UDP port 80 in the container to UDP port 8080 on the Docker host.

More information at,https://docs.docker.com/config/containers/container-networking/更多信息,请访问 https://docs.docker.com/config/containers/container-networking/

If you want to access port 3306 in the host from your docker container, you can use如果要从 docker 容器访问主机中的端口 3306,可以使用

sudo docker run -d --net host --name App1 APP/framework:app sudo docker run -d --net host --name App1 APP/framework:app

and you container will share the host's network meaning that you could do localhost:3306 from inside your container and "localhost" on the container would be exactly the same thing as "localhost" on the host.并且您的容器将共享主机的网络,这意味着您可以从容器内部执行 localhost:3306 并且容器上的“localhost”与主机上的“localhost”完全相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM