docker-compose up -d --build“ $ @”用错误的名字建立镜像

时间:2019-04-01 12:43:36

标签: docker docker-compose

docker-compose up -d --build“ $ @”以错误的名称构建映像

我想用以下名称构建Docker映像:

denpal_cli:latest
denpal_php:latest
denpal_nginx:latest

不幸的是,docker images会产生以下结果:

workspace_php:latest
workspace_nginx:latest
denpal:latest

通过这种方式,我无法使用其专有名称将它们推送到Docker pub,否则我必须做一个hackey:

docker tag denpal:latest denpal_cli:latest
docker tag workspace_php:latest denpal_php:latest
docker tag workspace_nginx:latest denpal_nginx:latest

Docker ps将其显示为结果:

workspace_cli_1       /sbin/tini -- /lagoon/entr ...   Up      9000/tcp
workspace_mariadb_1   /sbin/tini -- /lagoon/entr ...   Up      0.0.0.0:32768->3306/tcp
workspace_nginx_1     /sbin/tini -- /lagoon/entr ...   Up      8080/tcp
workspace_php_1       /sbin/tini -- /lagoon/entr ...   Up      9000/tcp
workspace_redis_1     /sbin/tini -- /lagoon/entr ...   Up      6379/tcp
workspace_solr_1      /sbin/tini -- /lagoon/entr ...   Up      0.0.0.0:32769->8983/tcp
workspace_varnish_1   /sbin/tini -- /lagoon/entr ...   Up      8080/tcp

这是docker-compose文件:

version: '2.3'

x-test-project:
  # Project name (leave `&test-project` when you edit this)
  &test-project denpal

x-volumes:
  &default-volumes
    # Define all volumes you would like to have real-time mounted into the docker containers
    volumes:
      - .:/app:delegated

x-environment:
  &default-environment
    TEST_PROJECT: *test-project

x-user:
  &default-user
    # The default user under which the containers should run. Change this if you are on linux and run with another user than id `1000`
    user: '1000'

services:

  cli: # cli container, will be used for executing composer and any local commands (drush, drupal, etc.)
    build:
      context: .
      dockerfile: Dockerfile.cli
    image: *test-project # this image will be reused as `CLI_IMAGE` in subsequent Docker builds
    << : *default-volumes # loads the defined volumes from the top
    user: root
    environment:
      << : *default-environment # loads the defined environment variables from the top

  nginx:
    build:
      context: .
      dockerfile: Dockerfile.nginx
      args:
        CLI_IMAGE: *test-project # Inject the name of the cli image
    << : *default-volumes # loads the defined volumes from the top
    << : *default-user # uses the defined user from top
    depends_on:
      - cli # basically just tells docker-compose to build the cli first
    networks:
      - test-network
      - default

  php:
    build:
      context: .
      dockerfile: Dockerfile.php
      args:
        CLI_IMAGE: *test-project
    << : *default-volumes # loads the defined volumes from the top
    << : *default-user # uses the defined user from top
    depends_on:
      - cli # basically just tells docker-compose to build the cli first
    environment:
      << : *default-environment # loads the defined environment variables from the top


  mariadb:
    image: mariadb-drupal
    ports:
      - "3306" # exposes the port 3306 with a random local port, find it with `docker-compose port mariadb 3306`
    << : *default-user # uses the defined user from top
    environment:
      << : *default-environment

  redis:
    image: redis
    << : *default-user # uses the defined user from top
    environment:
      << : *default-environment

  solr:
    image: solr:6.6-drupal
    << : *default-user # uses the defined user from top
    ports:
      - "8983" # exposes the port 8983 with a random local port, find it with `docker-compose port solr 8983`
    environment:
      << : *default-environment


  varnish:
    image: varnish-drupal
    links:
      - nginx # links varnish to the nginx in this docker-compose project, or it would try to connect to any nginx running in docker
    << : *default-user # uses the defined user from top
    environment:
      << : *default-environment
      VARNISH_BYPASS: "true" # by default we bypass varnish, change to 'false' or remove in order to tell varnish to cache if possible
    networks:
      - test-network
      - default

networks:
  test-network:
    external: true

Workspace是构建所基于的Jenkins目录的名称。

我还希望这些容器以denpal_cli_1,denpal_mariadb_1等名称联机。

这可能吗?

2 个答案:

答案 0 :(得分:1)

默认情况下,compose将使用当前目录名称作为“项目名称”,并将所有图像/服务/卷/网络/等作为前缀。那个名字您可以使用-p选项或通过设置COMPOSE_PROJECT_NAME环境变量来set a project name explicitly。因此,对于您的用例,可以将COMPOSE_PROJECT_NAME设置为denali

答案 1 :(得分:0)

要构建具有特定前缀的图像,应在image上指定它。

要使用任意名称命名容器,请使用container_name配置。

例如,您希望将容器命名为apache,但将图像命名为httpd:

version: '3'

services:
  apache:
    image: apache_2.4:latest (my custom tag)
    container_name: my-apache
    build:
      context: my_dockerfile_folder/
      dockerfile: Dockerfile-apache (The dockerfile which will build from httpd:2.4)
[...]

结果:

图片:apache_2.4:latest

容器名称:my-apache