Docker mounts local directories and data volume container operations

Docker mounts local directories and data volume container operations

1. Docker mounts the local directory

Docker can support mounting a directory on the host into the image.

Run in interactive mode

docker run -it -v /home/dock/Downloads:/usr/Downloads ubuntu64 /bin/bash

Background operation

docker run -d -v /home/dock/Downloads:/usr/Downloads --name ubuntu1 ubuntu64

Through the -v parameter, the path before the colon is the host directory, which must be an absolute path, and the path after the colon is the path mounted in the image.

Now the files in the host machine can be shared in the image.

The default mount path permissions are read and write. If you specify it as read-only, you can use: ro

docker run -it -v /home/dock/Downloads:/usr/Downloads:ro ubuntu64 /bin/bash

2. Docker data volume container

Docker also provides an advanced usage. It's called a data volume.

Data volume: "It is actually a normal container that is specifically used to provide data volumes for other containers to mount." It feels like a data mount information defined by a container. Other container startups can directly mount the mount information defined in the data volume container.

Example:

docker run -v /home/dock/Downloads:/usr/Downloads --name dataVol ubuntu64 /bin/bash

Create a normal container. Use --name to assign a name to it (if not specified, a random name will be generated).

Create a new container to use this data volume.

docker run -it --volumes-from dataVol ubuntu64 /bin/bash

--volumes-from is used to specify which volume to mount the data from.

In this way, the /usr/Downloads directory in the newly created container will be synchronized with the host directory /home/dock/Downloads

Supplementary knowledge: Linux series - Docker realizes container configuration localization by mounting data volumes to MySQL

Pull the mysql image

docker pull mysql

Create and run the mysql container (mount the data volume to mysql)

docker run -p 3306:3306 --name mysql \
-v /usr/local/docker/mysql/conf:/etc/mysql \
-v /usr/local/docker/mysql/logs:/var/log/mysql \
-v /usr/local/docker/mysql/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123123 \
-d mysql

After executing the above instructions, the MySQL container cannot be started. The error reported is that there is an error in reading the file /var/lib/mysql-file.

###############Solve the problem that mysql cannot be started above################

Temporarily associate the local configuration directory /usr/local/docker/mysql/conf with the app folder in the container (the container configuration directory cannot be directly associated. Due to file synchronization reasons, the container configuration directory will be empty and the container cannot be started). Later, the container configuration file will be copied to ./conf to achieve the purpose of copying the container file.

docker run -d -p 3306:3306 -v /usr/local/docker/mysql/conf:/app --name tempMysql -e MYSQL_ROOT_PASSWORD=123123 mysql

Enter the mysql container

docker exec -it tempMysql /bin/bash

Copy all the files in the etc/mysql directory to the app directory. Since local synchronization was done before, you can see the mysql configuration file in the local folder ./conf

cp -r /etc/mysql/* /app

Deleting a container

docker stop tempMysql

docker rm tempMysql

Create a new container to synchronize the local MySQL folder with the MySQL-related files in the container.

docker run -p 3306:3306 --name mysql \
-v /usr/local/docker/mysql/conf:/etc/mysql \
-v /usr/local/docker/mysql/logs:/var/log/mysql \
-v /usr/local/docker/mysql/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123123 \
-d mysql

The above Docker mounts local directories and data volume container operations are all the content that the editor shares with you. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • Docker View the Mount Directory Operation of the Container
  • Implementation of mounting NFS shared directory in Docker container
  • Docker - Summary of 3 ways to modify container mount directories
  • How to mount the host directory in Docker container
  • How to use Docker to mount the container directory to the host

<<:  56 practical JavaScript tool functions to help you improve development efficiency

>>:  CSS and HTML and front-end technology layer diagram

Recommend

How to find websites with SQL injection (must read)

Method 1: Use Google advanced search, for example...

Detailed explanation of node.js installation and HbuilderX configuration

npm installation tutorial: 1. Download the Node.j...

10 Popular Windows Apps That Are Also Available on Linux

According to data analysis company Net Market Sha...

Sample code using the element calendar component in Vue

First look at the effect diagram: The complete co...

Solve the problem of managing containers with Docker Compose

In Docker's design, a container runs only one...

Example of using #include file in html

There are two files a.htm and b.htm. In the same d...

A brief discussion on the implementation principle of Webpack4 plugins

Table of contents Preface know Practice makes per...

Detailed explanation of sql_mode mode example in MySQL

This article describes the sql_mode mode in MySQL...

Quickly master the use of Docker to build a development environment

As the platform continues to grow, the project...

win10 docker-toolsbox tutorial on building a php development environment

Download image docker pull mysql:5.7 docker pull ...

Eight hook functions in the Vue life cycle camera

Table of contents 1. beforeCreate and created fun...

Introduction to the use and advantages and disadvantages of MySQL triggers

Table of contents Preface 1. Trigger Overview 2. ...