From 3e96f46b30340f8468a7bac8c159b610a2309821 Mon Sep 17 00:00:00 2001 From: Maxime Petazzoni Date: Mon, 11 Nov 2013 13:29:56 -0800 Subject: [PATCH 1/2] Automatically create non-existent bind-mounted directories on the host Fixes #1279. Docker-DCO-1.1-Signed-off-by: Maxime Petazzoni (github: mpetazzoni) --- server.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/server.go b/server.go index 331c596411..db26441bc1 100644 --- a/server.go +++ b/server.go @@ -1909,8 +1909,11 @@ func (srv *Server) ContainerStart(job *engine.Job) engine.Status { // ensure the source exists on the host _, err := os.Stat(source) if err != nil && os.IsNotExist(err) { - job.Errorf("Invalid bind mount '%s' : source doesn't exist", bind) - return engine.StatusErr + err = os.MkdirAll(source, 0755) + if err != nil { + job.Errorf("Could not create local directory '%s' for bind mount: %s!", source, err.Error()) + return engine.StatusErr + } } } // Register any links from the host config before starting the container From 47d1413d7a0850edc64c62f1473b4c4a3f417d7c Mon Sep 17 00:00:00 2001 From: Maxime Petazzoni Date: Fri, 6 Dec 2013 10:43:54 -0800 Subject: [PATCH 2/2] Documentation about automatic bind-mount dir creation Docker-DCO-1.1-Signed-off-by: Maxime Petazzoni (github: mpetazzoni) --- docs/sources/reference/commandline/cli.rst | 11 ++++++++++- docs/sources/use/working_with_volumes.rst | 12 ++++++++---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/docs/sources/reference/commandline/cli.rst b/docs/sources/reference/commandline/cli.rst index f6318326e2..c00a97d5c4 100644 --- a/docs/sources/reference/commandline/cli.rst +++ b/docs/sources/reference/commandline/cli.rst @@ -1100,7 +1100,16 @@ using the container, but inside the current working directory. .. code-block:: bash - $ sudo docker run -p 127.0.0.1:80:8080 ubuntu bash + $ sudo docker run -v /dont/exist:/foo -w /foo -i -t ubuntu bash + +When the host directory of a bind-mounted volume doesn't exist, Docker +will automatically create this directory on the host for you. In the +example above, Docker will create the ``/dont/exist`` folder before +starting your container. + +.. code-block:: bash + + $ sudo docker run -p 127.0.0.1:80:8080 ubuntu bash This binds port ``8080`` of the container to port ``80`` on ``127.0.0.1`` of the host machine. :ref:`port_redirection` explains in detail how to manipulate ports diff --git a/docs/sources/use/working_with_volumes.rst b/docs/sources/use/working_with_volumes.rst index d908157473..82d5806954 100644 --- a/docs/sources/use/working_with_volumes.rst +++ b/docs/sources/use/working_with_volumes.rst @@ -89,11 +89,15 @@ Mount a Host Directory as a Container Volume: :: -v=[]: Create a bind mount with: [host-dir]:[container-dir]:[rw|ro]. - If "host-dir" is missing, then docker creates a new volume. -This is not available from a Dockerfile as it makes the built image less portable -or shareable. [host-dir] volumes are 100% host dependent and will break on any -other machine. +If ``host-dir`` is missing from the command, then docker creates a new volume. +If ``host-dir`` is present but points to a non-existent directory on the host, +Docker will automatically create this directory and use it as the source of the +bind-mount. + +Note that this is not available from a Dockerfile due the portability and +sharing purpose of it. The ``host-dir`` volumes are entirely host-dependent and +might not work on any other machine. For example::