docker-compose: using multiple Dockerfiles for multiple services

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:

  • W: Size of file /var/lib/apt/lists/http.debian.net_debian_dists_jessie_main_binary-amd64_Packages.gz is not what the server reported 9101219 9117278
  • When multiple networks in Docker compose ports aren't being mapped
  • How can I find out which capabilities a container has been given?
  • Docker connection refused hanging Django
  • Adding NPM and Bower to a Dockerize LAMP environment
  • psycopg2 installation for python:2.7-alpine in Docker
  • main:
      build: .
        - .:/code
        - DEBUG=true
        file: docker-compose.yml
        service: main
      build: utils/sql/
        - "5432"
        - DEBUG=true

    where postgresdb‘s Dockerfile is:

    FROM postgres
    ADD /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 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).

  • Docker images with visual(X) support? Or should I use VBox instead in this case?
  • Build vs Runtime environment variables
  • Symlinked Dockerfile for environments
  • How to access webserver running on localhost from a docker container on a network?
  • Using two docker files - dev and prod
  • Installation of chef-client(Bootstrapping) on docker container in a VM on Azure/AWS
  • 2 Solutions collect form web for “docker-compose: using multiple Dockerfiles for multiple services”

    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:

        build: service1
        build: service2

    See: docker-compose


    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 🙂

    Creator of ShutIt here. Gratified to hear that people are hearing good things about it.

    To be honest, in your position I’d write your own Dockerfile and use standard package management such as apt or yum. A quick check with an ubuntu image and python-pip and python-sqlalchemy are freely available.

    There are more convoluted solutions that may work for you using ShutIt, happy to discuss this offline, as I think it’s a bit off-topic. ShutIt was written for this kind of use case, as I could see that this would be a common problem given Dockerfiles’ limited utility outside the microservices space.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.