在Dockerfile中访问container_name(来自docker-compose) [英] Access container_name in Dockerfile (from docker-compose)

查看:628
本文介绍了在Dockerfile中访问container_name(来自docker-compose)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了一个docker-compose项目,该项目正在创建多个映像:

I have setup a docker-compose project which are creating multiple images:

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"

当我使用 docker-compose up -f file1.yml file2.override.yml 然后我将得到这些容器:如果是上面的容器,它将被命名为: renamed-varnish-cache

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

在相应的Dockerfile( ./ nginx-proxy / Dockerfile.proxy.nginx )中,我希望能够使用 container_name docker-compose.yml 中定义的$ c>属性。

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.

创建容器时,我想更新Varnish在Dockerfile中内联配置: RUN sed -i s | webserver_container_name |重命名清漆缓存| g; /etc/varnish/default.vcl;

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"

例如:

    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";
    }

有没有办法在Dockerfile中以变量的形式接收docker-compose命名项?

Is there a way to receive the docker-compose named items as variables inside Dockerfile?

推荐答案

在核心Docker中,有两个独立的概念。 image 是与其相关性打包在一起的某些软件的构建版本。 容器是图像的运行实例。有单独的 docker build docker run 命令来构建映像和启动容器,您可以从

In core Docker, there are two separate concepts. 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 Compose包含了这些概念。特别是, build:块对应于映像构建步骤,这就是调用Dockerfile的步骤。 其他Compose选项在Dockerfile中都不可用或不可见。您无法访问 container_name: environment:变量或 volumes ,因为这些变量在构建生命周期中目前不存在;您也无法从Dockerfile内部联系其他Compose服务。

Docker Compose wraps these concepts. In particular, the build: block corresponds to the image-build step, and that is what invokes the Dockerfile. None of the other Compose options are available or visible inside the Dockerfile. You cannot access the container_name: or environment: variables or volumes: because those don't exist at this point in the build lifecycle; you also cannot contact other Compose services from inside the Dockerfile.

如果多个容器具有基本相同的代码库但需要不同的顶层,则使多个容器在同一映像上运行是很常见的命令。一个示例是需要Celery后台工作者的Python Django应用程序。您将拥有相同的项目结构,但对于Celery工人却使用了不同的命令。

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; you'd have the same project structure but a different command for the Celery worker.

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

现在有了这个堆栈,您可以 docker-compose build 来构建一个映像,然后运行 docker-compose up 从该映像启动两个容器。 (在构建期间,您不知道容器名称是什么,并且将有两个容器名称,因此您不能仅在Dockerfile中使用一个。)

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. (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.)

在设计时级别,这意味着您通常不能在映像本身中包括配置类型设置(其他容器的主机名,主机共享文件系统的用户ID)。如果您的应用程序允许您将这些内容指定为环境变量,那是最简单的选择。您可以使用绑定挂载( volumes:)注入整个配置文件。如果以上两种方法都不适合您,则可以使用入口点脚本重写配置文件

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). 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. If neither of these things work for you, you can use an entrypoint script to rewrite the config file.

这篇关于在Dockerfile中访问container_name(来自docker-compose)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆