简体   繁体   中英

Docker with Multiple Mysql Container not working

Docker newbie here.

What I'm trying to achieve is to run multiple MySQL containers with docker compose.

This is my docker-compose.yml:

version: '2'
services:
  mysql:
    build: ./docker-configs/mysql
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: admin
    volumes:
      - ./data/mysql:/var/lib/mysql
      - ./data/init.d:/docker-entrypoint-initdb.d
  mysql2:
    build: ./docker-configs/mysql2
    ports:
      - "3306:3307"
    environment:
      MYSQL_ROOT_PASSWORD: admin
    volumes:
      - ./data/mysql2:/var/lib/mysql2
      - ./data/init.d:/docker-entrypoint-initdb.d  
  nginx-proxy:
     image: jwilder/nginx-proxy
     volumes:
        - /var/run/docker.sock:/tmp/docker.sock:ro
        - /etc/localtime:/etc/localtime:ro
        - ./docker-configs/nginx-proxy/certs:/etc/nginx/certs
     ports:
        - '80:80'
        - '443:443'

But when I run docker-compose up -d, it shows me the error below:

ERROR: for consultingdocker_mysql2_1 Cannot start service mysql2: driver failed programming external connectivity on endpoint consultingdocker_mysql2_1 (7eac3d28093db7be7b5c81495ee652f6a1df8208388d33df668f1732a118481a): Bind for 0.0.0.0:3306 failed: port is already allocated

ERROR: for mysql2 Cannot start service mysql2: driver failed programming external connectivity on endpoint consultingdocker_mysql2_1 (7eac3d28093db7be7b5c81495ee652f6a1df8208388d33df668f1732a118481a): Bind for 0.0.0.0:3306 failed: port is already allocated ERROR: Encountered errors while bringing up the project.

Please let me know what am I missing here?

The "port" array configuration is inverted. If you write this:

port:
  - "3306:3307"

it means "bind port 3306 on the host network to port 3307 in the container network".

While you want exactly the opposite.

Swap the numbers and it'll work

Port 3306 is already in use on the host by the container mysql , hence you cannot allocate the same port on the host for mysql2 container. Change your mysql2 service config to something as below and it should work -

  mysql2:
    build: ./docker-configs/mysql2
    ports:
      - "3307:3307"
    environment:
      MYSQL_ROOT_PASSWORD: admin
    volumes:
      - ./data/mysql2:/var/lib/mysql2
      - ./data/init.d:/docker-entrypoint-initdb.d  

Now your mysql2 service will be available on port 3307 on the host.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM