Reputation: 1158
I have a docker-compose file which builds 4 images and runs 4 containers which is fine. But in those 4 Dockerfile which I used to build 4 images, the only difference is that the cmd. I'm just wondering if there's a better way of doing this like build one image and run 4 containers from that image just by changing the cmd,
Here is my docker-compose.yml
version: '3.6'
services:
esg_scoring.insight:
build:
context: .
dockerfile: insight/Dockerfile
esg_scoring.momentum:
build:
context: .
dockerfile: momentum/Dockerfile
esg_scoring.pulse:
build:
context: .
dockerfile: pulse/Dockerfile
esg_scoring.sasb_mapping:
build:
context: .
dockerfile: sasb_mapping/Dockerfile
Upvotes: 2
Views: 3976
Reputation: 14776
In case you need multiple containers running the same image, and differing only by the command that starts them, you should most definitely avoid multiple docker images, and use the same image.
The command
directive in docker-compose
is what you are looking for.
In addition, you can use the YAML anchors syntax to reuse code blocks and simplify your code.
Here is an example of having 4 services, using the same image, with different commands. You can put all the arguments that are common to all, under the default
setting:
services:
bash:
build: .
command: bash
<<: &default
image: me/my-image
web:
<<: *default
command: bundle exec run server start
setup:
<<: *default
command: bundle exec run db setup
jobs:
<<: *default
command: bundle exec run job runner
The nice thing about this approach, is that it will only build once when you run docker-compose build
, since the build
directive is outside of the default
setting.
Upvotes: 7