From charlesreid1

Line 36: Line 36:
==Mounting Read-Only Directories==
==Mounting Read-Only Directories==


Read only control can be set on volumes as well: <code>--read-only</code> flag. This flag makes the entire contents of the container's root filesystem read-only, except for volumes mounted with the <code>-v</code> flag. To illustrate:
<pre>
$ # this will not work, because /canttouchthis is part of the container's root filesystem
$ docker run -t --read-only -v /icanwrite busybox touch /canttouchthis
touch: /canttouchthis: Read-only file system
$ # this will work, because /icanwrite is mounted with -v and is not read-only
$ docker run --read-only -v /icanwrite busybox touch /icanwrite/here
$ # no error, no poblem!
</pre>


=Patterns=
=Patterns=

Revision as of 02:52, 25 March 2017

Basics

Working Directory

First, we can set the working directory when we run a container by using the -w flag:

$ docker  run -w /path/to/dir/ -i -t  ubuntu pwd

This starts a new ubuntu image called pwd with the current working directory (when it starts up) set to /path/to/dir.

Setting Disk Space

We can set the amount of storage for the docker container using --storage-opt flag:

$ docker run -it --storage-opt size=120G fedora /bin/bash

This starts a fedora image with a bash shell, and uses 120 gb for the container.

Mounting Host Folders

Can mount folders on the host machine to drives in the docker machine:

$ docker  run  \
  -v `pwd`:`pwd` \
  -w `pwd` \
  -i -t  ubuntu pwd

This starts a docker container with the name "pwd", and mounts the current working directory (say, /home/someone/docker) to the same path in the docker image /home/someone/docker. It then sets the working directory to that directory.

Mounting Read-Only Directories

Read only control can be set on volumes as well: --read-only flag. This flag makes the entire contents of the container's root filesystem read-only, except for volumes mounted with the -v flag. To illustrate:

$ # this will not work, because /canttouchthis is part of the container's root filesystem
$ docker run -t --read-only -v /icanwrite busybox touch /canttouchthis
touch: /canttouchthis: Read-only file system

$ # this will work, because /icanwrite is mounted with -v and is not read-only
$ docker run --read-only -v /icanwrite busybox touch /icanwrite/here

$ # no error, no poblem!

Patterns

This section covers some patterns for organizing files so that you can move things in and out from containers, while also keeping in line with the philosophy behind docker containers, which is that they should be stateless.


Flags