I'm trying to connect my app running in a docker container to a database through a SSH Tunnel. My dockerfile is something like this:
# Alpine, PHP7.4, Nginx FROM richarvey/nginx-php-fpm:1.9.1 EXPOSE 8080 ENV WEBROOT /var/www/html/public/ RUN docker-php-ext-install tokenizer xml pcntl RUN echo "@community http://dl-4.alpinelinux.org/alpine/v3.6/community/" >> /etc/apk/repositories \ && apk add --update autossh@community \ && rm -rf /var/lib/apt/lists/* COPY . /var/www/html/ RUN chmod 600 /ssh_key RUN ssh -o StrictHostKeyChecking=no -o ServerAliveInterval=5 -o ServerAliveCountMax=1 -i /ssh_key -fN -4 -L 3317:127.0.0.1:3306 user@host RUN php artisan migrate --force
When building the image (
docker build -t project .) the connection to the database (from the migrations) fail and from the server, the auth logs are:
May 23 21:18:04 ultron sshd: pam_unix(sshd:session): session opened for user <user> by (uid=0) May 23 21:18:04 ultron systemd-logind: New session 126 of user <user>. May 23 21:18:05 ultron sshd: pam_unix(sshd:session): session closed for user <user> May 23 21:18:05 ultron systemd-logind: Session 126 logged out. Waiting for processes to exit. May 23 21:18:05 ultron systemd-logind: Removed session 126.
The tunnel and database connection on my local computer are working fine, furthermore if I don't put the
ssh command in background (the
-f flag) it does not disconnect, but of course the rest of the Dockerfile if not processed.
I tried to put the
ssh command on a detached screen with
screen -dm <ssh comand> or with
autossh, but with these two "alternatives" it does not even connect to the host (the
auth.log on the server does not receive the ssh connection).
Thank you in advance.