Is there a way to add a Dockerfile RUN command equivalent to a compose file?
Context:
docker-compose.yaml
has service webapp
with an accompanying Dockerfile (webapp.Dockerfile
)
When I do development, I have a docker-compose.local.yaml
that adds some additional development configurations to webbapp
service, e.g.:
services:
webapp:
stdin_open: true
tty: true
... # other stuff for my local development needs
I'd also like to run a couple of commands to the image being built, but I do not want to put it in a RUN
command in webapp.Dockerfile
. I'd like to somehow include it in docker-compose.local.yaml
so that it's something additional I can tack on without changing the base configs. (BTW I invoke the additional YAML file with COMPOSE_FILE
environment variable).
A RUN
command is part of the image build process; it can only be used in a Dockerfile. If I want to install additional commands into the image used by my compose configuration, I can build a new image based on the old one.
For example, if I have a docker-compose.yaml
that looks like this:
services:
web:
image: docker.io/alpinelinux/darkhttpd:latest
And I really want the curl
command to be available inside this container, I can create a docker-compose.override.yaml
like this:
services:
web:
build: .
And then put a Dockerfile
in the same directory as the docker-compose.yaml
that looks like this:
services:
web:
image: development
build:
context: .
And when docker-compose up
the environment, it will build the custom image and use that rather than using the image specified in docker-compose.yaml
:
$ docker-compose up
Building web
[+] Building 0.1s (6/6) FINISHED
=> [internal] load build definition from Dockerfile 0.1s
=> => transferring dockerfile: 124B 0.0s
=> [internal] load .dockerignore 0.1s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/alpinelinux/darkhttpd:latest 0.0s
=> [1/2] FROM docker.io/alpinelinux/darkhttpd:latest 0.0s
=> CACHED [2/2] RUN apk add curl 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:23e53345322fa1787aa6fd291347732501499f60656805675c6fd4217d040006 0.0s
=> => naming to docker.io/library/development 0.0s
WARNING: Image for service web was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating containers_web_1 ... done
Attaching to containers_web_1
web_1 | darkhttpd/1.14, copyright (c) 2003-2022 Emil Mikulic.
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