How to execute Docker image entry point in each exec command?

After trying to test Dockerfiles with Dockerspec, I finally had a problem. I cannot solve it correctly.

The problem is what I think from Docker himself; If I understand its process, the entry point is only executed at startup , but if the container remains on and I run the exec command, it will not be called again.

I think this is the desired behavior.

But if Entrypoint is a gosu "script that precedes all my commands, this is the problem ...


Example

"myImage" has this entry point: gosu 1000:1000 " $@ "

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

The output signal is "1000".

If I run the container: docker run -it myImage bash

In this container id -u displays "1000".

But if I run a new command in this container, it starts a new shell and would not execute Entrypoint, therefore: docker exec CONTAINER_ID id -u

Print "0" because the new shell starts as "root".


Is there a way to execute an input point each time? Or reuse the shell open?

Or is the best way to do this?

Or maybe I didn’t understand anything ?;)

Thanks!


EDIT

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

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

But, in my personal case, the best solution might be to not use Gosu, but the user’s flag :)

+5
source share
1 answer

if your goal is to run docker exec with a specific user inside 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 this as a 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 rewritable with a .sh script (or a .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 " $@ " 

grant execution rights using chmod +x gs , and then run it using ./gs from a local folder

+2
source

Source: https://habr.com/ru/post/1267362/


All Articles