KansaiRobot
KansaiRobot

Reputation: 9912

Difference in Volumes in docker run and COPY in dockerfile

If I do something like

docker run -v /opt/datadir:/var/lib/mysql image

I am mapping some location inside the container to a location in the host.

How is this different to the command COPY used when writing a Dockerfile?

Upvotes: 0

Views: 366

Answers (1)

Kapil Khandelwal
Kapil Khandelwal

Reputation: 1176

The major difference is seen in case we edit any of the file present inside that location.

Suppose the directory /opt/datadir contains a file temp.txt

In case of bind mount, if you try to edit the file temp.txt from the host machine, the changes will be reflected inside the container and vice-versa.

When we create COPY command in Dockerfile, it copies the content to the filesystem of the container. Hence any changes done inside the container are DOES NOT affect the files present on the host machine. In this case, if you want changes done on the host machine to be reflected inside the container, then you need to build a docker image and run a new container using the updated image.

When to use what?

For scenarios where the resource needs frequent updates, use bind mounts.

Eg: We want to provide our web server a configuration file that might change frequently.

In case the resource is independent of host filesystem, use COPY command inside dockerfile.

Eg: .tar, .zip, .war files or any file that requires no or very few updates inside the container.

Upvotes: 1

Related Questions