File I/O with Containers
Overview
Teaching: 15 min
Exercises: 5 minQuestions
How do containers interact with my local file system?
Objectives
Better understand I/O with containers
Copying
Copying files between the local host and Docker containers is possible. On your local host find a file that you want to transfer to the container and then
touch io_example.txt
# If on Mac may need to do: chmod a+x io_example.txt
echo "This was written on local host" > io_example.txt
docker cp io_example.txt <CONTAINER ID>:/home/docker/data/
and then from the container check and modify it in some way
pwd
ls io_example.txt
cat io_example.txt
echo "This was written inside Docker" >> io_example.txt
/home/docker/data
io_example.txt
This was written on local host
and then on the local host copy the file out of the container
docker cp <CONTAINER ID>:/home/docker/data/io_example.txt .
and verify if you want that the file has been modified as you wanted
cat io_example.txt
This was written on local host
This was written inside Docker
Volume mounting
What is more common and arguably more useful is to mount volumes to
containers with the -v
flag.
This allows for direct access to the host file system inside of the container and for
container processes to write directly to the host file system.
docker run -v <path on host>:<path in container> <image>
For example, to mount your current working directory on your local machine to the data
directory in the example container
docker run --rm -ti -v $PWD:/home/docker/data matthewfeickert/intro-to-docker:latest
From inside the container you can ls
to see the contents of your directory on your local
machine
ls
and yet you are still inside the container
pwd
/home/docker/data
You can also see that any files created in this path in the container persist upon exit
touch created_inside.txt
exit
ls *.txt
created_inside.txt
This I/O allows for Docker images to be used for specific tasks that may be difficult to do with the tools or software installed on only the local host machine. For example, debugging problems with software that arise on cross-platform software, or even just having a specific version of software perform a task (e.g., using Python 2 when you don’t want it on your machine, or using a specific release of TeX Live when you aren’t ready to update your system release).
Flag choices
What will be the result of running the following command?
docker run --rm -v $PWD:/home/docker/data matthewfeickert/intro-to-docker:latest
Solution
Outwardly it would appear that there is no affect! You are returned to your starting terminal. However, something did happen. Look again at the flags:
--rm -v
…but no-ti
for interactive. So the container got spun up bydocker run
, wasn’t given any command and and so executed a Bash shell with/bin/bash
, wasn’t put into an interactive state and finished, and then cleaned itself up with--rm
.
Running Jupyter from a Docker Container
You can run a Jupyter server from inside of your Docker container.
First run a container (with Jupyter installed) while
exposing the container’s internal port 8888
with the -p
flag
docker run --rm -ti -p 8888:8888 matthewfeickert/intro-to-docker:latest /bin/bash
Then start a Jupyter server with the server listening on all IPs
jupyter lab --allow-root --no-browser --ip 0.0.0.0
though for your convince the example container has been configured with these default settings so you can just run
jupyter lab
Finally, copy and paste the following with the generated token from the server as
<token>
into your web browser on your local host machine
http://localhost:8888/?token=<token>
You now have access to Jupyter running on your Docker container.
Key Points
Learn how
docker cp
worksLearn about volume mounts
Show port forwarding of applications