Docker - Summary of 3 ways to modify container mount directories

Docker - Summary of 3 ways to modify container mount directories

Method 1: Modify the configuration file (need to stop the docker service)

1. Stop the docker service

systemctl stop docker.service (critical, the docker service must be stopped before modification)

2. vim /var/lib/docker/containers/container-ID/config.v2.json

Modify the directory location in the configuration file, then save and exit

"MountPoints":{"/home":{"Source":"/docker","Destination":"/home","RW":true,"Name":"","Driver":"","Type":"bind","Propagation":"rprivate","Spec":{"Type":"bind","Source":"//docker/","Target":"/home"}}}

3. Start the docker service

systemctl start docker.service

4. Start the Docker container

docker start <container-name/ID>

Method 2: Submit an existing container as a new image and then rerun it

$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
 5a3422adeead ubuntu:14.04 "/bin/bash" About a minute ago Exited (0) About a minute ago agitated_newton
$ docker commit 5a3422adeead newimagename
$ docker run -ti -v "$PWD/dir1":/dir1 -v "$PWD/dir2":/dir2 newimagename /bin/bash

Then stop the old container, and use this new one. If for some reason you need the new container to use the old name, use docker rename after removing the old container.

Method 3: Export the container as an image, then import it as a new image

$docker container export -o ./myimage.docker container ID
$docker import ./myimage.docker newimagename
$docker run -ti -v "$PWD/dir1":/dir1 -v "$PWD/dir2":/dir2 newimagename /bin/bash

Then stop the old container, and use this new one. If for some reason you need the new container to use the old name, use docker rename after removing the old container.

Additional knowledge: How to prevent data loss after Docker restarts, teach you how to mount data volumes

When you use Docker to deploy web applications or MySQL databases, you will find that when the container is restarted, the logs or database data generated during the container operation will be cleared. So how do we save this data?

This requires understanding how Docker mounts the host disk directory to permanently store data.

1. Execute Docker Volume when creating a container

Use the docker run command to run a Docker container, use the image ubuntu/nginx, and mount the local directory /tmp/source to the container directory /tmp/destination

docker run -itd --volume /tmp/source:/tmp/destination --name test ubuntu/nginx bash

A Docker container is created based on the ubuntu/nginx image.

The name of the specified container is test, which is specified by the --name option.

The Docker Volume is specified by the --volume (can be abbreviated as -v) option, and the host's /tmp/source directory corresponds one-to-one with the /tmp/destination directory in the container.

2. View Docker Volume

Use the docker inspect command to view detailed information about the Docker container:

docker inspect --format='{{json .Mounts}}' test | python -m json.tool[{"Destination": "/tmp/destination","Mode": "","Propagation": "","RW": true,"Source": "/tmp/source","Type": "bind"}]

Using the --format option, you can selectively view the required container information. .Mount is the Docker Volume information of the container.

python -m json.tool can format the output json string for display.

Source represents the directory on the host, that is, /tmp/source.

Destination is the directory in the container, that is, /tmp/destination.

3. Local files can be synchronized to the container

Create a new hello.txt file in the local /tmp/source directory

touch /tmp/source/hello.txtls /tmp/source/hello.txt

The hello.txt file is visible in the container /tmp/destination/ directory

Using the docker exec command, you can execute commands in a container.

docker exectest ls /tmp/destination/hello.txt

Therefore, modifications to the directory /tmp/source/ on the host can be synchronized to the container directory /tmp/destination/.

4. Container files can be synchronized to the host machine

Create a new world.txt file in the container /tmp/destination directory

docker exec test touch /tmp/destination/world.txtdocker exec test ls /tmp/destination/hello.txtworld.txt

The world.txt file is visible in the host machine's /tmp/source/ directory

ls /tmp/source/hello.txt world.txt

The above summary of 3 methods of docker-modifying container mount directories is 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:
  • How to modify the contents of an existing Docker container
  • 5 ways to migrate Docker containers to other servers
  • How to view files in Docker image
  • docker cp copy files and enter the container

<<:  Causes and solutions for cross-domain issues in Ajax requests

>>:  A brief discussion on the comparison of varchar, char and text in postgresql database

Recommend

MySQL index cardinality concept and usage examples

This article uses examples to explain the concept...

Javascript Virtual DOM Detailed Explanation

Table of contents What is virtual dom? Why do we ...

Angular framework detailed explanation of view abstract definition

Preface As a front-end framework designed "f...

Introduction to fork in multithreading under Linux

Table of contents Question: Case (1) fork before ...

The url value of the src or css background image is the base64 encoded code

You may have noticed that the src or CSS backgroun...

Detailed explanation of 8 ways to pass parameters in Vue routing components

When we develop a single-page application, someti...

Vue implements simple production of counter

This article example shares the simple implementa...

MySQL 8.0.21 free installation version configuration method graphic tutorial

Six steps to install MySQL (only the installation...

Teach you to connect to MySQL database using eclipse

Preface Since errors always occur, record the pro...

The best solution for implementing digital plus and minus buttons with pure CSS

Preface: For the implementation of digital additi...

How to use VirtualBox to simulate a Linux cluster

1. Set up HOST on the host Macbook The previous d...

Detailed analysis of several situations in which MySQL indexes fail

1. Leading fuzzy query cannot use index (like ...

How to use worker_threads to create new threads in nodejs

Introduction As mentioned in the previous article...

Implement a simple search engine based on MySQL

Table of contents Implementing a search engine ba...