Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

477
Views
How to connect to a MySQL Docker Container via SSH?

When I try to tunnel via SSH to the Host Mashine (vServer) and then try to connect via the internal docker Container-IP then I can't connect to MySQL.

This is my docker-compose file.

version: '2'
services:
  mysql:
    build: ./mysql
    environment:
      MYSQL_ROOT_PASSWORD: test
    volumes:
      - ./db:/var/lib/mysql

The only solution I found was to forward the MySQL-Port of the mysql container to the Host-Mashine.

version: '2'
services:
  mysql:
    build: ./mysql
    environment:
      MYSQL_ROOT_PASSWORD: test
    volumes:
      - ./db:/var/lib/mysql
   ports:
     - 3306:3306

Then I am able to connect via the Host IP to MySQL but this is without SSH its direct via TCP and the port. This is a No-Go for me to bring the MySQL Service into the internet.

Reasons can be found here https://security.stackexchange.com/questions/63881/is-it-not-safe-to-open-mysqls-port-to-the-internet why it is not a good practice to bring your mysql port into the internet.


So what is a good practice to connect to my docker mysql container with SSH but keep the mysql ports closed?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

One simple way is to bind the MySQL port only to the localhost address. That assumes the host has a mysql client available outside of Docker.

ports:
  - 127.0.0.1:3306:3306

You could also omit the ports section completely (no port binding at all), and use the mysql client that's already inside the container.

docker-compose exec mysql bash

Then run the mysql command inside the container to do whatever queries you want to do.

over 4 years ago · Santiago Trujillo Report

0

An easy way to achieve this is to forward the ssh port of the docker conatiner to some port on your host, i.e.

ports:
   - 22:<some free host port>

and then access the container via ssh to the host port you used. Note, that it is a bad idea to use port 22, since that will cause a conflict with the ssh server running on your host.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!