We’ll learn in this tutorial the differences between using VOLUME, COPY and ADD when configuring Dockerfiles.
They all accomplish the same task (among other capabilities): moving files from the host computer to a Docker container.
VOLUME is different from
ADD because it creates a mountpoint that the host operating system can interact with.
VOLUME /Users/sjobs/Documents/cool-project /var/www
This command syncs the Docker container’s
/var/www directory with the host OS’s
cool-project directory. When any change is made to
cool-project within the host OS it is immediately made available to the docker container mounting that directory and vice versa.
VOLUME is especially useful for development environments where frequent modifications to the code are being made. For example running
nodemon on a Docker container with a
VOLUME allows you to make changes to your code that are immediately reflected as if Docker were invisible.
COPY accomplishes everything that
VOLUME accomplishes, but does it during build time. This is necessary for configuring containers with modified config files with services such as httpd, nginx, mongodb, etc… For example when configuring an nginx container modifying the
nginx.conf file is an operation that must be done with
One setback to
COPY is that it makes only the container that was copied to aware of the contents. Meaning we cannot access the contents on our host computer, or on other running docker containers.