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

HTML mouse css control

Generally speaking, the mouse is displayed as an u...

JS implements layout conversion in animation

When writing animations with JS, layout conversio...

Introduction to query commands for MySQL stored procedures

As shown below: select name from mysql.proc where...

Detailed explanation of the solution to font blur when using transform in CSS3

This question is very strange, so I will go strai...

MySQL knowledge points for the second-level computer exam mysql alter command

Usage of alter command in mysql to edit table str...

Introduction to Vue life cycle and detailed explanation of hook functions

Table of contents Vue life cycle introduction and...

A brief discussion on event-driven development in JS and Nodejs

Table of contents Event-driven and publish-subscr...

Sample code for installing Jenkins using Docker

Two problems that are easy to encounter when inst...

How to view the storage location of MySQL data files

We may have a question: After we install MySQL lo...

Pure js to achieve the effect of carousel

This article shares the specific code of js to ac...

Steps to enable MySQL database monitoring binlog

Preface We often need to do something based on so...

Detailed explanation of Docker Secret management and use

1. What is Docker Secret 1. Scenario display We k...

Native JS to implement sharing sidebar

This article shares a sharing sidebar implemented...

CSS3 uses transform to create a moving 2D clock

Now that we have finished the transform course, l...

Summary of examples of common methods of JavaScript arrays

Table of contents Common array methods concat() M...