0

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. 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.

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 i.e 33060

Can anyone please suggest any way to resolve this that would be appreciated? Thank you.

David Maze
  • 130,717
  • 29
  • 175
  • 215
SDsan
  • 1
  • 1
  • 1
    Your understanding [published port mapping](https://docs.docker.com/config/containers/container-networking/#published-ports) is not correct. `-p 3306:33060` does not mean _communication is happening between 3306:33060_. That means when someone tries to access 3306 on the host the request gets mapped to container's 33060 port. In this instance you have mysql running on localhost at 3306 hence you cannot map container's port to that – madteapot Mar 10 '21 at 09:25
  • yes but I want 3306 to map all packet to inside docker i.e 33060 – SDsan Mar 10 '21 at 09:34
  • Is the `APP/framework:app` image running a MySQL database, listening on a non-standard port 33060? And then, _from outside Docker_, sending a request to the normal MySQL port 3306 forwards to that port inside the container? – David Maze Mar 10 '21 at 10:43
  • (If you're trying to get requests to go the other way – forward requests from some port inside the container to a port on the host – `docker run -p` doesn't do that, but see [From inside of a Docker container, how do I connect to the localhost of the machine?](https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach)) – David Maze Mar 10 '21 at 10:44

2 Answers2

0

Some examples from the docker documentation, before the colon is the container port, and after the colon is the container host port.

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

-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.

-p 8080:80/udp
Map UDP port 80 in the container to port 8080 on the Docker host.

-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.

More information at, https://docs.docker.com/config/containers/container-networking/

Shaqil Ismail
  • 1,794
  • 1
  • 4
  • 5
-1

If you want to access port 3306 in the host from your docker container, you can use

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.

CefBoud
  • 214
  • 2
  • 4