0

Dockerfile

FROM ubuntu:18.04

RUN rm /bin/sh && ln -s /bin/bash /bin/sh

RUN apt-get update && apt-get install -y \
curl \
git \
make \
sudo 

RUN sudo useradd -m -p $(openssl passwd -1 1) me

USER me
WORKDIR /home/me

RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash \
&& . $HOME/.nvm/nvm.sh && nvm install 8.12.0

RUN mkdir myvol && touch myvol/TEST

VOLUME myvol

Then build the image:

docker build ./ -t nodejs

Then start new container:

docker run -it --rm --name nodejs nodejs:latest bash

Now i can see that i have one anonymous volume created in the docker space:

/var/lib/docker/volumes/39459a1b2cf16e9d2dc2fe8fa5261186a02af75410a0e63f39e17e6e63b3ffe0

when i do:

sudo ls -asl /var/lib/docker/volumes/39459a1b2cf16e9d2dc2fe8fa5261186a02af75410a0e63f39e17e6e63b3ffe0/_data

I dont see the TEST file! The unnamed volume is empty!

Like the Docs say:

The docker run command initializes the newly created volume with any data that exists at the specified location within the base image.

Why i dont see the pre-existing content in the volume?

1 Answer 1

1

The VOLUME specification in the Dockerfile requires an absolute path rather than a relative one:

VOLUME /home/me/myvol

That said, I tend to recommend against declaring a volume inside of a Dockerfile since it breaks downstream users ability to extend the image and results in anonymous volumes when users do not explicitly declare the volume as you see here. Instead a docker-compose.yml example is preferred.

I also recommend against using the names like nodejs:latest for your image since it could conflict with upstream images and someone seeing that may assume it was an official docker image. Use an image name with your username prefixed like linderman/nodejs:latest to avoid potential conflicts.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .