1

I'm copy previous mariadb container's data(/var/lib/mysql) and paste data to new container image. this is Dockerfile

FROM mariadb:latest

ENV MYSQL_ROOT_PASSWORD tt
ENV MYSQL_DATABASE tt
ENV MYSQL_USER tt
ENV MYSQL_PASSWORD tt

# copy other database data
ADD mysql /var/lib/mysql
RUN chown -R mysql:mysql /var/lib/mysql

VOLUME /var/lib/mysql

EXPOSE 3306

CMD ["mysqld"]

when I build docker image, all table remained

but run docker image by using volume, all table disappear, just db.opt remains.

how can i get database's data with using volume?

HWJ
  • 13
  • 2

1 Answers1

0

Your problem it's related on how VOLUMES work, what you are doing it's adding stuff inside /var/lib/mysql at build time, and then when you run the container, a new EMPTY VOLUME is created and linked at the /var/lib/mysql, which pretty much overwrites anything you put there before.

The correct way to go about it for an existing table space would be to create a volume with the docker volume create syntax (ref: https://docs.docker.com/storage/volumes/) and then using this trick ( https://github.com/moby/moby/issues/25245#issuecomment-365980572 ) you can add the table data to your volume, and then you run your mariadb container mounting said volume docker run --mount source=myvolume,target=/var/lib/mysql mariadb:latest

I will add that you shouldn't build an image with the tables added at the docker build layer, it makes the docker image huge and it's a wrong use of it all around except for some niche cases like QA databases that you destroy afterwards or read-only databases. The usual way about it it's either using VOLUMES as you stated or with bindings from the host OS.

Then, you shouldn't use mariadb:latest either, choose a particular version and stick with it, as using mariadb:latest can upgrade/downgrade your version and cause all kind of funny bugs.

Jimmy
  • 1,115
  • 1
  • 9
  • 21
  • thanks! then when deploying service include mariadb by docker-compose, shoud i distribute db data by using separate way(such as github)? – HWJ Mar 11 '21 at 08:21
  • Usually DB data isn't stored in Docker Images or Github. Most of the time it's in stored in whatever computer is hosting the database, and you upload Backups to some kind of Object Storage (like S3 or GCS), and then from those backups you can distribute your data to other hosts. – Jimmy Mar 14 '21 at 01:05