'Hot to pass ENV to image parent from Dockerfile
I want to use some already existing image from docker hub and change many options (env variables) that are available. Locally I can set them via docker run command in docker-compose:
services:
converter:
image: some/image
environment:
- SOME_OPTION=111
And it works.
But on production I'm not using docker-compose. I need that env variables to be permament and ready to use in multiple projects, without manually filling env. So I decided to simply create own Dockerfile from it:
ENV SOME_OPTION=111
FROM some/image
But it's wrong because according to IDE and documentation ENV cannot be set before FROM. But when I do
FROM some/image
ENV SOME_OPTION=111
My setting is ignored by parent image.
How to handle this without passing env in commandline or using docker-compose?
Edit:
I tried to avoid using specific images in this example because I tough it's universl docker question, not some package issue. Anyway, real example here like requested in comments:
Image is jlesage/docker-handbrake
It has many options listed here: https://github.com/jlesage/docker-handbrake for example AUTOMATED_CONVERSION_PRESET
I can change it in docker-compose.yml just fine. Finally I will change like 10 options and I do not want to set this in .env because these options should not be changed and I want to use it in other projects too with exact same video options. So I want to have separate image that will make Handbrake to work exactly like I want. I though it's what Docker is created for and it will be easy...
But when I set it in ENV in dockerfile it is just ignored, Handbreake runs with default options. It's because I'm setting this ENV after this program runs, but how to do this before then?
Solution 1:[1]
You don't modify the parent image, it should be treated as immutable unless you own that image. Instead you build and deploy your own image based off the parent image. So you have:
FROM some/image
ENV SOME_OPTION=111
Build it with:
docker build -t my/image .
docker push my/image
And then you deploy my/image instead of some/image. Note that my/image should point to a registry/repository where you have access to push images.
If you have built a new image with the variables set, but don't see them applying, then several possibilities come to mind:
- You may not be running your new image. You need to update the image references to point to the image you created with these settings, instead of the upstream image.
- You may be overriding the setting at runtime. The value of the ENV is the default if it is not set by the compose file, docker run, kubernetes manifest, etc. So double check how you run the image.
- The variable may be ignored by the image scripts. E.g. many images only apply variables on the first run to initialize the volume, and after that only use the settings or data in the volume.
As some initial debugging, I would run the image with a shell (like /bin/sh) and check the environment to see that the variable is set.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
