Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute the Entrypoint of a Docker images at each "exec" command?

After trying to test Dockerfiles with Dockerspec, I finally had an issue I can't resolve properly.

The problem is, I think, from Docker itself ; If I understand its process, an Entrypoint is only executed at run, but if the container stay started and I launch an "exec" command in, it's not re-called.

I think it's the wanted behavior.

But if the Entrypoint is a "gosu" script which precede all my commands, it's a problem...


Example

"myImage" has this Entrypoint : gosu 1000:1000 "$@"

If I launch : docker run -it myImage id -u

The output is "1000".

If I start a container : docker run -it myImage bash

In this container, id -u outputs "1000".

But if I start a new command in this container, it starts a new shell, and does not execute the Entrypoint, so : docker exec CONTAINER_ID id -u

Output "0", because the new shell is started as "root".


It there a way to execute each time the entrypoint ? Or re-use the shell open ?

Or a better way to do that ?

Or, maybe I haven't understand anything ? ;)

Thanks !


EDIT

After reading solutions proposed here, I understand that the problem is not how Docker works but how Serverspec works with ; my goal is to directly test a command as a docker run argument, but Serverspec start a container and test commands with docker exec.

So, the best solution is to found how get the stdout of the docker run executed by Serverspec.

But, in my personal use-case, the best solution is maybe to not use Gosu but --user flag :)

like image 878
Doubidou Avatar asked May 01 '17 17:05

Doubidou


People also ask

What is the ENTRYPOINT of a docker image?

Docker entrypoint is a Dockerfile directive or instruction that is used to specify the executable which should run when a container is started from a Docker image. It has two forms, the first one is the 'exec' form and the second one is the 'shell' form.

Can we use both CMD and ENTRYPOINT together?

Example of using CMD and ENTRYPOINT togetherIf 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.

What is exec $@ in docker ENTRYPOINT?

exec "$@" is typically used to make the entrypoint a pass through that then runs the docker command. It will replace the current running shell with the command that "$@" is pointing to. By default, that variable points to the command line arguments.


1 Answers

if your goal is to run the docker exec with a specific user inside of the container, you can use the --user option.

docker exec --user myuser container-name [... your command here]

If you want to run gosu every time, you can specify that as the command with docker exec

docke exec container-name gosu 1000:1000 [your actual command here]

in my experience, the best way to encapsulate this into something easily re-usable is with a .sh script (or .cmd file in windows).

drop this into a file in your local folder... maybe gs for example.

#! /bin/sh
docker exec container-name gosu 1000:1000 "$@"

give it execute permissions with chmod +x gs and then run it with ./gs from the local folder

like image 102
Derick Bailey Avatar answered Sep 18 '22 10:09

Derick Bailey