Bhim singh dangi
Bhim singh dangi

Reputation: 417

Mysql installed and persisting data in docker images

I am a newbie to Docker. I created a docker image with JAVA and MySQL installed in it. I tried running a normal Java application by copying it into the docker image, it ran successfully with the expected result. After that i tried to run a Java based application using mysql database. i created a database and tried executing the program, it ran successfully and i got the expected output.

But, when i closed that container and again tried to run it, it requires me to again create a new database with same name and my already existing data is lost. so, every time it opens a new mysql and i need to run it and create new database and table to store the data. Is there anyway to save my data in the image, so that every other time when i RUN the docker image, it should have the previous data stored in the same database?

Upvotes: 7

Views: 4030

Answers (3)

seanmcl
seanmcl

Reputation: 9946

If you're just starting out with docker, I would recommend mounting a local directory in your container for the database data. This assumes you'll only be running your application on a single machine, but it is easier than creating separate containers for your data. To do this, you would do something like this:

# Add VOLUMEs to allow backup of config and databases
VOLUME  ["/etc/mysql", "/var/lib/mysql"]

and do

$ docker run -v /path/to/local/etc:/etc/mysql -v /path/to/local/db:/var/lib/mysql your-image

That said, running mysql in docker is a decidedly non-trivial first step in the docker world. This is a good example:

https://github.com/tutumcloud/tutum-docker-mysql

Upvotes: 4

jripoll
jripoll

Reputation: 812

You have two ways to managing data in containers:

  • Data volumes: A data volume is a specially-designated directory within one or more containers that bypasses the Union File System to provide several useful features for persistent or shared data

  • Data volume containers: If you have some persistent data that you want to share between containers, or want to use from non-persistent containers, it's best to create a named Data Volume Container, and then to mount the data from it.

For more info see official user guide: https://docs.docker.com/userguide/dockervolumes/

Upvotes: 3

Regan
Regan

Reputation: 8781

There is a difference between image and container.

If you want to save the change you make in your container to a new image you should use

docker commit <container name or id> <optionnal image name>

If you just want to relaunch your container for more modification before commiting use:

docker start <container name or id>
docker attach <container name or id>

How to see list of all container

docker ps -a

Upvotes: 3

Related Questions