Multiple Dockerfiles in project

2019-01-21 04:17发布

How do you organize the Dockerfiles belonging to a project when you have one Dockerfile for the database, one for the application server, and so on? Do you create some sort of hierachy in the source? A big enterprise project can't consist of only one Dockerfile?

标签: docker
6条回答
等我变得足够好
2楼-- · 2019-01-21 04:48

In Intellij, I simple changed the name of the docker files to *.Dockerfile, and associated the file type *.Dockerfile to docker syntax.

查看更多
对你真心纯属浪费
3楼-- · 2019-01-21 04:55

When working on a project that requires the use of multiple dockerfiles, simply create each dockerfile in a separate directory. For instance,

app/ db/

Each of the above directories will contain their dockerfile. When an application is being built, docker will search all directories and build all dockerfiles.

查看更多
老娘就宠你
4楼-- · 2019-01-21 04:59

Add an abstraction layer, for example a YAML file like in this project https://github.com/larytet/dockerfile-generator which looks like

centos7: base: centos:centos7 packager: rpm install: - $build_essential_centos - rpm-build run: - $get_release env: - $environment_vars

A short Python script/make can generate all Dockerfiles from the configuration file

查看更多
ら.Afraid
5楼-- · 2019-01-21 05:00

I just create a directory containing a Dockerfile for each component. Example:

When building the containers just give the directory name and Docker will select the correct Dockerfile.

Autho Note

This answer is out of date. Fig not longer exists and has been replaced by Docker compose

查看更多
祖国的老花朵
6楼-- · 2019-01-21 05:08

Use docker-compose and multiple Dockerfile in separate directories

Don't rename your Dockerfile to Dockerfile.db or Dockerfile.web, it may not be supported by your IDE and you will lose syntax highlighting.

As Kingsley Uchnor said, you can have multiple Dockerfile, one per directory, which represent something you want to build.

I like to have a docker folder which holds each applications and their configuration. Here's an example project folder hierarchy for a web application that has a database.

docker-compose.yml
docker
├── web
│   └── Dockerfile
└── db
    └── Dockerfile

docker-compose.yml example:

version: '3'
services:
  web:
    # will build ./docker/web/Dockerfile
    build: ./docker/web
    ports:
     - "5000:5000"
    volumes:
     - .:/code
  db:
    # will build ./docker/db/Dockerfile
    build: ./docker/db
    ports:
      - "3306:3306"
  redis:
    # will use docker hub's redis prebuilt image from here:
    # https://hub.docker.com/_/redis/
    image: "redis:alpine"

docker-compose command line usage example:

# The following command will create and start all containers in the background
# using docker-compose.yml from current directory
docker-compose up -d

# get help
docker-compose --help

In case you need files from previous folders when building your Dockerfile

You can still use the above solution and place your Dockerfile in a directory such as docker/web/Dockerfile, all you need is to set the build context in your docker-compose.yml like this:

version: '3'
services:
  web:
    build:
      context: .
      dockerfile: ./docker/web/Dockerfile
    ports:
     - "5000:5000"
    volumes:
     - .:/code

This way, you'll be able to have things like this:

config-on-root.ini
docker-compose.yml
docker
└── web
    ├── Dockerfile
    └── some-other-config.ini

and a ./docker/web/Dockerfile like this:

FROM alpine:latest

COPY config-on-root.ini /
COPY docker/web/some-other-config.ini /

Here are some quick commands from tldr docker-compose. Make sure you refer to official documentation for more details.

查看更多
仙女界的扛把子
7楼-- · 2019-01-21 05:09

In newer versions(>=1.8.0) of docker, you can do this

docker build -f Dockerfile.db .
docker build -f Dockerfile.web .

A big save.

EDIT: update versions per raksja's comment

查看更多
登录 后发表回答