Problem :
What happens here is that you create an empty database when creating a Docker image:
mysql_install_db --user=mysql --verbose=1 --basedir=/usr --datadir=/var/lib/mysql --rpm > /dev/null && \
But when you create a container from this image, you mount the volume in the /var/lib/mysql
folder. This hides the data of your containers to open your host folder. Thus, you see an empty folder.
Solution :
If you look at https://hub.docker.com/_/mysql/ , you will see that this problem is solved by creating a database when the container actually starts, and not when the image is created. This answers your questions:
- If you start your container with the volume installed, then the database initialization will start and your data will actually be written to your FS host
- You must pass this information as env variables
In other words, start your database using a script in ENTRYPOINT
, and not directly on the image.
Warnings
Some warnings. The way you made your image is not really recommended, because Docker's philosophy is "one process per container." The difficulty that you are encountering here is that you will have to run several services in one container (apache, Mysql, etc.), so you may have to do something at both entry points, which is confusing. In addition, one service does not work, your container will still work, but it does not work as expected.
Then I would suggest splitting what you did as 1 image per process, or starting them with raw Docker, or a user like docker-compose
.
In addition, it will help you in that you can use existing and custom images from the Docker Hub: https://hub.docker.com . Less work for you and less error prone.
source share