[Docker] How to modify files in Docker

1. How to modify files in Docker

Maybe the Docker container you installed does not have editing commands such as vi and vim. Of course you can't install it, but it's too troublesome to install it every time you create a new container and every editing requirement. So how do you edit files in Docker? There are 3 common ways:

  • echo command mode

  • Use docker cpthe method of copying files back and forth

  • How to mount hosts and containers

Each method has its own advantages. The echo command is simple and convenient, the docker cp method is more versatile, and the mounting method is easy to persist.

Method 1, echo command method

Advantages: 1. Each container has it. 2. Compared with other methods, it is simple and convenient enough.

step:

  • Enter the container
docker exec -it lpg-promtail-1 /bin/bash
  • Use cat to view source content
cat /etc/promtail/config.yml
  • Copy edit new content
  • echo new content to overwrite
# 把编辑后的内容输出了...处开始覆写
echo '...' > /etc/promtail/config.yml

Method 2, copy files back and forth

  • Copy files in the container to the host
# docker cp 源 目标
docker cp elasticsearch_8.7.1:/usr/share/elasticsearch/config/elasticsearch.yml elasticsearch.yml
  • Modify files on the host

  • Copy the files on the host back to the container and overwrite the original files in the container

# 只要把docker cp 的源和目标反过来就行了
docker cp elasticsearch.yml elasticsearch_8.7.1:/usr/share/elasticsearch/config/elasticsearch.yml
  • If you need to restart the container after modifying the file, remember to restart the container.

Method 3, mounting method

You can mount files on the host into the container through -v commands and specify them when starting the container. For example:

docker run -p 3306:3306 --name mysql \
-v /mydata/mysql/log:/var/log/mysql \
-v /mydata/mysql/data:/var/lib/mysql \
-v /mydata/mysql/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=root  \
-d mysql:5.7

The above mounts the host's /mydata/mysql/conf folder to the container var/log/mysqldirectory.

The biggest advantage of this method is that after deleting the container, the file still exists in the host. Next time you start a new container, you may still be able to use it without having to reconfigure the environment and restore data. This is especially friendly for containers that require persistence, such as mysql . MySQL persistence in Docker, how Docker persists data

2. References

My article: "How to check which versions of a Docker image.md"

My article: "Docker sets domestic image source.md"

My article: "Docker Quick Start Practical Tutorial.md"

My article: "Docker installs MySQL, Redis, RabbitMQ, Elasticsearch, Nacos and other common services.md"

My article: "Docker installs Nacos service.md"

My article: "How to modify file .md in Docker"

My article: "Connection or communication methods between Docker containers.md"

My article: "How to persist database data with MySQL installed by Docker.md"

My article: "Making Docker Private Repository.md"

My article: "Using the docker-maven-plugin plug-in to build and publish push images to private warehouses.md"

My article: "Resolving Docker's failure to access port 9200 after installing Elasticsearch.md"


Portal: Nanny-style Spring5 source code analysis

Welcome to exchange technology and work life with the author

Contact the author

Guess you like

Origin blog.csdn.net/yuchangyuan5237/article/details/131799778