Docker Community Forums

Share and learn in the Docker community.

Not able to run a script or binary from a container after login to a new user

Hi,

I want to execute a shell script/binary through /bin/bash from the container after logging to new user. But after running the docker run command i am able to
logged into new user but unable to run a script or binary from the container.

I an using Ubuntu 16.04 as host PC and 18.09.02 docker version. I have created a docker file and two startup scripts(startup.sh and startup2.sh). 1st script is used to create a user and login to the new user. 2nd script is used to execute a user defined
binary from the new container.

I have created a test folder and created docker and startup scripts. Below is the code for my Dockerfile and two startup scripts.:

Dockerfile:

	FROM ubuntu:16.04
	
	LABEL maintainer "Deepak Singh <deepak_sing@hcl.com>"
	
	COPY ./startup.sh /startup.sh
	
	COPY ./startup2.sh /startup2
	
	ENTRYPOINT ["/startup.sh"]
	
	CMD ["/bin/bash /startup.sh"]
	
	CMD ["/bin/bash /startup2.sh"]

startup.sh:
#!/bin/bash
echo create new user
useradd -ms /bin/bash new-user
su new-user
echo start second startup script
source /startup2.sh
exec $@

startup2.sh:
#!/bin/bash
echo Execute a binary
/bin/cat /startup.sh
exec $@

I have build the docker with the below command:

root@lx511aa865140:~/docker_exp# docker build -t docker_exp .
Sending build context to Docker daemon 5.632kB
Step 1/7 : FROM ubuntu:16.04
—> 9361ce633ff1
Step 2/7 : LABEL maintainer “Deepak Singh deepak_sing@hcl.com
—> Using cache
—> c1f2d3efd1bf
Step 3/7 : COPY ./startup.sh /startup.sh
—> 02eb3bb50855
Step 4/7 : COPY ./startup2.sh /startup2.sh
—> cd3981943a3d
Step 5/7 : ENTRYPOINT ["/startup.sh"]
—> Running in 8ff900809ebd
Removing intermediate container 8ff900809ebd
—> 1e8c5739d249
Step 6/7 : CMD ["/bin/bash /startup.sh"]
—> Running in aab26e432e97
Removing intermediate container aab26e432e97
—> ddff176a0054
Step 7/7 : CMD ["/bin/bash /startup2.sh"]
—> Running in f1dd425bb46e
Removing intermediate container f1dd425bb46e
—> f8b36fa1a67d
Successfully built f8b36fa1a67d
Successfully tagged docker_exp:latest
root@lx511aa865140:~/docker_exp#

As i have told after running the docker only new user is getting logged but second startup script is not getting executed. Below is output of docker run:
Used below command to run the docker:

root@lx511aa865140:~/docker_exp#
root@lx511aa865140:~/docker_exp# docker run --rm -it --name docker_exp-test docker_exp /bin/bash
Create new user
new-user@94ba3c53b7cd:/ new-user@94ba3c53b7cd:/
new-user@94ba3c53b7cd:/ ls -l /bin/cat -rwxr-xr-x 1 root root 52080 Mar 2 2017 /bin/cat new-user@94ba3c53b7cd:/
new-user@c92d4093d437:/ new-user@c92d4093d437:/ ls -l
total 80
drwxr-xr-x 2 root root 4096 Feb 22 10:06 bin
drwxr-xr-x 2 root root 4096 Apr 12 2016 boot
drwxr-xr-x 5 root root 360 Apr 29 06:50 dev
drwxr-xr-x 1 root root 4096 Apr 29 06:50 etc
drwxr-xr-x 1 root root 4096 Apr 29 06:50 home
drwxr-xr-x 8 root root 4096 Sep 13 2015 lib
drwxr-xr-x 2 root root 4096 Feb 22 10:06 lib64
drwxr-xr-x 2 root root 4096 Feb 22 10:05 media
drwxr-xr-x 2 root root 4096 Feb 22 10:05 mnt
drwxr-xr-x 2 root root 4096 Feb 22 10:05 opt
dr-xr-xr-x 198 root root 0 Apr 29 06:50 proc
drwx------ 2 root root 4096 Feb 22 10:06 root
drwxr-xr-x 1 root root 4096 Feb 22 10:06 run
drwxr-xr-x 1 root root 4096 Mar 12 00:21 sbin
drwxr-xr-x 2 root root 4096 Feb 22 10:05 srv
-rwxrwxrwx 1 root root 342 Apr 29 06:15 startup.sh
-rwxrwxrwx 1 root root 104 Apr 29 06:12 startup2.sh
dr-xr-xr-x 13 root root 0 Apr 29 04:40 sys
drwxrwxrwt 2 root root 4096 Feb 22 10:06 tmp
drwxr-xr-x 1 root root 4096 Feb 22 10:05 usr
drwxr-xr-x 1 root root 4096 Feb 22 10:06 var
new-user@c92d4093d437:/$

Note: Below is the dcoker version and Host PC details :slight_smile:

root@lx511aa865140:~/docker_exp#
root@lx511aa865140:~/docker_exp# docker version
Client:
Version: 18.09.2
API version: 1.39
Go version: go1.10.4
Git commit: 6247962
Built: Tue Feb 26 23:56:24 2019
OS/Arch: linux/amd64
Experimental: false

Server:
Engine:
Version: 18.09.2
API version: 1.39 (minimum version 1.12)
Go version: go1.10.4
Git commit: 6247962
Built: Tue Feb 12 22:47:29 2019
OS/Arch: linux/amd64
Experimental: false
root@lx511aa865140:~/docker_exp#

root@lx511aa865140:~/docker_exp#
root@lx511aa865140:~/docker_exp# uname -a
Linux lx511aa865140 4.15.0-46-generic #49~16.04.1-Ubuntu SMP Tue Feb 12 17:45:24 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
root@lx511aa865140:~/docker_exp#