Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent database docker from being rebuilt and losing production data

I love using docker & docker-compose for both development and production environments.

But in my workflow, I keep considering dockers as disposable:

it means if I need to add a feature to my docker, I edit my Dockerfile, then run docker-compose build and docker-compose up -d and done.

But this time, the production DB is also in a Docker.

I still need to make some changes in my environment (eg. configuring backup), but now I can't rerun docker-compose build because this would imply a loss of all the data... It means I need to enter the docker (docker-compose run web /bin/bash) and run the commands inside it while still reporting those to my local Dockerfile to keep track of my changes.

Are there any best practices regarding this situation?

I thought of setting up a process that would dump the DB to a S3 bucket before container destruction, but it doesn't really scale to wide DBs...

I thought of making a Docker not destructible (how?), though it means losing the disposability of a container.

I thought of having a special partition that would be in charge of storing the data only and that would not get destructed when rebuilding the docker, though it feels hard to setup and unsecure.

So what?

Thanks

like image 400
Augustin Riedinger Avatar asked Feb 10 '17 09:02

Augustin Riedinger


People also ask

How can you persist the data when working with a database container?

With the database being a single file, if we can persist that file on the host and make it available to the next container, it should be able to pick up where the last one left off. By creating a volume and attaching (often called “mounting”) it to the directory the data is stored in, we can persist the data.

How do you store data persistently in Docker?

Volumes are the best way to persist data in Docker. Bind mounts may be stored anywhere on the host system. They may even be important system files or directories. Non-Docker processes on the Docker host or a Docker container can modify them at any time.

Why do I lose data if Docker restarts?

Docker does not persist data if a container stop exists, note that many people got confused and believe that docker has data persistence without any configuration because they stop a container and they see that their data is still there when they restart the container, but in docker terms the container exists even if ...

Should you use Docker for database in production?

If you're working on a small project, and are deploying to a single machine, it's completely okay to run your database in a Docker container. Be sure to mount a volume to make the data persistent, and have backup processes in place. Try to restore them every once in a while to make sure your backups are any good.


1 Answers

This is what data volumes are for. There is a whole page on the docker documentation site covering this.

The idea is that when you destroy the container, the data volume persists with data on it, and when you restart it the data hasn't gone anywhere. I will say though, that putting databases in docker containers is hard. People have done it and had severe dataloss and severe jobless.

I would recommend reading extensively on this topic before trusting your production data to docker containers. This is a great article explaining the perils of doing this.

like image 147
jaxxstorm Avatar answered Oct 25 '22 00:10

jaxxstorm