docker-compose up -d - 使用错误的名称构建“$ @”构建图像

问题描述 投票:1回答:2

docker-compose up -d - 使用错误的名称构建“$ @”构建图像

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

denpal_cli:latest
denpal_php:latest
denpal_nginx:latest

不幸的是,docker图像会产生以下结果

workspace_php:latest
workspace_nginx:latest
denpal:latest

这样我就不能用他们正确的名字将它们推送到Docker酒吧,或者我必须做一个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等。

这可能吗?

docker docker-compose
2个回答
1
投票

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


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:最新

Container_name:my-apache

© www.soinside.com 2019 - 2024. All rights reserved.