Rider_BY
Rider_BY

Reputation: 1129

How to solve "port is already allocated" error when running two docker-compose files at the same time?

I have many project based on docker-compose files with different settings. If I wanna start another project I will docker-compose stop current project and docker-compose up another. But my issue sounds as: how to start 2 or more docker-compose images with any projects at the same time ? My OS linux ubuntu. My docker-compose look as:

application:
    build: code
    volumes:
        - ./mp:/var/www/mp
        - ./logs/mp:/var/www/mp/app/logs
    tty: true
db:
    image: mysql
    ports:
        - 3306:3306
    environment:
        MYSQL_ROOT_PASSWORD: root
        MYSQL_DATABASE: mp-DB
        MYSQL_USER: root
        MYSQL_PASSWORD: root
php:
    build: php-fpm
    ports:
        - 9000:9000
    volumes_from:
        - application
    links:
        - db
nginx:
    build: nginx
    ports:
        - 80:80
    links:
        - php
    volumes_from:
        - application
    volumes:
        - ./logs/nginx/:/var/log/nginx
elk:
    image: willdurand/elk
    ports:
        - 81:80
    volumes:
        - ./elk/logstash:/etc/logstash
        - ./elk/logstash/patterns:/opt/logstash/patterns
    volumes_from:
        - application
        - php
        - nginx

If I try run another project I got error

'driver failed programming external connectivity on endpoint mpdockerenv_db_1 Bind for 0.0.0.0:3306 failed: port is already allocate'

I think I should will to forward ports from containers with different ports but I don't know how to done it.

Upvotes: 2

Views: 4342

Answers (1)

Joaquin Javi
Joaquin Javi

Reputation: 876

docker-compose is a tool for handle exactly build for your question .

I mean imagine you have a complex project and you need to organize and have more clean way to handle system environments.

Usually in a docker-compose.yml file you can have as meany docker images you will use.

f.example a partial file i use :

mongo:
image: mongo:latest
ports:
 - "3002:27017"
environment:
MONGODB_DATABASE: "meteor-console-dev"

php-fpm-dev:
image: jokediaz/php-fpm.5.6-laravel
volumes:
 - ./repos/datamigration:/usr/share/nginx/html/datamigration
 - ./unixsock:/sock
 - ./config/php-fpm-5.6/:/usr/local/etc/php
links:
 - mongo

So if we take a look the following rules :

  • ports : you can map external port output : internal docker port

  • environment : set a system environment var

  • volumes : you're mapping a directory from your Filesystem : inside to docker container ( so even if you destroy de container these data will be presisted )

  • links : docker internally has a little internal net DNS management , so if you type : docker network inspect bridge command you will see Subnet range and also a gateway ( usually 172.17.0.1 ) so that mean your running applications inside docker can see each others internally thought this ip , if you put link and the name of the image entry , the docker (little DNS) can map from one container ip to other.

Another point is to make a docker-compose up when docker-compose is modified for re-create all changes you did , a good idea is to make docker-compose down before ( be carefully this will be delete any un-mapped volume ) to clear and free space.

Take a look at docker-compose file reference :

docker-compose file reference

in your case

 ports:
    - 3306:3306

the port 3306 is in use by host . ( probably you have a running instance of mysql in your system so port is in use )

So simply change to other free port :

 ports:
    - 3308:3306

Upvotes: 3

Related Questions