Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 751
Vana Avatar asked Dec 05 '17 08:12

Vana


People also ask

Can I use both ENTRYPOINT and CMD?

Example of using CMD and ENTRYPOINT together If both ENTRYPOINT and CMD are present, what is written in CMD is executed as an option of the command written in ENTRYPOINT. If an argument is added at the time of docker run , the contents of CMD will be overwritten and the ENTRYPOINT command will be executed.

Can you have both CMD and ENTRYPOINT in Dockerfile?

That's right, it is possible to have both in your Dockerfile. There are many situations in which combining CMD and ENTRYPOINT would be the best solution for your Docker container. In such cases, the executable is defined with ENTRYPOINT, while CMD specifies the default parameter.

Does ENTRYPOINT override CMD in docker?

Docker Entrypoint ENTRYPOINT is the other instruction used to configure how the container will run. Just like with CMD, you need to specify a command and parameters. However, in the case of ENTRYPOINT we cannot override the ENTRYPOINT instruction by adding command-line parameters to the `docker run` command.

Can you tell the difference between CMD and ENTRYPOINT?

The ENTRYPOINT instruction looks almost similar to the CMD instruction. However, the main highlighting difference between them is that it will not ignore any of the parameters that you have specified in the Docker run command (CLI parameters).


3 Answers

When you use both entrypoint and command, the command section will be appended to entrypoint executable as arguments. Thus in your case:

ENTRYPOINT ["/start.sh"]
CMD ["aptly", "api", "serve"]

Is equivalent to running:

ENTRYPOINT["/start.sh", "aptly", "api", "serve"]
like image 110
yamenk Avatar answered Sep 30 '22 05:09

yamenk


One important note, since nobody else has mentioned it: in order to use ENTRYPOINT and CMD together, you need to specify both in the array format. Doing something like this WILL NOT WORK:

ENTRYPOINT ./my_script.sh
CMD echo "hello world"

In the code above, ./my_script.sh will be called, but CMD will not be passed in.

like image 28
Jeremy Blalock Avatar answered Sep 30 '22 06:09

Jeremy Blalock


Can't tell much without knowing what the entrypoint does, but essentially this is what you are doing with this combination of entrypoint and cmd:

/start.sh aptly api serve

If you are after simply starting your server you can simply do something like this (valid path to the aptly executable may be neccessary):

ENTRYPOINT ["aptly"]
CMD ["api", "serve"]

Unless you are doing much more than just running an executable there's no need for an entrypoint.

like image 32
emix Avatar answered Sep 30 '22 07:09

emix