[Docker] Does not connect to MySQL workbench ... DB container port forwarding settings

A problem I encountered while playing with Docker personally. A note on the solution when you want to change the port but it doesn't work. Please let me know if there is a good way.

I want to change the port forwarding of a MySQL container

# docker-compose.yml
version: "3"
services:
  db:
    build:
      context: ./docker/mysql
    container_name: db
    volumes:
      - db-store:/var/lib/mysql
      - db-logs-store:/var/log/mysql
      - ./docker/mysql/my.cnf:/etc/mysql/conf.d/my.cnf
    environment:
      - MYSQL_DATABASE=${DB_NAME:-JETmysql}
      - MYSQL_USER=${DB_USERNAME}
      - MYSQL_PASSWORD=${DB_PASS}
      - MYSQL_ROOT_PASSWORD=${DB_PASS}
      - TZ=${TZ:-Asia/Tokyo}
    ports:
      - ${DB_PORT}:3307 #Port to access: Port in the container
volumes:
  db-logs-store:
  db-store:
  redis-store:

# .env
DB_CONNECTION=mysql
DB_HOST=db #0.0.0.0
DB_PORT=3307

Port forwarding is a communication request that comes to a specific port. I will transfer it. Normally, 3306 is used for MySQL, but 3307 is used because you want to avoid conflicts. I thought I would apply it.

Define the port in the ports part of the above YML. The $ {DB_PORT} part is pulled from the env file. The left side is the port forwarding destination, and the right side is the port number used inside the Docker container. It means "when a request comes to local port 3307, connect it to 3307 in the container". Because the container is positioned as a network isolated from the local terminal, It is recognized that there is a port number even inside the container.

I was wondering if I could define it only here, but I was hit by an event that I couldn't connect for some reason other than 3306. This time I was trying to connect a GUI called MySQL Workbench.

Screen Shot 2020-10-31 at 13.55.00.png

docker-compose ps

           Name                           Command               State                Ports
-------------------------------------------------------------------------------------------------------
db                              docker-entrypoint.sh --por ...   Up       3306/tcp, 33060/tcp,
                                                                          0.0.0.0:3307->3307/tcp

Looks like it's defined. ..

Port specification when docker run

docker run -p 3307:3307 db

This works fine. If so, does it work if I specify the port with the command?

Add command to yml


    ports:
      - ${DB_PORT}:3307 #Port to access: Port in the container
    command: --port 3307

Screen Shot 2020-10-31 at 13.56.49.png

It seems that I was able to connect successfully. I don't know why I need a port specification command, It seems to work with this.

Recommended Posts

[Docker] Does not connect to MySQL workbench ... DB container port forwarding settings
Connect from Java to PostgreSQL
[Docker] Does not connect to MySQL workbench ... DB container port forwarding settings
MySQL container does not start in Docker
Docker for Windows: MySQL container does not start when migrating from Hyper-v to WSL2
Docker test DB does not start
WSL2 + Docker Desktop for Windows does not launch only MySQL container
I was addicted to not being able to connect to AWS-S3 from the Docker container
How to solve when you cannot connect to DB with a new container because the port is assigned to the existing docker container
[Java] Connect to MySQL
What to check when rails db: migration does not pass
bundle install does not install mysql2
Connect to MySQL 8 with Java
[Android] Connect to MySQL (unfinished)
If the DB container does not start with Initializing database files
Connect from local to remote host JMX via SSH port forwarding