docker-compose:对多个服务使用多个Dockerfiles [英] docker-compose: using multiple Dockerfiles for multiple services

查看:473
本文介绍了docker-compose:对多个服务使用多个Dockerfiles的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用docker-compose,我想使用不同的Dockerfiles来进行不同的服务构建步骤。 docs 似乎建议将不同的Docker文件放在不同的目录中,但我想他们都在同一个(也许可以区分使用以下约定:Dockerfile.postgres,Dockerfile.main ...)。这是可能的吗?



编辑:我包含这个docker-compose文件的方案:

  main:
build:。
卷:
- 。:/ code
环境:
- DEBUG = true

postgresdb:
extends:
文件:docker-compose.yml
service:main
build:utils / sql /
ports:
- 5432
environment:
- DEBUG = true

其中 postgresdb 的Dockerfile是:

  FROM postgres 

#http://www.slideshare.net/tarkasteve/developerweek-2015 -docker-tutorial
ADD make-db.sh /docker-entrypoint-initdb.d/

主要是:

  FROM python:2.7 

RUN mkdir / code
WORKDIR / code
ADD requirements.txt / code /

运行点安装 - 升级pip
运行点安装-r requirements.txt
添加。 / code /

现在这样工作,但是我想扩展通过调用根据SQL Alchemy构建的模型在数据库中创建表的Python脚本,可以将postgresdb 的Dockerfile(Python脚本称为 python manage.py create_tables )。我想将它添加到db的Dockerfile中,但是由于容器的隔离,我无法使用SQL Alchemy,因为该映像基于 postgres 映像,而不是Python的,它不包含 sqlalchemy 包...



我该怎么办?我试图在 postgresdb 中使用 main 服务,但不幸的是它不携带python及其包,所以我仍然无法编写一个创建Postgres数据库(通过shell脚本)及其表(通过Python脚本)的单个Docker文件。

解决方案由于Docker处理构建上下文的方式,这是不可能的。



您将不得不使用并放置一个 Dockerfile

请参阅: Dockerfile



您实际上需要一个 docker-compose.yml ,看起来像:

  service1:
build:service1

service2:
build:service2

docker-com姿势



更新:



情况 - 虽然我明白你想做什么,为什么我个人不会这样做我自己。隔离是一件好事,有助于管理预期和复杂性。我将执行数据库创建作为基于应用程序的源代码或应用程序容器本身的另一个容器。



或者,您可以查看更多脚本和模板驱动解决方案,例如关闭我没有任何经验,但听到上帝关于) 。



FWIW:分离问题ftw:)


I'm using docker-compose and I'd like to use different Dockerfiles for different services' build steps. The docs seem to suggest to place different Dockerfiles in different directories, but I'd like them all to be in the same one (and perhaps distinguishable using the following convention: Dockerfile.postgres, Dockerfile.main...). Is this possible?

Edit: The scenario I have contains this docker-compose file:

main:
  build: .
  volumes:
    - .:/code
  environment:
    - DEBUG=true

postgresdb:
  extends:
    file: docker-compose.yml
    service: main
  build: utils/sql/
  ports:
    - "5432"
  environment:
    - DEBUG=true

where postgresdb's Dockerfile is:

FROM postgres

# http://www.slideshare.net/tarkasteve/developerweek-2015-docker-tutorial
ADD make-db.sh /docker-entrypoint-initdb.d/

and the main is:

FROM python:2.7

RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/

RUN pip install --upgrade pip
RUN pip install -r requirements.txt
ADD . /code/

This works right now, but I'd like to extend postgresdb's Dockerfile by calling a Python script that creates tables in the database according to models built upon SQL Alchemy (the Python script would be called as python manage.py create_tables). I wanted to add it to the db's Dockerfile, but due to the isolation of the containers I can't use SQL Alchemy there because that image is based on the postgres image instead of Python's, and it doesn't contain the sqlalchemy package...

What can I do? I tried to use the main service in postgresdb, but unfortunately it doesn't carry python and its packages over, so I still can't write a single Dockerfile that creates the Postgres database (through the shell script) as well as its tables (through a Python script).

解决方案

This is not possible due to the way Docker handles build contexts.

You will have to use and place a Dockerfile in each directory that becomes part of the Docker build context for that service.

See: Dockerfile

You will in fact require a docker-compose.yml that looks like:

service1:
    build: service1

service2:
    build: service2

See: docker-compose

Update:

To address your particular use-case -- Whilst I understand what you're trying to do and why I personally wouldn't do this myself. The isolation is a good thing and helps to manage expectations and complexity. I would perform the "database creation" as either another container based off your app's source code or within the app container itself.

Alternatively you could look at more scripted and template driven solutions such as shutit (I have no experience in but heard god thigns about).

FWIW: Separation of concerns ftw :)

这篇关于docker-compose:对多个服务使用多个Dockerfiles的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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