我正在尝试使用 docker 容器来设置到远程数据库的 SSH 隧道,该远程数据库只能通过 SSH 访问。我有一个包含多个容器的 docker 网络,并希望使数据库可用于网络中的所有容器。
SSH 容器的 Dockerfile 如下所示:
FROM debian:stable
RUN apt-get update && apt-get -y --force-yes install openssh-client autossh postgresql-client
COPY .ssh /root/.ssh
RUN chown root:root /root/.ssh/config
EXPOSE 12345
ENTRYPOINT ["/usr/bin/autossh", "-M", "0", "-v", "-T", "-N", "-4", "-L", "12345:localhost:1234", "user@remotedb" ]
.ssh 目录里面是我的密钥和配置文件,看起来像这样:
Host remotedb
StrictHostKeyChecking no
ServerAliveInterval 30
ServerAliveCountMax 3
隧道本身在这个容器上工作,这意味着我可以从它内部以 localhost:12345 的身份访问数据库。
现在我也想从同一网络中的其他容器访问它。
我的 docker-compose.yml 看起来像这样(我注释掉了一些试验):
version: '2'
networks:
my_network:
driver: bridge
ipam:
config:
- subnet: 10.12.0.0/16
gateway: 10.12.0.1
services:
service_1:
image: my/image:alias
volumes:
- somevolume
# links:
# - my_ssh
ports:
- "8080"
environment:
ENV1: blabla
networks:
my_network:
ipv4_address: 10.12.0.12
my_ssh:
build:
context: ./dir_with_Dockerfile
# ports:
# - "23456:12345"
expose:
- "12345"
networks:
my_network:
ipv4_address: 10.12.0.13
我尝试使用主机名“my_ssh”、ipv4_address、“localhost”以及端口 12345 和 23456 从 service_1 内部访问远程数据库。这些组合都不起作用。我哪里错了?
或者我还能如何实现从容器到远程数据库的永久连接?