如何在本地主机上运行两个或多个应用程序

时间:2019-07-22 10:47:02

标签: docker docker-compose

我正在开始使用docker和docker-compose。我有教程,我使用docker-compose.yml文件在本地计算机上运行我的一个站点。

通过转到http://localhost

,我可以看到我的网站正在运行

我现在的问题是试图运行多个站点。如果我的一个站点正在运行,而我尝试使用docker-compose up -d运行另一个站点,则会收到以下错误。

$ docker-compose up -d
Creating network "exampleCOM_default" with driver "bridge"
Creating exampleCOMphp-fpm ...
Creating exampleCOMmariadb ... error

ERROR: for exampleCOMmariadb  Cannot start service db: driver failed     programming external connectivity on endpoint exampleCOMmariadb     (999572f33113c9fce034b4ed72aaCreating exampleCOMphp-fpm ... done

轻松分配     创建exampleCOMnginx ...错误

ERROR: for exampleCOMnginx  Cannot start service nginx: driver failed programming external connectivity on endpoint exampleCOMnginx (9dc04f8b06825d7ff535afb1101933be7435c68f4350f845c756fc93e1a0322c): Bind for 0.0.0.0:443 failed: port is already allocated

ERROR: for db  Cannot start service db: driver failed programming external connectivity on endpoint exampleCOMmariadb (999572f33113c9fce034b4ed72aa072708f6f477eb2af8ad614c0126ca457b64): Bind for 0.0.0.0:3306 failed: port is already allocated

ERROR: for nginx  Cannot start service nginx: driver failed programming external connectivity on endpoint exampleCOMnginx (9dc04f8b06825d7ff535afb1101933be7435c68f4350f845c756fc93e1a0322c): Bind for 0.0.0.0:443 failed: port is already allocated

在启动项目时遇到错误。

这是我的docker-compose文件。我正在使用LEMP堆栈(PHP,NGINX,MARIADB)

version: '3'

services:
    db:
      container_name: ${SITE_NAME}_mariadb
      build:
        context: ./mariadb
      volumes:
          - ./mariadb/scripts:/docker-entrypoint-initdb.d
          - ./.data/db:/var/lib/mysql
          - ./logs/mariadb:/var/log/mysql
      environment:
          MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
          MYSQL_DATABASE: ${MYSQL_DATABASE}
          MYSQL_USER: ${MYSQL_USER}
          MYSQL_PASSWORD: ${MYSQL_PASSWORD}
      ports:
          - '${MYSQL_PORT:-3306}:3306'
      command:
          'mysqld --innodb-flush-method=fsync'
      networks:
        - default
      restart: always
    nginx:
      container_name: ${SITE_NAME}_nginx
      build:
        context: ./nginx
        args:
          - 'php-fpm'
          - '9000'
      volumes:
        - ${APP_PATH}:/var/www/app
        - ./logs/nginx/:/var/log/nginx
      ports:
        - "80:80"
        - "443:443"
      depends_on:
        - php-fpm
      networks:
        - default
      restart: always
    php-fpm:
      container_name: ${SITE_NAME}_php-fpm
      build:
          context: ./php7-fpm
          args:
              TIMEZONE: ${TIMEZONE}
      volumes:
          - ${APP_PATH}:/var/www/app
          - ./php7-fpm/config/php.ini:/usr/local/etc/php/php.ini
      environment:
          DB_HOST: db
          DB_PORT: 3306
          DB_DATABASE: ${MYSQL_DATABASE}
          DB_USERNAME: ${MYSQL_USER}
          DB_PASSWORD: ${MYSQL_PASSWORD}
      networks:
        - default
      restart: always
networks:
  default:
    driver: bridge

2 个答案:

答案 0 :(得分:0)

即使docker-compose创建了专用网络,映射到的主机端口也阻止您启动服务的另一个实例。

您可以通过使用docker-compose分配的随机主机端口来解决此问题。

docker-compose中的端口条目为

ports
 host_port:container_port

如果指定,则仅随机分配容器端口主机端口。参见here

答案 1 :(得分:0)

您可以提供范围内的host_port值。

在下面的示例中,我运行了nginx容器并启动了多个nginx容器,这些容器会根据范围值[30000-30005]自动暴露给主机端口。

 Command: 

        docker run -p 30000-30005:80 --name nginx1 -d nginx

 Output:

 9083d5fc97e0   nginx ... Up 2 seconds    0.0.0.0:30001->80/tcp   nginx1
 f2f9de1efd8c   nginx ... Up 24 seconds   0.0.0.0:30000->80/tcp   nginx