V
V
Vyacheslav Grachunov2018-04-13 21:51:29
linux
Vyacheslav Grachunov, 2018-04-13 21:51:29

How to forward a port from a host inside a docker container?

You need to connect from inside the docker container to the MySQL database on another server. At the same time, an ssh tunnel is forwarded on the host and the database is available on port 3307.
Inside the MySQL container, the server containing the database with Federated tables referring to the remote server, but does not work, because the container has its own 127.0.0.1 and the port is not forwarded to it. There is no SSH in the container, so just taking it and forwarding it will not work. You need to somehow forward 127.0.0.1:3307 from the host to the container. And also on 127.0.0.1, because on a remote server bind-address=127.0.0.1 and cannot be changed.
What can you suggest? The whole Internet got out.
I will describe in more detail:
There are 2 servers. For example, 1.1.1.1 and 2.2.2.2
on 1.1.1.1 production server, it has 2 databases, conditionally fastdata and userdata.
In the first, the data required by all users is updated almost every second. In the second, user data is also frequently updated, but not so.
On 2.2.2.2 test server. There's a whole lot there. Incl. as many as 2 MySQL servers. One on the host, the other in the container. Total:
1) Server on the host, on the standard port 3306
2) Also port forwarded from 1.1.1.1 - bound to 3307
3) Base in the container (MariaDB 10.1
. the server should be a copy of the production database, but with other users (test). Those. the userdata base is there, but fastdata should be. absolutely identical to the base from the sale at any time. Assumed solution via Federated tables, i.e. looks like that:

ENGINE=FEDERATED DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci CONNECTION='mysql://root:[email protected]:3307/fastdata/table1';

But doesn't work. Because 127.0.0.1 in the container is naturally its own and no production base is bound there. Connect to 172.17.0.3 inside a container where the host cannot be accessed because in the database on the prod bind-address=127.0.0.1 and stupidly refuse to connect. I thought to run the container with --net='host' - then it will be on the same network as the host, but then the container base will be on port 3306, and it is already occupied by the local base, but I also did not understand how to change the port inside the container. Here is such a hitch.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vyacheslav Grachunov, 2018-04-14
@Qwentor

In general, the solution turned out to be simple:
Those. as I expected via --net=host - this way the container works on the host network and there is no need to redirect anything. /etc/mysql is really not forwarded to change my.cnf, but /etc/mysql/conf.d is calmly forwarded, and the mariadb.cnf file is in it, in which the port change to 3308 is registered.
The problem is solved.

H
Hikmat Abdunabiev, 2018-04-14
@Khikmat

But what if you specify the ip address of the network card to which the ssh tunnel is forwarded?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question