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.
# 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.
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. ..
docker run -p 3307:3307 db
This works fine. If so, does it work if I specify the port with the command?
ports:
- ${DB_PORT}:3307 #Port to access: Port in the container
command: --port 3307
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