docker 如何在不断退出的容器中启动交互式shell?

1aaf6o9v  于 2023-05-16  发布在  Docker
关注(0)|答案(3)|浏览(211)

我使用postgres镜像启动了一个容器示例。当容器运行时,我使用
docker exec -it postgres-13.1 bash
从这里开始,我编辑了postgresql.conf,并引入了一个错字,导致尝试重新启动容器时立即退出,并且日志显示

FATAL:  configuration file "/var/lib/postgresql/data/postgresql.conf" contains errors

不可能使用docker exec -it postgres-13.1 bash执行bash脚本,因为这只适用于运行的容器。
我可以发出什么命令来启动容器并直接放入bash shell,而不是尝试启动数据库服务器?

svmlkihl

svmlkihl1#

docker cp可以将文件复制到(或复制出)已停止的容器。这应该足以修复文件并重新启动容器。

kpbwa7wx

kpbwa7wx2#

正如Chris提到的,docker cp命令适用于你的情况在你的机器上创建一个配置文件(比如correct-postgresql.conf),然后你可以启动你的容器,它工作得很好。

docker cp correct-postgresql.conf <containerId>:/var/lib/postgresql/data/postgresql.conf
qacovj5a

qacovj5a3#

关于Chris的好解决方案,更稳定的解决方案是将postgresql.conf文件挂载到主机上。这样做,你就可以在不访问运行容器的情况下更改你的postgre配置。你也不需要将docker cp conf文件放入容器中。你可以挂载.conf文件,如下所示docker run -v /home/<your-user>:/var/lib/postgresql/data/postgresql.conf ....但是如果你之前已经创建了容器,你现在应该通过/var/lib/docker/containers/<postgre-container-long-hashed-ID>/hostconfig.json文件更改容器配置:

1. docker stop <postgre-container>
2. change /var/lib/docker/containers/<postgre-container-long-hashed-ID>/hostconfig.json file as formal notation for mounting a filesystem
3. save changes to hostconfig.json file
4. sudo systemctl start docker.service
5. docker start <postgre-container>

相关问题