Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute command before default entrypoint?

Tags:

docker

nginx

I have built a docker image over nginx-alpine one. I know that, at the start, the nginx image reads some configuration file and starts the server.

The configuration file (I can see that from github) reads the configuration stored inside /etc/nginx.

I'd like to perform some runtime operation on that configuration before actually starting the script. This configuration is based on an environment variable that I would set inside of my deployment descriptor.

I know that I could simply overwrite the script, but what if in further versions they change it? I would have to keep updating it too.

So, would it to be possible to:

  1. execute some custom shell instructions
  2. give the command to the default image entrypoint?
like image 426
Phate Avatar asked Mar 24 '26 21:03

Phate


1 Answers

If I not misunderstand your requirement, your concerns related to next 2 points:

  1. You need to change some configuration according to runtime environment variable which could only there when you run the container.
  2. You want to make actions for 1, then pass control to the default entrypoint. Meanwhile, you don't want to change default entrypoint.sh, because you worried it will change later. Change this code is really hard to maintain.

Then, what I suggested is: wrap your customized docker-entrypoint.sh to call default entrypoint.sh. Next is the thoughts, just an example:

  • Original situation:

    Dockerfile:

    FROM alpine
    ...
    ENTRYPOINT ./entrypoint.sh
    

    entrypoint.sh:

    # do default entrypoint things here
    
  • Updated solution to meet your requirements:

    Dockerfile:

    FROM alpine
    ENV myvariable=""
    ...
    ENTRYPOINT ./docker-entrypoint.sh $myvariable
    

    docker-entrypoint.sh:

    # change the configurations base on the first parameter: $1
    # change configure code here
    
    # call default entrypoint.sh
    ./entrypoint.sh
    

    With above solution, you can base on the environment myvariable to do configure code in self defined docker-entrypoint.sh. Meanwhile, even later entrypoint.sh in original image changes later, it will still be transparent to you as you just call it, no code change for this script.

like image 122
atline Avatar answered Mar 27 '26 14:03

atline



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!