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

How to enable Flash in Windows Server 2016

I recently deployed and tested VMware Horizon, an...

How to implement mobile web page size adaptation

I finally finished the project at hand, and the m...

Analysis of two implementation methods for adding static routing in Linux

Command to add a route: 1.Route add route add -ne...

Introduction to HTML basic controls_PowerNode Java Academy

The <input> tag The <input> tag is us...

Docker Compose practice and summary

Docker Compose can realize the orchestration of D...

Native JS to achieve sliding button effect

The specific code of the sliding button made with...

Build a Docker image using Dockerfile

Table of contents Build a Docker image using Dock...

Vue imports Echarts to realize line scatter chart

This article shares the specific code of Vue impo...

Implementation of vue+drf+third-party sliding verification code access

Table of contents 1. Background 2. Verification p...

js implements mouse switching pictures (without timer)

This article example shares the specific code of ...

js implementation of verification code case

This article example shares the specific code of ...

Semantics, writing, and best practices of link A

The semantics, writing style, and best practices ...

HTML left and right layout example code

CSS: Copy code The code is as follows: html,body{ ...