entrypoint: "entrypoint.sh" - docker compose

30,900

Solution 1

entrypoint: "entrypoint.sh" overrides ENTRYPOINT ["test.sh"] from Dockerfile.

From the docs:

Setting entrypoint both overrides any default entrypoint set on the service’s image with the ENTRYPOINT Dockerfile instruction, and clears out any default command on the image - meaning that if there’s a CMD instruction in the Dockerfile, it is ignored.

  • ENTRYPOINT ["test.sh"] is set in Dockerfile describing docker image

  • entrypoint: "entrypoint.sh" is set in docker-compose file which describes multicontainer environment while referencing the Dockerfile.

  • docker-compose build builder will build image and set entrypoint to ENTRYPOINT ["test.sh"] set in Dockerfile.

  • docker-compose up builder will start container with entrypoint entrypoint.sh pip wheel --no-index '-f /build' . set in docker-compose file

Solution 2

ENTRYPOINT is a command or script that is executed when you run the docker container.

If you specify entrypoint in the docker-compose.yaml, it overrides ENTRYPOINT from specified Dockerfile.

CMD is something that is passed as the parameters to the ENTRYPOINT

So if you just run the dev/Dockerfile, it would execute

test.sh python manage.py test --noinput

If you overrided CMD in docker-compose.yaml as you did, it would execute

test.sh pip wheel --non-index -f /build .

But because you also overrided ENTRYPOINT in your docker-compose.yaml, it is going to execute

entrypoint.sh pip wheel --non-index -f /build .

So basically, entrypoint.sh is a script that will run inside your container builder when you execute docker-compose up command.

Also you can check this answer for more info What is the difference between CMD and ENTRYPOINT in a Dockerfile?

Solution 3

Update: If the base image has entrypoint.sh, it will run that, but if you override with your own entrypoint then the container will run the override entrypoint.

If you to override the default behaviour of base image then you can change, ohterwise you do not need to override it from docker-compose.

What does entrypoint: "entrypoint.sh" actually do?

It totally depend on the script or command inside entrypoint.sh, but few things can be considered.

ENTRYPOINT instruction allows you to configure a container that will run as an executable. It looks similar to CMD, because it also allows you to specify a command with parameters. The difference is ENTRYPOINT command and parameters are not ignored when Docker container runs with command line parameters. (There is a way to ignore ENTTRYPOINT, but it is unlikely that you will do it.)

In simple word, entrypoint can be a complex bash script, for example in case of mysql entrypoint which is more then 200 LOC which does the following task.

  • start MySQL server
  • wait for MySQL server to up
  • Create DB
  • Can perform DB migration or DB initlization

So much complex task is not possible with CMD, as in CMD you can run the bash but it will be more headache to make it work. Also it make Dockerfile simple and put the complex task to entrypoint.

When there is entrypoint, anything that is passed to CMD will be consider as a argument for entrypoint.

In your case, CMD is CMD ["python", "manage.py", "test", "--noinput"] it will be passed as an argument and the best to run this is to use use

# set of command
#start long running process at the end that is passed from CMD
exec "$@"

Finally, the exec shell construct is invoked, so that the final command given becomes the container's PID 1. $@ is a shell variable that means "all the arguments",

use-a-script-to-initialize-stateful-container-data

cmd-vs-entrypoint

Share:
30,900
overexchange
Author by

overexchange

Current technologies: Javascript & Python

Updated on October 19, 2020

Comments

  • overexchange
    overexchange over 3 years

    There is no such file by name entrypoint.sh in my workspace.

    But below instruction in docker-compose.yml is referring it:

    builder: 
      build: ../../
      dockerfile: docker/dev/Dockerfile
      volumes:
        - ../../target:/wheelhouse
      volumes_from:
        - cache
      entrypoint: "entrypoint.sh"
      command: ["pip", "wheel", "--non-index", "-f /build", "."]
    

    where ../docker/dev/Dockerfile has

    # Set defaults for entrypoint and command string
    ENTRYPOINT ["test.sh"]
    CMD ["python", "manage.py", "test", "--noinput"]
    

    What does entrypoint: "entrypoint.sh" actually do?

  • overexchange
    overexchange over 4 years
    Related to docker-compose stackoverflow.com/questions/58439706/…
  • Adiii
    Adiii over 4 years
    The only thing make me wander is "There is no such file by name entrypoint.sh in my workspace." then how is overriding?
  • overexchange
    overexchange over 4 years
    @Adiii there is entrypoint.sh in base docker image FROM todobackendbase. Base docker image copies entrypoint.sh in folder /usr/local/bin