I
I
ince2020-02-26 10:44:04
Docker
ince, 2020-02-26 10:44:04

How to deploy a copy of a stateful container?

There is a docker container with a database running on it.
Data has been entered into the database.
How to wrap the current container and run a copy of it side by side so that the original state (data in the database) is preserved.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Sokolov, 2020-02-26
@sergiks

In a good way, it is better to mount a volume with data in a docker container: keep the data separate from the engine.

docker run --name some-mysql -v /my/own/datadir:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag

### или создать дата-volume и его приделать к контейнеру
docker volume create data_volume
docker run --name some-mysql -v data_volume:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag
### так после выключения контейнера данные не пропадут, останутся в этом data_volume, 
### который можно скопировать, подключить к другим контейнерам

But, nevertheless:
Option 1.
Export data from the database to a folder on the host using mysqldumpor whatever database you have. Backups, in any case, must be done. Raise a copy of the container, import data from the dump into it. For examples of commands for mysql in docker, see the Creating database dumps section below :
### Creating database dumps
docker exec some-mysql sh -c 'exec mysqldump --all-databases -uroot -p"$MYSQL_ROOT_PASSWORD"' > /some/path/on/your/host/all-databases.sql

### Restoring data from dump files
docker exec -i some-mysql sh -c 'exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD"' < /some/path/on/your/host/all-databases.sql

Option 2.
Save the current state of the container via docker commit - you will get a new image with all the changes. Run it as a copy.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question