jshbrntt
jshbrntt

Reputation: 5405

Passing arguments to sub-processed entry point

Basically I want my program to be started subcommand of /bin/sh -c, so that I can Ctrl + C it and have it stop the process.

If I use this form.

ENTRYPOINT executable

It will convert to /bin/sh -c 'executable', which gives me the ability to Ctrl + C but it prevents me from passing further arguments in the COMMAND when running e.g.

docker run executable arg1

Will still end up as /bin/sh -c 'executable'.

If I use define the ENTRYPOINT like so, using the array format.

ENTRYPOINT [ "executable" ]

This will work.

docker run executable arg1

Start running as executable arg1, but Ctrl + C will not work as its not a subprocess of a shell.

Upvotes: 0

Views: 641

Answers (2)

jshbrntt
jshbrntt

Reputation: 5405

You have a write your own docker-entrypoint.sh script and use that to wrap your executable while also passing it arguments from the docker command.

Create a file called docker-entrypoint.sh with the following contents.

#!/usr/bin/env sh
executable "$@"

Then at the end of your Dockerfile use the following commands.

# Copy over the entrypoint script.
COPY docker-entrypoint.sh /usr/local/bin/

# Default entrypoint.
ENTRYPOINT [ "docker-entrypoint.sh" ]

Then you will be able to pass the executable in the container parameters like so.

docker run -it --rm image arg1

Which will run like this in the container.

PID   USER     TIME   COMMAND
    1 root       0:00 sh /usr/local/bin/docker-entrypoint.sh arg1
    6 root       0:00 executable arg1
   16 root       0:00 sh
   22 root       0:00 ps faux

You will also be able to shutdown the container by using Ctrl+C when attached.

Upvotes: 0

Shahriar
Shahriar

Reputation: 13814

You need to run your docker in interactive mode.

Use command docker run -it

The -it instructs Docker to allocate a pseudo-TTY connected to the container’s stdin; creating an interactive bash shell in the container.

I have tried to reproduce the problem you have faced.

Dockerfile I have used

FROM ubuntu
COPY hold.sh .
ENTRYPOINT ["./hold.sh"]

Shell script like executable

$ cat hold.sh 
#!/usr/bin/env bash

echo "$@"

echo "waiting"
sleep 5m

Now I have build it and run

$ docker build -t test .
$ docker run -it test "running"
running
waiting
^C⏎ 

Its working fine.

Upvotes: 1

Related Questions