Forem

Francesco Bianco
Francesco Bianco

Posted on β€’ Edited on

6 1

Extends ENTRYPOINT or CMD scripts from Dockerfile in a smart way

Sometimes we need to create a new image starting from a base image with proper ENTRYPOINT or CMD scripts, eg. docker-entrypoint.sh or service-foreground.sh, it looks in generic Dockerfile as follow

COPY docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
COPY service-foreground.sh /usr/local/bin/service-foreground.sh

ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["service-foreground.sh"]
Enter fullscreen mode Exit fullscreen mode

Now, you require to personalize the behavior of this script just a little bit and you annoyed by recreate these files from zero or copy from the base just to override in your build. Well, think different, you will act an extending approach like class inheritance

## Extend foreground script
RUN cd /usr/local/bin; \
    cp apache2-foreground apache2-foreground-inherit; \
    { \
        echo '#!/bin/bash'; \
        echo '[[ "$1" = "mycmd" ]] && { mycmd "$@"; exit; }'; \
        echo 'apache2-foreground-inherit "$@"'; \
    } > /usr/local/bin/apache2-foreground

## Extend entrypoint script
RUN cd /usr/local/bin; \
    cp docker-entrypoint.sh docker-entrypoint-inherit.sh; \
    { \
        echo '#!/bin/bash'; \
        echo '[[ "$1" = "mycmd" ]] && set -- mycmd "$@"'; \
        echo 'docker-entrypoint-inherit.sh "$@"'; \
    } > /usr/local/bin/docker-entrypoint.sh
Enter fullscreen mode Exit fullscreen mode

Break any rule before writing a new production Dockerfile, be smart and reuse all of things from base image before create new stuff.

πŸ‘‹ One new thing before you go

Tired of getting nickel-and-dimed on your software career? πŸ˜’

We have created a membership program that helps cap your costs so you can build and experiment for less. And we currently have early-bird pricing which makes it an even better value! πŸ₯

Just one of many great perks of being part of the network ❀️

Top comments (0)

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

πŸ‘‹ Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay