Tuesday, December 28, 2021

[SOLVED] How to docker exec a shell builtin of docker container specifically on Ubuntu docker image/container

Issue

thank you for reading my post.

Problem:

# docker ps
CONTAINER ID        IMAGE                  COMMAND             
35c8b832403a        ubuntu1604:1   "sh -c /bin/sh"    

# docker exec -i -t 35c8b832403a type type
rpc error: code = 2 desc = oci runtime error: exec failed: container_linux.go:262: starting container process caused "exec: \"type\": executable file not found in $PATH"

# Dockerfile
FROM ubuntu:16.04

ENV PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
RUN apt-get update && apt-get -y upgrade
ENTRYPOINT ["sh", "-c"]
CMD ["/bin/bash"]

Description:

My objective is to get "type" shell builtin been execute in a way of writing docker exec as below

docker exec -i -t 35c8b832403a type type (FAILED)

NOT

docker exec -i -t 35c8b832403a sh -c "type type" (PASSED)

I have googling around, do some modification in the container (change /etc/profile, /etc/environment, bashrc) but failed.

From the docker documentation itself, it has state that:

COMMAND will run in the default directory of the container. It the underlying image has a custom directory specified with the WORKDIR directive in its Dockerfile, this will be used instead.

COMMAND should be an executable, a chained or a quoted command will not work. Example: docker exec -ti my_container "echo a && echo b" will not work, but docker exec -ti my_container sh -c "echo a && echo b" will.

But seem it IS POSSIBLE when I able to get the right output FROM DOCKER FEDORA (Dockerfile: FROM fedora:25)

# docker ps
CONTAINER ID        IMAGE                  COMMAND             
2a17b2338518        fedora25:1   "sh -c /bin/sh" 

# docker exec -i -t 2a17b2338518 type type
type is a shell builtin

Question:

Is there any way to enable this on Ubuntu docker? Image/Container tweaks? Vagrantfile Configuration? Please help.

Others:

Using docker run, I able to get the right output because of the "ENTRYPOINT" in the Dockerfile. However the image need to be save instead of export.


Solution

Just in case, to be able to execute type as you expect, it would need to be part of the path. Being a shell builtin wouldn't help because as you said, you don't want to execute /bin/bash -c 'type type'

If you want to have type executed as a builtin shell command, this means you need to execute a shell /bin/bash or /bin/sh and then execute 'type type' on it, making it /bin/bash -c 'type type'

After all, as @Henry said, docker exec is a the full command that will be executed and there is no place for CMD or ENTRYPOINT on it.



Answered By - txomon