Persistent Volumes -- Newbie Question for sure

I am trying to create a volume for LEMP application called “/data”.

My build works if I don’t mount the host machine (my Macbook) to it with the -v. I have spent 1/2 day on -v vs -mount and so on.

My understanding is if I defined a volume that the information will be copied to the Volume mount location on first boot. If I start my docker image it will come up and the files and directories are there.

If I run it with -v /MyHostData:/data then it is empty. I understand from the documentation that the host directory is mounted on top of the local directory (hence empty the first time). But isn’t it supposed to copy the volume information over to the directory on first boot.

I pushed the image up to docker: cbitterfield/lemp-ubuntu18-04:v1.1

Can someone provide some clarity on this issue?

… snip (Dockerfile)
FROM ubuntu:18.04
USER root

RUN mkdir /data
RUN mkdir /data/html
RUN mkdir /data/mysql
RUN (mkdir /data/logs; mkdir /data/logs/nginx;mkdir /data/logs/mysql)
RUN (mkdir /data/conf;mkdir /data/conf/nginx_sites-available;mkdir /data/conf/nginx_sites-enabled;mkdir /data/conf/nginx_conf.d; mkdir /data/conf/mysql;mkdir /data/conf/tls)

VOLUME /data

OSX 10.14.x (Host OS)

Client: Docker Engine - Community
Version: 19.03.5
API version: 1.40
Go version: go1.12.12
Git commit: 633a0ea
Built: Wed Nov 13 07:22:34 2019
OS/Arch: darwin/amd64
Experimental: false

Server: Docker Engine - Community
Engine:
Version: 19.03.5
API version: 1.40 (minimum version 1.12)
Go version: go1.12.12
Git commit: 633a0ea
Built: Wed Nov 13 07:29:19 2019
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: v1.2.10
GitCommit: b34a5c8af56e510852c35414db4c1f4fa6172339
runc:
Version: 1.0.0-rc8+dev
GitCommit: 3e425f80a8c931f88e6d94a8c831b9d5aa481657
docker-init:
Version: 0.18.0
GitCommit: fec3683

This is a mount-bind, not a named volume. In order to make docker copy the existing data from /data into the mounted volume, you will need to use a volume:
docker volume create --driver local -o o=bind -o type=none -o device="/MyHostData" hostdata

Then use -v hostdata:/data in your docker run command instead. You can see the volume with docker volume ls and check its declartion with docker volume inspect hostdata. If you do not create the volume beforehands, it will be a named volume located in /var/lib/docker/volumes/.

Thanks that helps a lot. Can the volume be built as part of the docker file build process so that it is integral to the image?

I am not realy sure what you want to do. You did declare the volume in your Dockerfile. If you create a container from the image and don’t mount anything into /data, docker will still create an anonymous volume, which will be visible with docker volume ls. From what I remember an anonymous volume will be delete with the container. Though, I am not 100% sure about them beeing delete, as the last time I started a container with docker run was ages ago. Declarative configuration with docker-compose.yml are the way to go for docker-compose or docker stack deploy deployments.