141

I am learning Docker and I have doubts about when and where to use ADD and VOLUME. Here is what I think both of these do:

ADD

Copy files to the image at build time. The image has all the files so you can deploy very easily. On the other hand, needing to build every time doesn't look like a good idea in development because building requires the developer to run a command to rebuild the container; additionally, building the container can be time-consuming.

VOLUME

I understand that using docker run -v you can mount a host folder inside your container, this way you can easily modify files and watch the app in your container react to the changes. It looks great in development, but I am not sure how to deploy my files this way.

3
  • 3
    In general, it's best to prefer COPY to ADD. They're almost the same, but ADD has some extra abilities wrt URLs and archive files that can be surprising. Jan 3, 2015 at 12:56
  • 2
    @jamesmstone - that link (and the official docker docs) recommend the opposite -- use COPY rather than ADD. Dec 20, 2016 at 23:47
  • oops, you're right - cheers! Jul 10, 2018 at 1:05

2 Answers 2

232

ADD

The fundamental difference between these two is that ADD makes whatever you're adding, be it a folder or just a file actually part of your image. Anyone who uses the image you've built afterwards will have access to whatever you ADD. This is true even if you afterwards remove it because Docker works in layers and the ADD layer will still exist as part of the image. To be clear, you only ADD something at build time and cannot ever ADD at run-time.

A few examples of cases where you'd want to use ADD:

  • You have some requirements in a requirements.txt file that you want to reference and install in your Dockerfile. You can then do: ADD ./requirements.txt /requirements.txt followed by RUN pip install -r /requirements.txt
  • You want to use your app code as context in your Dockerfile, for example, if you want to set your app directory as the working dir in your image and to have the default command in a container run from your image actually run your app, you can do:

    ADD ./ /usr/local/git/my_app

    WORKDIR /usr/local/git/my_app

    CMD python ./main.py

VOLUME

Volume, on the other hand, just lets a container run from your image have access to some path on whatever local machine the container is being run on. You cannot use files from your VOLUME directory in your Dockerfile. Anything in your volume directory will not be accessible at build-time but will be accessible at run-time.

A few examples of cases where you'd want to use VOLUME:

  • The app being run in your container makes logs in /var/log/my_app. You want those logs to be accessible on the host machine and not to be deleted when the container is removed. You can do this by creating a mount point at /var/log/my_app by adding VOLUME /var/log/my_app to your Dockerfile and then running your container with docker run -v /host/log/dir/my_app:/var/log/my_app some_repo/some_image:some_tag
  • You have some local settings files you want the app in the container to have access to. Perhaps those settings files are different on your local machine vs dev vs production. Especially so if those settings files are secret, in which case you definitely do not want them in your image. A good strategy in that case is to add VOLUME /etc/settings/my_app_settings to your Dockerfile, run your container with docker run -v /host/settings/dir:/etc/settings/my_app_settings some_repo/some_image:some_tag, and make sure the /host/settings/dir exists in all environments you expect your app to be run.
4
  • 20
    By far the most useful post I found so far on ADD and VOLUME
    – Jasmeet
    Jan 18, 2018 at 2:14
  • 6
    What happens if VOLUME is specified, yet not provided during docker run (e.g. the -v xxx parameter is missing)? Is the resp. VOLUME then effectively becoming transient?
    – col.panic
    Mar 21, 2018 at 8:41
  • Inside a Dockerfile, volumes are probably meant only for persistence and/or debugging, but you can use the volume command line switch to get an app into an existing image (no Dockerfile needed) and run it like this docker run -v $HOST_PATH:$CONTAINER_PATH node:latest node $CONTAINER_PATH/app.js. Nov 11, 2018 at 22:43
  • nice "layer" detail Feb 11, 2019 at 22:04
32

The VOLUME instruction creates a data volume in your Docker container at runtime. The directory provided as an argument to VOLUME is a directory that bypasses the Union File System, and is primarily used for persistent and shared data.

If you run docker inspect <your-container>, you will see under the Mounts section there is a Source which represents the directory location on the host, and a Destination which represents the mounted directory location in the container. For example,

"Mounts": [
  {
    "Name": "fac362...80535",
    "Source": "/var/lib/docker/volumes/fac362...80535/_data",
    "Destination": "/webapp",
    "Driver": "local",
    "Mode": "",
    "RW": true,
    "Propagation": ""
  }
]

Here are 3 use cases for docker run -v:

  1. docker run -v /data: This is analogous to specifying the VOLUME instruction in your Dockerfile.
  2. docker run -v $host_path:$container_path: This allows you to mount $host_path from your host to $container_path in your container during runtime. In development, this is useful for sharing source code on your host with the container. In production, this can be used to mount things like the host's DNS information (found in /etc/resolv.conf) or secrets into the container. Conversely, you can also use this technique to write the container's logs into specific folders on the host. Both $host_path and $container_path must be absolute paths.
  3. docker run -v my_volume:$container_path: This creates a data volume in your container at $container_path and names it my_volume. It is essentially the same as creating and naming a volume using docker volume create my_volume. Naming a volume like this is useful for a container data volume and a shared-storage volume using a multi-host storage driver like Flocker.

Notice that the approach of mounting a host folder as a data volume is not available in Dockerfile. To quote the docker documentation,

Note: This is not available from a Dockerfile due to the portability and sharing purpose of it. As the host directory is, by its nature, host-dependent, a host directory specified in a Dockerfile probably wouldn't work on all hosts.

Now if you want to copy your files to containers in non-development environments, you can use the ADD or COPY instructions in your Dockerfile. These are what I usually use for non-development deployment.

8
  • 3
    Should I create 2 docker files? One for development and one for deployment? Jan 2, 2015 at 0:12
  • I don't think so. There is nothing wrong with having the ADD instruction in your Dockerfile, as it is executed only by the docker build command. This is needed when others build your container for the first time, and when you are ready to deploy it to other non-development environments.
    – ivan.sim
    Jan 2, 2015 at 0:14
  • 3
    But wouldn't it be more efficient to build an image without the files and use the -v command for development, and have another docker file create an image that includes the files with ADD for deployment? Jan 2, 2015 at 0:31
  • 1
    It's a trade-off you have to decide. Pick what works for you. How long does the build with an ADD takes anyway? Couple seconds in total? If you have two Dockerfile files, and you share it with others (or publish it on docker registry), which one is the default? You will have some extra maintenance overhead to ensure the right default Dockerfile gets to the right users. But at the end of the day, you decide what works best for you. Personally, I like to ensure that there is one and only one Dockerfile to build my container.
    – ivan.sim
    Jan 2, 2015 at 0:59
  • 14
    By the way I think it's fine to first ADD, then override that add with -v for development. That way you won't need separate Dockerfiles. Nov 25, 2015 at 8:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.