Docker volume binding: Base image vs derivative im

2019-05-29 13:27发布

问题:

It looks like any volume binding (-v) used when you run a base image will not be retained in the derived image that you commit, and the volume binding option has to be passed each time you run the derived image.

Example:


docker run -it -v /opt/hostappsdir:/apps ubuntu

make changes in the container (named: john_doe) and commit to a new image (local/test)

docker commit john_doe local/test

Then, this does not list the files in the host directory /opt/hostappsdir

docker run --rm local/test ls /apps

but this one does it

docker run --rm -v /opt/hostappsdir:/apps local/test ls /apps

Is there any way I can avoid passing the volume binding option each time?

回答1:

Volumes are by definition not part of the layers of the container and thus will not be part of the layers you commit when creating a Image from the container.

If you like to e.g. add you specific configuration, add some extensions, add some packages, you should always derive from the base image, never just "commit" arbitrary, not formalised changes.

Create a Dockerfile with

from <baseimage>
COPY yourconfig /etc/somewhere
COPY yourasset /var/www

RUN sed ... \ // change some configuration
  && apt-get update && apt-get install curl // some packages

Hint: I do myself not like the idea of "commiting changes a containers to an image" - that is risky, does not produce the same result every time and more over, does not formalise what you actually changed. You will have hard times .e.g updating your image due to updates of the base image, since you do not know, what you have done - and even if you have written it down, its a lot of effort to do that again, again and again.



标签: docker