49

I want to change the default exposed port for mysql docker container, but if i try to use this command:

 docker run --detach --name=test-mysql -p 52000:52000  --env="MYSQL_ROOT_PASSWORD=mypassword" mysql

It does not work. mysql -uroot -pmypassword -h 127.0.0.1 -P 52000 Warning: Using a password on the command line interface can be insecure. ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0

If I use the standard port 3306:3306 then it works fine, but i want change the port. Is it possibile?

I had already tried -p 52000:3600 , but i have always gotten:

mysql -uroot -pmypassword -h 127.0.0.1 -P 52000 Warning: Using a password on the command line interface can be insecure. ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0

Jim Aho
  • 9,932
  • 15
  • 56
  • 87
lbottoni
  • 962
  • 2
  • 14
  • 26

2 Answers2

58

You need to map the container-port 3306 on the prefered TCP port (of your server):

-p <host_port>:<container_port> (map container_port xx on host_port yy)

So for your mysql

docker run --detach --name=test-mysql -p 52000:3306  --env="MYSQL_ROOT_PASSWORD=mypassword" mysql
lvthillo
  • 28,263
  • 13
  • 94
  • 127
  • $ docker run --detach --name=test-mysql -p 52000:3306 --env="MYSQL_ROOT_PASSWORD=mypassword" mysql 0ce69064b76e48088a9886ee7d084775de9afbc30d1db436ca928bc3fdff707b mymachine@MACHINE C:\wamp\bin\mysql\mysql5.6.17\bin $ mysql -uroot -pmypassword -h 127.0.0.1 -P 52000 Warning: Using a password on the command line interface can be insecure. ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0 not work, I had already tried – lbottoni Jan 13 '17 at 14:42
  • @user1326966 Probably your IP is wrong. Your mapping 52000 on the public ip of your server. This means that you can't use the 127.0.0.1. (only from inside the container you can access it with 127.0.0.1:3306. But to access your container from the outside you'll need server-ip:52000 which will internally resolve to your container-ip:3306 – lvthillo Jan 13 '17 at 14:46
  • @user1326966 If you really want to adapt the container-port (I don't understand why) but than you can edit the dockerfile and expose your port instead of 3306. Than you can access it inside the container with -P 52000. – lvthillo Jan 13 '17 at 14:50
22

there is also a second option:

don't map a port to another port but let mysql itself run directly on another port using the MYSQL_TCP_PORT-variable.

example:

docker run --detach --name=test-mysql --env="MYSQL_TCP_PORT=52000" mysql

anion
  • 1,516
  • 1
  • 21
  • 35