DEV Community

Krzysztof Żuraw
Krzysztof Żuraw

Posted on • Originally published at krzysztofzuraw.com on

1

Executing scripts when docker starts

I recently needed to execute a shell script when Docker is starting. Why? I wanted dynamically take environment variables from the .env file and convert them to JSON objects in index.html.

It turns out that docker has a special folder called /docker-entrypoint.d/. It will take every shell script left there and execute it while running the container. Perfect for my case.

What I learned as well (from Francisco Marques) is that there is a two-digit convention for naming Linux scripts. Why? It allows docker to execute them one by one in sorted order. In my case I wanted the shell script to run after internal docker scripts has already finished running so I put a 50 number at the beginning of the script name.

Summary

If you want to run a shell script when building a Docker container put a script inside /docker-entrypoint.d folder (e.g by COPY inside Dockerfile). Make sure it has a number at the beginning of the script name so the script will be executed in proper order.

Billboard image

Deploy and scale your apps on AWS and GCP with a world class developer experience

Coherence makes it easy to set up and maintain cloud infrastructure. Harness the extensibility, compliance and cost efficiency of the cloud.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more