简体   繁体   中英

Dockerfile: is it possible to reference overridden entrypoint and cmd?

I'm trying to build a image using mysql 5.6 from here as a base image. I need to do some initialization before the database starts up, so I need to override the entrypoint:

# Stuff in my Dockerfile
...
COPY my-entrypoint.sh /usr/local/bin/
ENTRYPOINT ["my-entrypoint.sh"]

My entrypoint is fairly simple, too:

#!/bin/bash
echo "Running my-entrypoint.sh"
# My initialization stuff here
...
# Call mysql entrypoint
/usr/local/bin/docker-entrypoint.sh mysqld

This seems to work, but I'd rather not have to hard-code the mysql entrypoint in my script (or my Dockerfile). Is there a way to reference the overridden entrypoint in my Dockerfile, so that it is available to my entrypoint script? Something like this, perhaps?

COPY my-entrypoint.sh /usr/local/bin/
ENTRYPOINT ["my-entrypoint.sh", BASE_ENTRYPOINT, BASE_CMD]

It has to appear in somewhere in someway, otherwise you can't get such information.

  • Option 1: use an ENV for previous entrypoint in Dockerfile , and then refer to it in your own entrypoint.sh:

    Dockerfile:

     FROM alpine:3.3 ENV MYSQL_ENTRYPOINT "/usr/bin/mysql mysqld" ADD entrypoint.sh / ENTRYPOINT ["/entrypoint.sh"] 

    Entrypoint.sh:

     #!/bin/sh echo $MYSQL_ENTRYPOINT 
  • Option 2: just pass previous entrypoint command as parameter to your entrypoint:

    Dockerfile:

     FROM alpine:3.3 ADD entrypoint.sh / ENTRYPOINT ["/entrypoint.sh"] CMD ["/usr/bin/mysql mysqld"] 

    Entrypoint.sh:

     #!/bin/sh echo $1 

Personally I prefer option #1.

2 ways:

Just pass it in after you specify your image, everything after that becomes the CMD and appended to your ENTRYPOINT . So...

# Stuff in my Dockerfile
...
COPY my-entrypoint.sh /usr/local/bin/
ENTRYPOINT ["my-entrypoint.sh"]

Then docker run ... image <your-entrypoint-etc> then just have your custom entrypoint pick up the 1st arg to it and use that however you need.

Second way, just pass it as an environment variable at runtime.

docker run ... -e MYSQL_ENTRYPOINT=<something> ...

And in your entrypoint script refer to the env variable ... $MYSQL_ENTRYPOINT ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM