Docker - Entrypoint (Main)
Table of Contents
About
Language - (Main|Application Entry point) in Docker.
The entry point script is the script called when creating a container from an image with the docker run command
The entrypoint is specified in a dockerfile.
When the entrypoint program exits, the VM is stopped (killed). A process needs to run in the foreground.
Example
Default
When you run docker like this:
docker run -i -t ubuntu bash
- the image is ubuntu
- the entrypoint is the default /bin/sh -c because there is no ENTRYPOINT in the ubuntu dockerfile
- and the command is bash.
Wrapper entrypoint.sh
The below script start a process before starting the command given at the command line.
It's a wrapper around the default entrypoint.
If you set the entrypoint in your dockerfile, you need to set it with the exec form otherwise you get no arguments.
#!/usr/bin/env bash
echo Starting the ssh-agent for convenience
eval `ssh-agent`
# Start the passed command ($*)
/bin/sh -c "$*"
Management
Assignement
Console
docker run --entrypoint /script.sh repo/image
Default (dockerfile)
The default entrypoint is specified in the dockerfile. See Doc entrypoint
It has two different behavior that depends of the assignment format. ENTRYPOINT has the following forms:
# exec form, preferred
ENTRYPOINT ["executable", "param1", "param2"]
# or shell form
ENTRYPOINT command param1 param2
# or
ENTRYPOINT ["python"]
CMD ["myPythonFile.py"]
If you want to get the parameters from the command line passed to the entry point, you need to choose the exec form
Exec form
Command line arguments to docker run will be appended after all elements in an exec form ENTRYPOINT, and will override all elements specified using CMD.
Shell form
The shell form prevents any command line arguments (cmd or via run) from being used.
The ENTRYPOINT will be started as a subcommand of /bin/sh -c, which does not pass signals. This means that the executable will not be the container’s PID 1 - and will not receive Unix signals - so your executable will not receive a SIGTERM from docker stop <container>.
List
docker inspect --format "{{range .Config.Entrypoint}}{{.}}{{end}}" (containerName|imageName)
Example on the image org/ubuntu:latest
docker inspect --format "{{range .Config.Entrypoint}}{{.}}{{end}}" org/ubuntu:latest
See Docker - Inspect
Multiple process
To run multiple services in a single container there are multiple ways
- Script
- A process manager
Bash Script
With a script:
- you start any number of programs in the background,
- and you start a process at the end that stay in the foreground to keep the VM alive.
Example of script:
- and the fg (foreground utility)
#!/bin/bash
set -m # to make job control work
/app/server &
/app/server -bar &
fg %1
Docker file:
ADD multiple.sh /app/
CMD /app/multiple.sh
Process Manager
A process manager can start more than one process.