Docker run vs docker build

Say I have a dockerfile that initializes a certain directory during the build phase.

When I run it from the docker hub like: docker run -v /some/local/directory:/var/lib/mysql nameoffile:latest
the files get initialized in the proper directory on my home pc.
But when I build the file locally from a Dockerfile with the docker build command, and then run the image that was created, the image has already been initialized with the directory /var/lib/mysql in a docker volume at /var/lib/docker/volumes/sdjhsdjfsd67… and my intended destination for that directory is empty.

So how do I run or build a local Dockerfile with a volume pointing to a location where I want it to be?

Best thing to do is to COPY (or maybe ADD) a copy of that data in, before you declare the VOLUME.

# Before you start, create mysql.tar.gz in the current directory, then...
ADD mysql.tar.gz /var/lib/mysql
VOLUME ["/var/lib/mysql"]

Then the initial copy of the volume data (provided you don’t force a host directory to be mounted over it) will have whatever was in that tar file.

You can’t provide or mount any volumes during a docker build. Any content that goes into the image generally should be in the context directory.

(Since you mention /var/lib/mysql, will the off-the-shell mysql or mariadb containers work for your application, without building a custom image?)

I am trying to build a simple php+apache and mysql docker combination. I cannot get the ones from the docker hub to work with phpmyadmin.

Ok, I found out what to do:

The trick is to make a separate script that initializes any directories/volumes and starts whatever deamon the dockerfile was built for. Then in the end of the dockerfile with CMD, run that script.