Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Dockerfiles

I am trying to use docker and want to create an Ubuntu base with three containers that do the following:

  1. Container: Install Wildfly
  2. Container: Install MySQL
  3. Container: Other Required Packages

Does that mean, I have to create three Dockerfiles in three different directories containing each the following top line?:

FROM ubuntu:18.04
like image 592
Chris S. Avatar asked Mar 18 '26 19:03

Chris S.


2 Answers

You can have more than one Dockerfile in the same directory if desired. To specify the Dockerfile to use, use the -f argument, e.g

docker build -f wildfly.Dockerfile ./wildfly
docker build -f mysql.Dockerfile ./mysql
docker build -f other.Dockerfile ./other

In Compose, these arguments correspond to the dockerfile and context properties.

It is not always the case that you need to write a docker file, for example for the database service you can simply pull the image from the docker hub and use it directly. something like below

db:
    image: mysql

You can, of course, have them share the same context, e.g.

docker build -f wildfly.Dockerfile .
docker build -f mysql.Dockerfile .
docker build -f other.Dockerfile .

Just be aware that the context is sent in full to the daemon (respecting .dockerignore) so this might lead to longer build times if there is a lot of redundant data.

If there is a lot of reuse between the Dockerfiles, you can even have all of them in one file, e.g.

FROM ubuntu:20.04 as base
...
FROM base AS wildfly
(install wildfly)

FROM base AS mysql
(install mysql)
...

Then you can build the specific image with e.g.

docker build --target wildfly  .

In Compose, these arguments correspond to the target and context properties.

This is called multi-stage builds, and is not always a good idea but is sometimes helpful to mitigate Docker's lack of support for #include.

like image 106
Krumelur Avatar answered Mar 20 '26 07:03

Krumelur


Orchestrate the containers with docker-compose

1- Create docker-compose.yml

2- Inside define:

version: '3'
services:
  wildly:
    build:
      context: .
      dockerfile: Dockerfile_Wildfly
  mysql:
    build:
      context: .
      dockerfile: Dockerfile_Mysql
  anotherpackages:
    build:
      context: .
      dockerfile: Dockerfile_AnotherPackages

it is not always the case that you need to write a docker file, for example for the database service you can simply pull the image from the docker hub and use it directly. something like below

db:
    image: mysql

3- Create files and both define commands you prefer:

Dockerfile_Wildfly
 FROM wildfly

Dockerfile_Mysql
 FROM mariadb

Dockerfile_AnotherPackages
 FROM node
 FROM nginx
like image 27
Darlan D. Avatar answered Mar 20 '26 07:03

Darlan D.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!