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

Will Update in a Mysql transaction lock the table?

Two cases: 1. With index 2. Without index Prerequ...

Use ab tool to perform API stress test on the server

Table of contents 1 A brief introduction to syste...

Jenkins Docker static agent node build process

A static node is fixed on a machine and is starte...

How to shut down/restart/start nginx

closure service nginx stop systemctl stop nginx s...

Solution to the problem that Docker cannot stop or delete container services

Preface Today, a developer gave me feedback that ...

We're driving IE6 to extinction on our own

In fact, we wonder every day when IE6 will really...

W3C Tutorial (8): W3C XML Schema Activities

XML Schema is an XML-based alternative to DTD. XM...

How to mark the source and origin of CSS3 citations

I am almost going moldy staying at home due to th...

React hooks pros and cons

Table of contents Preface advantage: shortcoming:...

An in-depth summary of MySQL time setting considerations

Does time really exist? Some people believe that ...

Examples of new selectors in CSS3

Structural (position) pseudo-class selector (CSS3...

How to use the Linux more command in Linux common commands

more is one of our most commonly used tools. The ...

How to create a Pod in Kubernetes

Table of contents How to create a Pod? kubectl to...

Writing a shell script in Ubuntu to start automatically at boot (recommended)

The purpose of writing scripts is to avoid having...

How to use Typescript to encapsulate local storage

Table of contents Preface Local storage usage sce...