如何实现Dockerfile继承? [英] How to implement Dockerfile inheritance?

查看:174
本文介绍了如何实现Dockerfile继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Dockerfile可安装生产&测试依赖项.我想为测试使用单独的映像,因此生产映像较小,而没有太多的代码重复.也许有类似 FROM 的语句引用其他Dockerfile吗?

I have a Dockerfile which installs production & test dependencies. I want to have separate image for tests, so production image is smaller, without to much code duplication. Maybe there is something like FROM statement for referencing other Dockerfiles?

Dockerfile有以下几行:

Dockerfile has following lines:

ADD requirements.txt ${PROJECT_DIR}/requirements.txt
RUN pip install --no-cache --process-dependency-links --trusted-host github.com -r requirements.txt
ADD requirements-test.txt ${PROJECT_DIR}/requirements-test.txt
RUN pip install --no-cache --process-dependency-links --trusted-host github.com -r requirements-test.txt

项目的前两个安装依赖关系,后两个-用于测试的安装依赖项(pytest,pylint等).

First two install depencencies for project, second two - install dependencies for testing (pytest, pylint, etc.).

我也有docker-compose来启动数据库,redis缓存等.这是我运行服务和运行测试的方式:

I also have docker-compose that starts database, redis cache, etc. This is how I run service and run tests:

run:
    docker-compose -f docker-compose.yaml run
test:
    docker-compose -f docker-compose-dev.yaml run py.test tests/

在两个 docker-compose.yaml 内都有我的容器的此构建配置:

Inside both docker-compose.yaml has this build config for my container:

build:
  context: .
  dockerfile: ./Dockerfile

因此,我可以从 docker-compose.yaml 中引用不同的Dockerfile,但我不希望它们成为只有两行不同的完整副本.

So, I could reference different Dockerfiles from my docker-compose.yaml, but I don't want them to be complete copies that have only two lines difference.

推荐答案

FROM 是这里的解决方法.生产和开发映像应该不会有太多差异,或者根本不会有太大差异,因为这种想法是部署已开发的映像.以我的经验,从生产映像开始并从中扩展开发映像比较容易,因为开发需要更多的软件,例如调试器,编译器等.

FROM is the way to go here. Production and Development images should not differ too much, or at all, since the idea is to deploy what you have developed. In my experience it is easier to start with a production image and extend a development image from that, since development requires more software like debuggers, compilers, etc.

由于评论:

dockerfile 中, FROM 指令带有一个图像标签,因此类似 ubuntu:latest 的东西,然后docker首先会尝试找到它本地映像,然后尝试从存储库中提取映像.

Within a dockerfile the FROM directive takes an image tag, so something like ubuntu:latest and than docker will first try to find that image locally and than try to pull it from a repo, if not.

如果没有存储库,则可以通过运行以下命令在本地创建基本映像:

In case you do not have a repo, you can create the base image locally by running:

docker build --tag vendor/production:0.0.1 .

将编译图像的内容.比你可以写:

what will compile the image. Than you can write:

FROM vendor/production:0.0.1

在您的 dev-dockerfile 中并进行编译.我通常会为所有编译内容创建一个很小的bash脚本.

in your dev-dockerfile and compile this. I usually create a tiny bash script for all the compilation stuff.

这篇关于如何实现Dockerfile继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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