Manage data in Docker

Source: Internet
Author: User

Manage data in Docker

So far, we have introduced some basic Docker concepts, learned how to use Docker's image, and how to communicate with multiple containers over the network. this chapter describes how to manage data in the docker container and how to share data between different containers.

We will introduce two main methods for managing data in docker:

  • Data volumes

  • Data volume container

Data volumes

OneData volumeIs a special usage directory in one or more containers. It bypassesUnion File SystemThe following are some useful features for persistent data and shared data:

  • Data volumes can share and reuse Data between different containers.

  • Modifications to Data volume take effect immediately (TRANSLATOR: data volumn is a directory where multiple containers are mounted. You can view volumne information through docker inspect)

  • Changes to data volume are not included when the image is upgraded (TRANSLATOR: The image is stateless throughout the docker design, which is very beneficial to the upgrade and reuse. Data marked with status, such as database data and generated logs, should be stored in volume. The persistence and restoration of volume are described below, in the form of files rather than images)

  • Volumes persistence until no iner uses them

New virtualization options for the open-source project Docker and Red Hat

Dockerlite: lightweight Linux Virtualization

Detailed explanation of the entire process of building Gitlab CI for Docker

What is the difference between Docker and a normal Virtual Machine?

Use MySQL in Docker

Docker will change everything

Official version of Docker 1.0 is released and downloaded

Add volume

You can use-v to add a data volume during docker run. This parameter can be used multiple times to add multiple data volumes during docker run. Let's mount a volume for our web application container.

$ sudo docker run -d -P --name web -v /webapp training/webapp python app.py

Here a new volume will be created to the/webapp in the container. (TRANSLATOR: If you log on to a shell of your iner through ssh or through-I, you can use ls/webapp to verify that the mounting is successful)

Note: You can also add the volume field in the Dockerfile, so that when you create a new image container, a new volume will be automatically created.

Install a directory as a data volume

Using-v can not only create a new volume, but also mount a directory of the host machine to the container.

$ sudo docker run -d -P --name web -v /src/webapp:/opt/webapp training/webapp python app.py

This command will mount the local directory/src/webapp to the/opt/webapp directory in the container. It is very convenient to use this method to test the program. For example, we can mount our source code to the container using this method, after modifying the local code, you can immediately see how the modified Code works in the container. The Directory of the host must be an absolute path. If the directory does not exist, docker will automatically create it for you.

Note that Dockerfile cannot be used here, because such usage is incompatible with portability and sharing. because the local directory is related to the local directory and may not work on all hosts. (TRANSLATOR: the ghost knows what the host looks like when you use an image)

By default, Docker sets volume to read/write, but we can also mount a directory to read-only:

$ sudo docker run -d -P --name web -v /src/webapp:/opt/webapp:ro training/webapp python app.py

Here we also mount the/src/webapp directory, but we added the ro parameter to tell docker that the volume is read-only.

For more details, please continue to read the highlights on the next page:

  • 1
  • 2
  • Next Page

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.