简体   繁体   English

访问 Dockerfile 中的 container_name(来自 docker-compose)

[英]Access container_name in Dockerfile (from docker-compose)

I have setup a docker-compose project which are creating multiple images:我已经设置了一个 docker-compose 项目,它正在创建多个图像:

cache_server:
    image: current_timezone/full-supervisord-cache-server:1.00
    container_name: renamed-varnish-cache
    networks:
      - network_frontend
    build:
      context: "./all-services/"
      dockerfile: "./cache-server/Dockerfile.cacheserver.varnish"
      args:
        - DOCKER_CONTAINER_USERNAME=username
    ports:
      - "6081:6081"
      - "6082:6082"

When I use docker-compose up -f file1.yml file2.override.yml I will then get the containers: in the case of above one it will be named: renamed-varnish-cache当我使用docker-compose up -f file1.yml file2.override.yml我将获得容器:在上述情况下,它将被命名为: renamed-varnish-cache

In the corresponding Dockerfile ( ./nginx-proxy/Dockerfile.proxy.nginx ) I want to be able use the container_name property defined in the docker-compose.yml shown above.在相应的 Dockerfile ( ./nginx-proxy/Dockerfile.proxy.nginx ) 中,我希望能够使用上面显示的docker-compose.yml中定义的container_name属性。

When the containers are created I want to update the Varnish configurations inline inside Dockerfile: RUN sed -i "s|webserver_container_name|renamed-varnish-cache|g" /etc/varnish/default.vcl"创建容器后,我想更新 Dockerfile 内的 Varnish 配置: RUN sed -i "s|webserver_container_name|renamed-varnish-cache|g" /etc/varnish/default.vcl"

For instance:例如:

    backend webserver_container_name{
        .host = "webserver_container_name";
        .port = "8080";
    }

To: I anticipate I will have to replace the - with _ for the backend:致:我预计我将不得不将后端的-替换为_

    backend renamed_varnish_cache{
        .host = "renamed-varnish-cache";
        .port = "8080";
    }

Is there a way to receive the docker-compose named items as variables inside Dockerfile?有没有办法接收 docker-compose 命名项目作为Dockerfile 中的变量?

In core Docker, there are two separate concepts.在核心 Docker 中,有两个独立的概念。 An image is a built version of some piece of software packaged together with its dependencies;映像是某些软件及其依赖项打包在一起的构建版本; a container is a running instance of an image.容器是图像的运行实例。 There are separate docker build and docker run commands to build images and launch containers, and you can launch multiple containers from a single image.有单独的docker builddocker run命令来构建镜像和启动容器,您可以从单个镜像启动多个容器。

Docker Compose wraps these concepts. Docker Compose 包装了这些概念。 In particular, the build: block corresponds to the image-build step, and that is what invokes the Dockerfile.特别是build:块对应于 image-build 步骤,这就是调用 Dockerfile 的内容。 None of the other Compose options are available or visible inside the Dockerfile. Dockerfile 中没有其他 Compose 选项可用或可见。 You cannot access the container_name: or environment: variables or volumes: because those don't exist at this point in the build lifecycle;您无法访问container_name:environment: variables 或volumes:因为它们在构建生命周期中此时不存在; you also cannot contact other Compose services from inside the Dockerfile.您也无法从 Dockerfile 内部联系其他 Compose 服务。

It's pretty common to have multiple containers run off the same image if they have largely the same code base but need a different top-level command.如果多个容器具有基本相同的代码库但需要不同的顶级命令,那么让多个容器从同一个映像运行是很常见的。 One example is a Python Django application that needs Celery background workers;一个例子是 Python Django 应用程序需要 Celery 后台工作程序; you'd have the same project structure but a different command for the Celery worker.您将拥有相同的项目结构,但 Celery 工作人员的命令不同。

version: '3.8'
services:
  web:
    build: .
    image: my/django-app
  worker:
    image: my/django-app
    command: celery worker ...

Now with this stack you can docker-compose build to build the one image, and then run docker-compose up to launch both containers from that image.现在使用此堆栈,您可以docker-compose build以构建一个映像,然后运行docker-compose up以从该映像启动两个容器。 (During the build you can't know what the container names will be, and there will be two container names so you can't just use one in the Dockerfile.) (在构建期间,您无法知道容器名称是什么,并且会有两个容器名称,因此您不能只在 Dockerfile 中使用一个。)

At a design level, this means that you often can't include configuration-type settings in the image itself (other containers' hostnames, user IDs for host-shared filesystems).在设计级别,这意味着您通常不能在映像本身中包含配置类型设置(其他容器的主机名、主机共享文件系统的用户 ID)。 If your application lets you specify these things as environment variables, that's the easiest option.如果您的应用程序允许您将这些内容指定为环境变量,那么这是最简单的选择。 You can use bind mounts ( volumes: ) to inject whole config files.您可以使用绑定挂载 ( volumes: :) 来注入整个配置文件。 If neither of these things work for you, you can use an entrypoint script to rewrite the config file .如果这些都不适合您,您可以使用入口点脚本来重写配置文件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 docker-compose 没有从环境变量中设置 container_name - docker-compose is not setting container_name from environment variable 如何在 docker-compose 中设置 container_name 以进行集群部署? - How to set container_name in docker-compose for swarm deployment? 尽管docker-compose.yml中的container_name不同,但docker-compose仍会重新创建运行中的容器 - docker-compose recreates a running container despite having a different container_name in the docker-compose.yml 如何让`docker-compose up $container_name` 不仅附加到 $container_name 还附加到它所依赖的所有容器? - How to make `docker-compose up $container_name` to attache not only to $container_name but to all containers it depends on? docker-compose:使用相同的container_name在多个项目之间共享容器 - docker-compose: Sharing container between multiple projects by using the same container_name docker 容器从 Dockerfile 运行,但不是从 docker-compose - docker container runs from Dockerfile but not from docker-compose 从 Dockerfile 到 Docker-compose - From Dockerfile to Docker-compose "docker-compose.yml 容器名和主机名" - docker-compose.yml container_name and hostname Docker 容器从 Dockerfile 工作,但得到下一个:从 docker-compose 容器中找不到 - Docker container works from Dockerfile but get next: not found from docker-compose container docker-compose 从容器内部获取容器名称 - docker-compose get Container Name from inside of the Container
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM