简体   繁体   English

MySQL Docker 容器在首次运行后存储密码

[英]MySQL Docker Container storing password after first run

All my experience with Docker so far has led me to believe that containers are stateless.到目前为止,我使用 Docker 的所有经验都让我相信容器是无状态的。

If so, why is my container storing the password that I change it to after the first run if I spun it up without specifying a volume or bind mount?如果是这样,如果我在没有指定卷或绑定安装的情况下启动它,为什么我的容器会存储我在第一次运行后更改的密码? I am especially puzzled since none of the other edits I make to the dbms persist (like creating tables).我特别困惑,因为我对 dbms 所做的其他编辑都没有持续存在(比如创建表)。

Additional Details:额外细节:
Versions:版本:
1. Docker - 18.09.0 build 4d60db4 1. Docker - 18.09.0 构建 4d60db4
2. Image - mysql/mysql-server:latest 2.镜像-mysql/mysql-server:latest

Commands:命令:
1. $ docker run --name=sql -d mysql/mysql-server:latest 1. $ docker run --name=sql -d mysql/mysql-server:latest
2. $ docker logs sql 2>&1 | grep GENERATED 2. $ docker logs sql 2>&1 | grep GENERATED $ docker logs sql 2>&1 | grep GENERATED to grab the generated password for first login $ docker logs sql 2>&1 | grep GENERATED获取第一次登录时生成的密码
3. $ docker exec -it sql mysql -uroot -p 3. $ docker exec -it sql mysql -uroot -p
4. mysql> Enter Password: <generated password> 4. mysql> Enter Password: <generated password>
5. mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'stkoverflw'; 5. mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'stkoverflw';
6. mysql> exit mysql> exit
7. $ docker stop sql 7. $ docker stop sql
8. $ docker start sql 8. $ docker start sql
9. $ docker exec -it sql mysql -uroot -p 9. $ docker exec -it sql mysql -uroot -p
10. mysql> Enter Password: <stkoverflw> 10. mysql> Enter Password: <stkoverflw>

How does the password configuration persist across restarts of the container?密码配置如何在容器重新启动后保持不变?

Containers are not stateless.容器不是无状态的。 Containers are easy to create and destroy, so they can be used to run a service which is stateless, but each container is itself stateful.容器易于创建和销毁,因此它们可用于运行无状态的服务,但每个容器本身都是有状态的。

When the container is running, there is a volume containing its root filesystem.当容器运行时,有一个包含其根文件系统的卷。 You don't have to tell Docker to create it.您不必告诉 Docker 创建它。 Docker has to create it because otherwise where do the container's files go? Docker 必须创建它,否则容器的文件会去哪里?

When you say docker stop , the container stops running but it is not destroyed.当您说docker stop ,容器会停止运行,但不会被销毁。 When you say docker start , the same container resumes with the same root volume.当你说docker start ,同一个容器以同一个根卷恢复。 That's where the changed password persists.这就是更改后的密码仍然存在的地方。 The process running in the container was stopped and a new process was started (so state held in memory would be lost), but the filesystem is still there.容器中运行的进程停止并启动一个新进程(因此内存中的状态将丢失),但文件系统仍然存在。

To get rid of a container (including the changed password), say docker rm .要摆脱容器(包括更改的密码),请说docker rm Then you can say docker run to start from scratch.然后你可以说docker run从头开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM