How can I run docker commands after "docker run" from a script?

My final goal is to start-up a docker container and move some files around in that docker container. Because this takes several commands, I want to write a script that does this automatically (as opposed to writing all these commands each time by hand). My plan here is to create a bash-script for this.

The problem starts right after the first command:

nvidia-docker run -it --name "Test" 

This put the following output on screen (in the same GUI as the Ubuntu terminal I wrote the command in):

[I 14:42:36.484 NotebookApp] Writing notebook server cookie secret to /root/.local/share/jupyter/runtime/notebook_cookie_secret
[W 14:42:36.509 NotebookApp] WARNING: The notebook server is listening on all IP addresses and not using encryption. This is not recommended.
[I 14:42:36.513 NotebookApp] Serving notebooks from local directory: /notebooks
[I 14:42:36.513 NotebookApp] 0 active kernels
[I 14:42:36.513 NotebookApp] The Jupyter Notebook is running at: ip addresses on your system]:8888/?token=6731bf8e21c987cd142076cbeb77ed3cf0f97275bcdb7bce
[I 14:42:36.513 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 14:42:36.513 NotebookApp] 

Now I can 'exit' this by typing ctrl+C, which allows me to go 'back' to the original terminal (still, same GUI) and start typing Ubuntu terminal commands again. But how do I replicate this action in a script?

7

1 Answer

I think what you're missing is the -d parameter to run it in the background

docker run -d --name "Test"

Doing that starts up the container without transferring you into it. So that way your script can continue to run other commands.

Now if you need to actually "move some files around" within the container, what you can do is mount your script as a volume and run it.

docker run -d -v$(pwd):/my --name Test bash
docker exec -d Test bash /my/script.sh

So that way you mount your preset working directory into the container and then within the container run that script, while continuing along in your original script. so then add

docker exec -it Test bash

and now you are in your container, after having run the script.

or, you can just run several docker exec commands instead of mounting a folder.

#!/bin/bash
docker run -d --name Test bash
docker exec -d Test bash cp /x /y
docker exec -d Test bash cp /y /z
docker exec -it Test bash

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like