I already have a Mysql running on host at port 3306.
version: "3"
services:
db:
container_name: db_mysql
image: mysql
ports:
- "3309:3306"
environment:
- MYSQL_ROOT_PASSWORD=123456
The container msyql port 3306 is mapped to host port 3309. But error:
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
docker-compose up
Can't start server: Bind on TCP/IP port: Address already in use
[ERROR] Do you already have another mysqld server running on port: 3306 ?
Is container mysql port 3306 still using the host port 3306?
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
103f1e16c782 mysql "docker-entrypoint.s…" 3 minutes ago Exited (1) 3 minutes ago db_mysql
If the ports are commented out, same error.
version: "3"
services:
db:
container_name: db_mysql
image: mysql
#ports:
#- "3309:3306"
environment:
- MYSQL_ROOT_PASSWORD=123456
UPDATE
After stopping the Mysql on host, it worked. This means that the Docker container (mysql) uses the host port 3306?
2
Answers
I’m going to guess that the default bridge network is mapping directly to the same network that the local mysql server is using. Thus you have a port conflict when starting the container. Add a docker network and use that.
You can get a list of the networks using
docker network ls
You can then explore the settings of each docker network using
docker network inspect <name>
where name is one of the network names you get from the ls command. There are a number of built-in networks like bridge. When you don’t specify a network, bridge is what will be used (assuming there is no other local configuration made to have changed this).Specify a network for use in your docker-compose orchestration explicitly, which will create a network for use. Frequently you will want more than one network for orchestration of multiple containers, as in the scenario where you have some application servers, a reverse proxy and a backend database like mysql.
Docker compose run command does not create any of the ports specified in the service configuration. This prevents port collisions with already-open ports. If you do want the service’s ports to be created and mapped to the host, specify the –service-ports
$docker compose run --service-ports SERVICE [COMMAND] [ARGS...]
https://docs.docker.com/engine/reference/commandline/compose_run/