如何在”docker-compose up”时不启动entrypoint命令

44次阅读
没有评论

问题描述

在使用docker-compose启动多个容器时,希望在运行docker-compose up -d命令时不启动特定容器的entrypoint命令。以下是用户的docker-compose.yml文件:

version: "3"
services:
  httpd:
    image: 'nginx:stable-alpine'
    ports:
      - '80:80'
    volumes:
      - ./laravel:/var/www/html
      - ./.docker-config/nginx/nginx.conf:/etc/nginx/conf.d/default.conf:ro
    depends_on:
      - php
      - mysql
    networks:
      - backstage
  php:
    build:
      context: ./.docker-config/dockerfiles
      dockerfile: php.dockerfile
    volumes:
      - ./laravel:/var/www/html:delegated
    networks:
      - backstage
  mysql:
    image: mysql:5.7
    env_file:
      - ./.docker-config/mysql/mysql.env
    ports:
      - '33060:3306'
    networks:
      - backstage
  composer:
    build:
      context: ./.docker-config/dockerfiles
      dockerfile: composer.dockerfile
    volumes:
      - ./laravel:/var/www/html
    networks:
      - backstage
  artisan:
    build:
      context: ./.docker-config/dockerfiles
      dockerfile: php.dockerfile
    volumes:
      - ./laravel:/var/www/html
    entrypoint: ["php", "/var/www/html/artisan"]
    depends_on:
      - mysql
    networks:
      - backstage
  npm:
    image: node:14-alpine
    working_dir: /var/www/html
    entrypoint: ["npm"]
    volumes:
      - ./laravel:/var/www/html
    networks:
      - backstage
  phpunit:
    build:
      context: ./.docker-config/dockerfiles
      dockerfile: php.dockerfile
    volumes:
      - ./laravel:/var/www/html
    entrypoint: ["vendor/bin/phpunit"]
    networks:
      - backstage

如上所示,用户在phpunit容器中定义了entrypoint,但是他不希望在运行docker-compose up -d时启动phpunit容器的entrypoint命令。

解决方案

请注意以下操作注意版本差异及修改前做好备份。

方案1

你可以通过将不想运行entrypoint命令的服务的规模设置为0来实现。使用docker-compose up --scale <service_name>=0 -d命令,可以阻止phpunit服务的容器启动,具体请参考官方文档
你还可以查看compose profiles,了解如何在docker-compose文件中排除特定服务的更多选项。
以下是具体步骤:
1. 打开终端或命令提示符。
2. 进入包含docker-compose.yml文件的目录。
3. 运行以下命令:

docker-compose up --scale phpunit=0 -d

这将阻止phpunit服务的容器启动。

方案2

使用compose profiles可以更灵活地控制服务的启动。
另一种方法是使用compose profiles来控制服务的启动。compose profiles允许你在docker-compose文件中定义不同的配置选项,并根据需要选择性地启用它们。
以下是具体步骤:
1. 打开docker-compose.yml文件。
2. 在文件顶部添加一个新的profile,例如no-phpunit
3. 在该profile中,将phpunit服务的配置设置为不启用entrypoint命令。例如:

version: "3"
services:
  phpunit:
    entrypoint: null
  1. 保存文件并关闭。
  2. 运行以下命令启动docker-compose,并指定要使用的profile:
docker-compose --profile no-phpunit up -d

这将使用no-phpunit profile启动docker-compose,并阻止phpunit服务的entrypoint命令启动。
请注意,使用compose profiles可以更灵活地控制服务的启动,你可以根据需要定义多个profile,并根据需要选择性地启用它们。

正文完