/var/lib/docker does not exist on host

If you want to mount a volume, let’s say for a MySQL database, and you want the volume data to be accessible in your Host OS file browser, and you want that data to persist regardless of what the container is doing, then this is what you want.

I may be wrong, but -v /data for example shouldn’t do anything. -v is --volume, and it’s exactly designated to do what you want it to do - mount path for shared files between the container and the host OS. But it requires 2 parameters (not 1, but 2). The first parameter specifying a location or a volume on your host OS, and the second specifying a location/resource inside your container. -v /data won’t do anything.

I use this command for my project, I restart the container all the time and even have to run a separate mysql container for another project once in a while. When ever I run this command again to start up my container for my project, after it’s booted my data shows up in the database just as I expect.

docker run --name=mysql-project \
  --rm -it --detach \
  --publish 3306:3306 \
  -e MYSQL_ROOT_PASSWORD=thepass \
  -e ALLOW_EMPTY_PASSWORD=no \
  --volume=/Users/john/Desktop/mysql-data:/bitnami/mysql \
  bitnami/mysql:5.7

--name=mysql-project \ - Sets the name of the container
--rm -it --detach \ - removes the container when I stop it, interactive mode but also it runs in the background
--publish 3306:3306 \ - The first 3306 is the port on my OS, the second 3306 is the port inside the container. Makes it so I can connect to the DB from my NodeJS project running on my Host OS via localhost:3306
-e MYSQL_ROOT_PASSWORD=thepass \ - The password for the MySQL root user
-e ALLOW_EMPTY_PASSWORD=no \ - Sometimes I need empty password == yes, so this is a placeholder
--volume=/Users/john/Desktop/mysql-data:/bitnami/mysql \ - Mounts the /bitnami/mysql folder from inside the container and connects it to the mysql-data folder on my Desktop. This folder persists as expected and I can access these files as expected
bitnami/mysql:5.7 - The image I choose to run, has JSON support and doesn’t require any other configuration to run.

I also run PHPMyAdmin to interact with the MySQL Database. Use this with the MySQL container to experiment with your data, prove to yourself that it is persisting, and see the files populate the volume on your Host OS when you run it.

docker run --name phpmyadmin \
  --rm -it --detach \
  --link mysql-project:db \
  --publish 8080:80 \
  phpmyadmin/phpmyadmin

After running both of these containers, you should have MySQL running on port 3306, and PHPMyAdmin running HTTP on port 8080. Login to PHPMyAdmin with the MySQL root user and the password that is specified in the mysql docker container command.

I hope this clears things up for some of you. I’m no expect but this has been working for me no problem.

2 Likes