moby: COPY can add files to an existing VOLUME but RUN can't

Description

in a Dockerfile, after a path has been declared as a VOLUME, one can’t add more files from RUN executions that will persist when volume is later created as a container is ran, but COPY can.

According to the reference documentation

Changing the volume from within the Dockerfile: If any build steps change the data within the volume after it has been declared, those changes will be discarded.

Steps to reproduce the issue:

  1. build this Dockerfile : https://github.com/ndeloof/bug
  2. run a container from it

Describe the results you received:

docker run bug
1.txt
2.txt
3.txt

Describe the results you expected:

docker run bug
1.txt
2.txt

or

docker run bug
1.txt
2.txt
3.txt
4.txt

but not something in the middle

Additional information you deem important (e.g. issue happens only occasionally):

if COPY can add files to a pre-declared VOLUME, then it would be great the same happens to RUN. Images like the official jenkins one have for sample to use some intermediate folders to let downstream images amend the volume content, with entrypoint script to copy files there.

Output of docker version:

Client:
 Version:      17.06.0-ce-rc5
 API version:  1.30
 Go version:   go1.8.3
 Git commit:   b7e4173
 Built:        Tue Jun 20 07:14:13 2017
 OS/Arch:      darwin/amd64

Server:
 Version:      17.06.0-ce-rc5
 API version:  1.30 (minimum version 1.12)
 Go version:   go1.8.3
 Git commit:   b7e4173
 Built:        Tue Jun 20 07:18:21 2017
 OS/Arch:      linux/amd64
 Experimental: true

Output of docker info:

Containers: 25
 Running: 0
 Paused: 0
 Stopped: 25
Images: 107
Server Version: 17.06.0-ce-rc5
Storage Driver: overlay2
 Backing Filesystem: extfs
 Supports d_type: true
 Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins: 
 Volume: local
 Network: bridge host ipvlan macvlan null overlay
 Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: cfb82a876ecc11b5ca0977d1733adbe58599088a
runc version: 2d41c047c83e09a6d61d464906feb2a2f3c52aa4
init version: 949e6fa
Security Options:
 seccomp
  Profile: default
Kernel Version: 4.9.31-moby
Operating System: Alpine Linux v3.5
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 1.952GiB
Name: moby
ID: 6UPI:IP6U:JJU2:JKGB:JAIG:KUHK:MOHQ:RLG6:XMTZ:W5XV:RGLC:OCFZ
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): true
 File Descriptors: 19
 Goroutines: 30
 System Time: 2017-06-27T11:34:09.862973854Z
 EventsListeners: 1
No Proxy: *.local, 169.254/16
Registry: https://index.docker.io/v1/
Experimental: true
Insecure Registries:
 127.0.0.0/8
Live Restore Enabled: false

About this issue

  • Original URL
  • State: open
  • Created 9 years ago
  • Comments: 16 (12 by maintainers)

Most upvoted comments

docker does not consider it to own that location

It is interesting that it does not do that even if the location does not exist and it had to create it! I would claim that in that case it is a bit strange that it does not consider it to own it?

And just to add to this, when image is built using BuildKit, then when running the container, the volume is still created and is seeded with data from the image.

When using BuildKit (now the default), the VOLUME keyword is a metadata-only instruction; it adds a volume property to the final image’s configuration, but unlike the classic builder, the configuration does not take effect during the build. That means that RUN statements don’t get a volume attached, and writing to a path that’s defined as a VOLUME will write to the image’s filesystem (and those files to be persisted in the image). When running the produced image (with a volume definition), a volume will be attached at the given location; if no volume is attached explicitly by the user, the Docker Engine will create an anonymous volume, and mount it instead.

if I run the image with -v to mount a host directory. It does not work even if the host directory is empty or non-existent. I think for this feature to be complete it should work with when you use host-mounted volumes as well.

This is by design; bind-mounting a location from the host means you’re granting the container access to the file(s) in that location on the host. But unlike a volume, docker does not consider it to own that location, and therefore will not populate the path using files from the image. The container is granted access to the location though, so a process inside the container is allowed to read and/or write to the path, but the Docker Engine does not perform such actions automatically. For (named) volumes, the situation is different, as the Docker Daemon (or volume driver plugin) manages the storage location of those, and therefore considered “owner” of the storage (including the ability to populate content).

You can do this with some trickery by defining a “named” volume with mount-options that define the storage location; it’s a bit of a “hack” to trick the daemon into considering it a volume (but using a bind-mount with a custom location under the hood); see this comment for things related to the above, and for an example; https://github.com/moby/moby/issues/36974#issuecomment-387053229

Yes, this is “by design”. The builder doesn’t do anything special here nor it should. The only behavior is that on container startup files from the image are copied to volume(in some cases). I think this is quite a bad feature and was very surprised when I first found out about it. But I guess reverting it isn’t an option as it has been there for a long time. For this builder case there is an easy solution to not create volumes for RUN and treat them differently than regular containers. I think that would be a good fix but wondering if maybe someone is relying on the opposite behavior atm.