As Kingsley Uchnor said, you can have multiple Dockerfile , one per directory, which represent something you want to build.
The ENTRYPOINT command makes it so that apache2 starts when the container starts. I want to also be able to start mongod when the the container starts with the command service mongod start . According to the documentation however, there must be only one ENTRYPOINT in a Dockerfile.
It's ok to have multiple processes, but to get the most benefit out of Docker, avoid one container being responsible for multiple aspects of your overall application. You can connect multiple containers using user-defined networks and shared volumes.
FROM can appear multiple times within a single Dockerfile in order to create multiple images. Simply make a note of the last image ID output by the commit before each new FROM command.
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
EDIT: comment from @vsevolod: it's possible to get syntax highlighting in VS code by giving files .Dockerfile extension(instead of name) e.g. Prod.Dockerfile, Test.Dockerfile etc.
docker-compose
and multiple Dockerfile
in separate directoriesDon't rename your
Dockerfile
toDockerfile.db
orDockerfile.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
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.
This answer is out of date. Fig no longer exists and has been replaced by Docker compose. Accepted answers cannot be deleted.
Docker Compose supports the building of project hierachy. So it's now easy to support a Dockerfile in each sub directory.
├── docker-compose.yml
├── project1
│ └── Dockerfile
└── project2
└── Dockerfile
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.
In Intellij, I simple changed the name of the docker files to *.Dockerfile, and associated the file type *.Dockerfile to docker syntax.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With