Recently I saw a lot of docker entrypoint
scripts using exec $@
, where the actual process of the docker image will start. I wanted to use this too for my own images in my docker swarm cluster, but it doesnt start the actual process. Instead it quits the container, because it can't find any arguments. Or nat least, that is what I am seeing with echo
"Arguments: $@
"".
Does somebody knows how to actually make it work? Did I do something wrong with the ENTRYPOINT
and CMD command? This is my Dockerfile and entrypoint.sh
script:
FROM openresty/openresty:alpine-fat
WORKDIR /
COPY entrypoint.sh entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/bin/sh"]
CMD ["entrypoint.sh"]
#!/bin/sh
# Start Proxy
exec "$@"
I have tested this myself.
Little change that have to be made in your code:
FROM openresty/openresty:alpine-fat
WORKDIR /
COPY entrypoint.sh entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
CMD ["default-command", "default-arg"]
#!/bin/sh
# Start Proxy
echo "Arguments: $@"
exec "$@"
entrypoint.sh will be able to start your ENTRYPOINT
CMD
defines default arguments that are passed to the script if no additional arguments are specified when starting the container.
Try using docker run myimage
after you have changed it.
Let me know if it worked.
The problem in your code is in the combination and configuration of the ENTRYPOINT
and CMD
in your Dockerfile and how Docker passes arguments to your entrypoint.sh
ENTRYPOINT: In your Dockerfile you have defined ENTRYPOINT ["/bin/sh"]
.
This means that the container always starts /bin/sh
as the main process.
CMD: You used CMD ["entrypoint.sh"]
.
This passes entrypoint.sh
as an argument to /bin/sh
.
This causes entrypoint.sh
to be seen as an argument and not executed as you intend. The exec "$@"
in your script does not receive the correct arguments, causing the container to close immediately.