dockerdocker-entrypoint

Docker entrypoint and cmd together


I try to setup a Docker with both entrypoint and cmd.

FROM debian:stretch
RUN apt-get update && \
apt install gnupg ca-certificates -y 
RUN echo "deb http://repo.aptly.info/ squeeze main" > /etc/apt/sources.list.d/aptly.list
RUN apt-key adv --keyserver keys.gnupg.net --recv-keys 9E3E53F19C7DE460
RUN apt update && apt install aptly -y
ADD aptly.conf /etc/aptly.conf
ADD start.sh .
VOLUME ["/aptly"]
ENTRYPOINT ["/start.sh"]
CMD ["aptly", "api", "serve"]

But entrypoint script is not stopping... The cmd command is not launching

Here my script :

#!/bin/bash
set -e 
init_aptly() {  
  #import pgp key
  #create nginx root folder in /aptly
  su -c "mkdir -p /aptly/.aptly/public"
  echo "12"
  #initialize repository
  #aptly create repo doze-server -   distribution="stable"
}
#check for first run
if [ ! -e /aptly/.aptly/public ]; then
  init_aptly
  echo "13"
fi
echo "14"

The script always echo 14, I would like only one and then, execute the cmd command from dockerfile


Solution

  • If your image has an ENTRYPOINT, as other answers here have noted, that is the only command the container runs, and the CMD is passed to it as additional arguments. Also see the section Understand how CMD and ENTRYPOINT interact in the Dockerfile documentation.

    This means, in the entrypoint wrapper script, you need to actually run the command you're passed. The usual approach here is to end your script with the shell construct exec "$@", like

    #!/bin/sh
    # ... do startup-time setup ...
    # ...then run the main container command
    exec "$@"
    

    Note that this works however the command part is provided. If for example you docker run your-image some-other-command, then it will run the entrypoint wrapper script from the image, but at the end it will run some-other-command instead of the CMD from the Dockerfile.