Single init script for multiple containers? #164103
-
BodyI'm running three containers from the same Docker image, but I only want an init script to run in one of them (like setting up a database, seeding data, etc.). Right now, I can pass the script when running the container manually, but I’ve read that this isn't considered a best practice. What’s the recommended way to ensure only one container runs the init logic, while the others skip it — without maintaining multiple images? Guidelines
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
You can handle this by modifying your Docker image to include the init script via CMD or ENTRYPOINT, and using a custom environment variable like IS_MASTER to control whether the script runs. 🔧 Example Approach:
And for the others:
This way, you keep a single image but control behavior cleanly through environment variables. |
Beta Was this translation helpful? Give feedback.
-
|
You can handle this using a single Docker image by wrapping your init logic in a script that checks an environment variable. Steps:
#!/bin/sh
if [ "$RUN_INIT" = "true" ]; then
./init.sh # your DB setup or seeding script
fi
exec "$@"
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
✅ This way you keep a single image and just control behavior through environment variables — clean and scalable. |
Beta Was this translation helpful? Give feedback.
You can handle this by modifying your Docker image to include the init script via CMD or ENTRYPOINT, and using a custom environment variable like IS_MASTER to control whether the script runs.
🔧 Example Approach:
docker run -e IS_MASTER=true true your-imageAnd for the others:
docker run your-imageThis way, you keep a single image but control behavior cleanly through environment variables.