Build the same image multiple times with different entrypoints in Docker

2/6/2018

I have a Django app and use Celery for background tasks.

For deploying, I use Docker with Kubernetes.

Deployment is automatized with Jenkins.

All is good, however I feel like this can be drastically optimized.

The problem is that Jenkins builds almost the same images for a django app and 5 celery workers. The only difference they have is the entry point. Django app image starts gunicorn, celery container starts, well, celery.

What is the best practice to build almost the same images?

I would optimally like to build the same image several times and indicate the entrypoint during the build process.

Thank you for any help.

-- Jahongir Rahmonov
celery
django
docker
kubernetes

2 Answers

2/6/2018

Not sure of the specific differences in entrypoints but you possibly could use --build-arg in a creative way to pass in the different builds. Note that ENTRYPOINT won't interpolate the build argument but you could do something along the lines of this:

ARG NAME=/some/default/value
RUN ln -s ${NAME} /executable
ENTRYPOINT ["/executable"]

... and then build with something like:

docker build --build-arg NAME=/foo/bar/baz -t baz-build:1.0 .

Obviously, you would change that RUN line accordingly.

(credit idea to commenter here: https://github.com/moby/moby/issues/18492#issuecomment-347364597)

-- Eric Smalling
Source: StackOverflow

2/6/2018

An option that comes to mind is to have the same entrypoint for all the images and then, using environment variables for example, have a logic in the entrypoint code that will launch one program or the other. Here's an extremely simple example.

if [ $ROLE == "worker" ];then
    program_1
else
    profram_2
fi

Another option could be using the same entrypoint and then be able to select the exact program using the command argument. Find an example here: https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/. An example Dockerfile and app-entrypoint.sh here

https://github.com/bitnami/bitnami-docker-wordpress/blob/master/4/Dockerfile https://github.com/bitnami/bitnami-docker-wordpress/blob/master/4/rootfs/app-entrypoint.sh

-- Javier Salmeron
Source: StackOverflow