Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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

like image 435
aralar Avatar asked Apr 23 '15 23:04

aralar


People also ask

Is it possible to have multiple Dockerfiles?

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

Can Docker compose run multiple containers?

The docker-compose. yml file allows you to configure and document all your application's service dependencies (other services, cache, databases, queues, etc.). Using the docker-compose CLI command, you can create and start one or more containers for each dependency with a single command (docker-compose up).

How do I run multiple copies of a compose file on the same host?

🔗 Compose uses the project name to create unique identifiers for all of a project's containers and other resources. To run multiple copies of a project, set a custom project name using the -p command line option or the COMPOSE_PROJECT_NAME environment variable.


1 Answers

You have to add it in build section. So, you can specify different alternative dockerfiles for each service.

services:   service1:     build:         context: .         args:             - NODE_ENV=local         dockerfile: Dockerfile_X     ports:         - "8765:8765" 
like image 137
Alejandro Galera Avatar answered Sep 27 '22 20:09

Alejandro Galera