Trying to copy a script into a detached Docker container, and execute it with docker exec

Viewed 22

Right now I am setting my Docker instance running with:

sudo docker run --name docker_verify --rm \
  -t -d daoplays/rust_v1.63

so that it runs in detached mode in the background. I then copy a script to that instance:

sudo docker cp verify_run_script.sh docker_verify:/. 

and I want to be able to execute that script with what I expected to be:

sudo docker exec -d docker_verify bash \
  -c "./verify_run_script.sh"

However, this doesn't seem to do anything. If from another terminal I run

sudo docker container logs -f docker_verify

nothing is shown. If I attach myself to the Docker instance then I can run the script myself but that sort of defeats the point of running in detached mode.

I assume I am just not passing the right arguments here, but I am really not clear what I should be doing!

1 Answers

When you run a command in a container you need to also allocate a pseudo-TTY if you want to see the results.

Your command should be:

sudo docker exec -t docker_verify bash \
  -c "./verify_run_script.sh"

(note the -t flag)

Steps to reproduce it:

# create a dummy script
cat > script.sh <<EOF
echo This is running!
EOF

# run a container to work with
docker run --rm --name docker_verify -d alpine:latest sleep 3000

# copy the script
docker cp script.sh docker_verify:/

# run the script
docker exec -t docker_verify sh -c "chmod a+x /script.sh && /script.sh"

# clean up
docker container rm -f docker_verify

You should see This is running! in the output.

Related