Docker Learning Note Four: Managing data in a container

Source: Internet
Author: User
Tags docker run
To add a data volume

You can use the Docker create and Docker run commands in conjunction with-V to add data volumes to the container. You can mount a data volume by using-V multiple times:

$ docker run-d-P--name web-v/webapp training/webapp python app.py
mount the host directory as a data volume
$ docker run-d-P--name web-v/src/webapp:/opt/webapp training/webapp python app.py

This command mounts the host directory/src/webapp to the/opt/webapp directory of the container web, and if/opt/webapp already exists in the mirror,/src/webapp overwrites the mount, but does not remove the previously existing content, and once the mounted directory is removed, The original data will be available again.

The directory within the container must be an absolute path, such as/opt/webapp, where the host directory can be an absolute path or a name value, if the host directory is an absolute path, the Docker binding mounts the specified path, and if a name,docker is used to create a volume named by name. If the board of the host you want to mount does not exist, Docker will create it automatically.

Docker mounts volumes in read-write mode by default in a read-only manner:

$ docker run-d-P--name web-v/src/webapp:/opt/webapp:ro training/webapp python app.py
to mount a shared storage volume as a data volume

Use the Docker Run command to create a volume that is called My-named-volume, which is driven by a Flocker volume:

$ docker run-d-p \
--volume-driver=flocker \-
v my-named-volume:/opt/webapp
--name Web Training/webapp Python app.py

Create a volume with Docker volume creates before using in the container:

$ docker Volume create-d Flocker-o SIZE=20GB my-named-volume
$ docker run-d-p \
v My-named-volume:/op/webapp \
--name Web Training/webapp python app.py
creating a Mounted data volume container

Use the docker create command to create a data volume container that uses--volumes-from to mount data volumes in other containers in the Docker Run command:

$ docker create-v/dbdata--name dbstore training/postgres/bin/true
$ docker run-d--volumes-from Dbstore--name db 1  training/postgres
Backup, recovery, migration data volumes
$ docker Run--RM--volumes-from dbstore-v $ (PWD):/backup ubuntu  Tar cvf/backup/backup.tar/dbdata
– RM container exits automatically when containers are deleted

This command runs a new container, mounts the volume from the Dbstore container, then mounts the host's current directory to the container's/backup, then executes the tar command, backs up the/dbdata volume's data to/backup/backup.tar, the command completes, the container ends, A backup of the dbdata volume will be obtained.

Recover data:

Create a new container

$ docker run-v/dbdata--name dbsotre2 Ubuntu/bin/bash

Unzip the backup file in a new container data volume

$ docker run-rm--volumes-from dbstore2-v $ (PWD):/backup ubuntu bash-c "Cd/dbdata && tar Xvf/backup/backup.ta R--strip 1 "

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.