Docker Community Forums

Share and learn in the Docker community.

Is there any way to run a docker inside and then run a test/application throutgh scrpit?


(Computecqe) #1

I wish to have a shell script that should do the below.

  1. Run a docker image (which has already built).
  2. Go inside the container and then run a test/application.
  3. Once test execution is done inside container, copy the log file in local host machine.
  4. Exit from the docker container.

Can it be done?
Please guide me if yes.


(Sam) #2
docker exec container_name_or_id  command_to_issue with parms.

where do the ‘logs’ exist in the container?

docker cp

should work for copying the files from the container to the host


(Computecqe) #3

Thanks sdetweil.

Your answer only works when I have a running container.

But in my case, I do not have a running container.
First, I wish to run the image, then jump into that container and then run the test.


(Sam) #4

run the image = create container
docker exec containter_id command

or docker attach container_id
then exec command from the commandline

so, net…

script does

# start the container
id=`docker run -d --rm other parms image_name`
# run the command in the container
docker exec id "command to run and parms"
# copy the container log file out
docker cp id from_container_file  to_host_file 
# kill the container
docker kill id  (--rm will cause it to auto destroy)

(Computecqe) #6

Thanks sdetweil.
It really helpful.


(Computecqe) #7

docker exec id “executable” --> its working
But when I give changing to a different folder using this command --> its NOT working
For ex: docker exec id “cd ~/Desktop” --> its NOT working
How to resolve this issue???


(Sam) #8

what is the image you started? is it a windows container or linux or?


(Computecqe) #9

Its a linux container…
Let me tell you my wish.

I wish to run my docker image as…

  1. sudo docker run -it -d [docker image]
  2. cd /root/test (Inside docker)
  3. ./data/get_data.sh (Inside docker)
  4. ./examples/create_examples.sh (Inside docker)

How to do this?


(Sam) #10

sorry, do not quite understand…

u want to do those commands inside the container as its job? AFTER it starts?
write script, add it to the image and set the script to the entrypoint

if you want to run those from OUTSIDE after the container starts

create the script on the host
docker cp it into the container
then docker exec to execute it


(Computecqe) #11

Yes…Initially I did the same thing and issue observed.
I followed the below steps:

  1. Ran docker image in detached mode.
  2. Created a script in the host machine
  3. Copied it into the container
  4. Used docker exec command to execute it
    Found below error though the test_run.sh file is there in the path:
    sudo docker exec $id test_run.sh

rpc error: code = 13 desc = invalid header field value “oci runtime error: exec failed: container_linux.go:247: starting container process caused “exec format error”\n”


(Computecqe) #12

rpc error: code = 13 desc = invalid header field value “oci runtime error: exec failed: container_linux.go:247: starting container process caused “exec format error”\n”
error is coming everytime when I run any script from the container.

But when I run any executable which is available already in the container, its RUNNING properly.


(Sam) #13

because this is linux, you must make the script executable, right? is the host linux?

chmod +x test_run.sh & test_run.sh


(Computecqe) #14

I did chmod +x test_run.sh before copied to container.
After that ran like…
sudo docker exec $id ./test_run.sh – throws below error.
rpc error: code = 13 desc = invalid header field value “oci runtime error: exec failed: container_linux.go:247: starting container process caused “exec format error”\n”

But the same ./test_run.sh is running properly if I run in a attached container.
Throwing failures only in detached mode.


(Sam) #15

ok, i just did

doit2.sh

#!/bin/bash
echo i am here

make executable

chmod +x doit2.sh

start a docker container

 docker run -d --name foo - ubuntu sleep 1000

copy the script to the container, change name to insure not using host script

docker cp doit2.sh foo:doit.sh

note: this puts the file in the ROOT, which is NOT in the PATH list (liux for security)

exec script

docker exec foo doit.sh

fails

sam@buildserver:~$ docker exec foo doit.sh
OCI runtime exec failed: exec failed: container_linux.go:296: starting container process caused "exec: \"doit.sh\": executable file not found in $PATH": unknown

however using explicit container path

sam@buildserver:~$ docker exec foo ./doit.sh

works

i am here

check file in container

docker exec ls doit.sh -laF

returns

-rwxrwxr-x 1 1000 1000 29 Feb  7 12:06 doit.sh*

(Computecqe) #16

Thanks sdetweil…
I missed to #!/bin/bash which throws errors always.
Now my scripts are working after adding #!/bin/bash
Thanks for your kind help.


(Sam) #17

right… scripts are not executable by themselves…

that first line tells the system what application needs to be run to process the file. and the system take that and re-issues the command for you

/bin/bash scriptname parms