docker Mount Local Directory and Data Volume Container Operations

  • 2021-09-20 21:53:32
  • OfStack

1. docker Mount Local Directory

docker supports mounting directories on 1 host machine into the image.

Run in interactive mode

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

Running in the background

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

With the-v parameter, the colon is preceded by the host directory, which must be an absolute path, and the colon is followed by the path mounted within the mirror.

Files in the host can now be shared within the mirror.

The default mounted path permissions are read and write. If it is specified as read-only, it can be used: 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, which is specially used to provide data volume for other containers to mount". It feels like 1 data mount information defined by 1 container. Other container startup 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. He is given a name with--name (if not, 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 data volume to mount data from.

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

Additional knowledge: One of the Linux family-docker localizes container configuration by mounting data volumes to mysql

Pull mysql image

docker pull mysql

Create and run mysql container (mount 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

Carry out the above instruction operation, and the result mysql container can't be started. The error report is read file/var/lib/mysql-file error.

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

Temporarily associate the local configuration direction/usr/local/docker/mysql/conf with the app folder in the container (the container configuration directory cannot be directly associated, because the container configuration directory is empty and the container cannot be started due to file synchronization), and then copy the container configuration file 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 mysql container

docker exec -it tempMysql /bin/bash

Copy all the files in the etc/mysql directory to the app directory. Because of the local synchronization before, you can see the local folder./conf has the mysql configuration file

cp -r /etc/mysql/* /app

Delete 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

Related articles: