How to implement Docker volume mounting

How to implement Docker volume mounting

The creation of the simplest hello world output image is the easiest start, but if we need to modify our running code, or have input and output files, we have no way out. After each container is finished running, its allocated resources and file system will disappear, so we need to mount a volume to the docker container locally, so that when the container is running, file operations under the container's mount point will also affect the local volume mounted to the container. Just like mount in linux. In this way, we only need to modify the code in the local volume without modifying the image.

For example, we create a Dockerfile:

in:

VOLUME: creates a mount point in the container. We can mount a local file volume under the container's /data, so that our operations in the container's data directory are the same as those on the local volume.

CMD: The command to be run when the container is started. There can only be one.

The operation of the created image is to count the information in a file and then input it into 2.txt. Use $docker build –t count:v1 ./ to create an image and view it

When running, we need to add -v /root/laipeng.han/file:/data to mount the local file /root/laipeng.han/file under the container's /data, then all files under /root/laipeng.han/file can be seen under the container's /data/.

The running command is: docker run -v /root/laipeng.han/file:/data count:v1

The final container outputs the result file 2.txt under /data/, which can also be seen under /root/laipeng.han/file/. In this way, we can modify the countfile.py code under /root/laipeng.han/file/ and run it directly without creating a new image file.

The above codes have been tested personally, please feel free to run them. I hope they will be helpful for your study, and I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • Introduction to container data volumes in Docker
  • Two ways to manage volumes in Docker
  • Docker volume deletion operation
  • Docker volumes file mapping method
  • Docker Data Storage Volumes Detailed Explanation
  • Docker volume usage details and examples
  • Docker writes data to the data volume

<<:  Detailed explanation of the use of props in React's three major attributes

>>:  CocosCreator implements skill cooling effect

Recommend

Detailed explanation of how to create an updateable view in MySQL

This article uses an example to describe how to c...

MySQL 5.6.24 (binary) automatic installation script under Linux

This article shares the mysql5.6.24 automatic ins...

JavaScript implementation of verification code case

This article shares the specific code for JavaScr...

Method of building docker private warehouse based on Harbor

Table of contents 1. Introduction to Harbor 1. Ha...

Setting up shared folders in Ubuntu virtual machine of VMWare14.0.0

This is my first blog post. Due to time constrain...

A practical record of encountering XSS attack in a VUE project

Table of contents Preface Discover the cause Cust...

Textarea tag in HTML

<textarea></textarea> is used to crea...

Understanding what Node.js is is so easy

Table of contents Official introduction to Node.j...

Example of building a redis-sentinel cluster based on docker

1. Overview Redis Cluster enables high availabili...

Basic usage and pitfalls of JavaScript array sort() method

Preface In daily code development, there are many...

vue front-end HbuliderEslint real-time verification automatic repair settings

Table of contents ESLint plugin installation in H...

MySQL Database Basics: A Summary of Basic Commands

Table of contents 1. Use help information 2. Crea...

Analysis of MySQL joint index function and usage examples

This article uses examples to illustrate the func...

MySQL character set garbled characters and solutions

Preface A character set is a set of symbols and e...