Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "undeclare" volumes in docker image?

Tags:

docker

mysql

I am writing a Dockerfile for setting up an image for testing a web application. I am basing it on the tutum/lamp image (https://github.com/tutumcloud/tutum-docker-lamp/blob/master/Dockerfile) because that seems to be a good base to start from.

As part of my dockerfile, I want to create a mysql database and set up some stuff in it. However, the tutum/lamp image declares VOLUME ["/etc/mysql", "/var/lib/mysql" ], so if I understand correctly, any changes that I make to the MySQL database in the Dockerfile will not be persisted.

  • Do I understand that correctly?

If yes,

  • Is there a way to "undeclare" those volumes so that those directories will be part of the union file system like everything else?

Thanks!

like image 703
Mikkel Avatar asked Oct 11 '14 14:10

Mikkel


People also ask

How do I specify volumes in Dockerfile?

In Dockerfile you can specify only the destination of a volume inside a container. e.g. /usr/src/app . When you run a container, e.g. docker run --volume=/opt:/usr/src/app my_image , you may but do not have to specify its mounting point ( /opt ) on the host machine.

How do I remove a specific docker volume?

Remove one or more specific volumes - Docker 1.9 and laterUse the docker volume ls command to locate the volume name or names you wish to delete. Then you can remove one or more volumes with the docker volume rm command: List: docker volume ls.

Can I add volume to existing docker container?

Adding a Volume To a Running Docker Container Containers must have their volumes configured on startup, which means to add a new volume, you must restart the container. While there is a hacky solution (more on that below), it's highly recommended that a container restart should be done anyway.

How do I manage docker volumes?

You can manage volumes using Docker CLI commands or the Docker API. Volumes work on both Linux and Windows containers. Volumes can be more safely shared among multiple containers. Volume drivers let you store volumes on remote hosts or cloud providers, to encrypt the contents of volumes, or to add other functionality.


2 Answers

You can't really undeclare a volume, but you can build your own version of the original image by modifying its dockerfile.

like image 176
Assaf Lavie Avatar answered Sep 19 '22 17:09

Assaf Lavie


Not possible to change an existing container, so you have two options:

  1. Take the Tutum container and build your own variant
  2. Manage persistence of the tutum container using a data container.

Data containers

Create a container that creates a data volume reference:

docker run -it --name dbvol -v /var/lib/mysql ubuntu env

This can then be used when running the mysql database to persist the data:

docker run -d --volumes-from dbvol -p 3306:3306 tutum/mysql:5.6

The data persists as long as the "dbvol" container exists. It can be deleted at any stage:

docker rm dbvol

Reference:

  • http://blog.tutum.co/2014/05/27/containerize-your-database-volume-with-tutum-mysql-images/
  • https://docs.docker.com/userguide/dockervolumes/
like image 40
Mark O'Connor Avatar answered Sep 17 '22 17:09

Mark O'Connor