【发布时间】:2021-12-18 14:17:22
【问题描述】:
在执行docker-compose stop my_image和docker-compose start my_image时丢失容器内所有数据、安装的应用程序和创建的文件夹是否正常?
我正在使用docker-compose up --scale my_image=4 创建容器
更新编号。 1
我的容器中运行着 sshd 服务器。当我连接到容器执行touch test.txt 时,我看到文件已创建。
但是,在执行docker-compose stop my_image 和docker-compose start my_image 后,容器为空,ls -l 显示缺少文件 test.txt
更新编号。 2
我的 Dockerfile
FROM oraclelinux:8.5
RUN (yum update -y; \
yum install -y openssh-server openssh-clients initscripts wget passwd tar crontabs unzip; \
yum clean all)
RUN (ssh-keygen -A; \
sed -i 's/UsePAM yes/#UsePAM yes/g' /etc/ssh/sshd_config; \
sed -i 's/#UsePAM no/UsePAM no/g' /etc/ssh/sshd_config; \
sed -i 's/#PermitRootLogin yes/PermitRootLogin yes/' /etc/ssh/sshd_config; \
sed -i 's/#PasswordAuthentication yes/PasswordAuthentication yes/' /etc/ssh/sshd_config)
RUN (mkdir -p /root/.ssh/; \
echo "StrictHostKeyChecking=no" > /root/.ssh/config; \
echo "UserKnownHostsFile=/dev/null" >> /root/.ssh/config)
RUN echo "root:oraclelinux" | chpasswd
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
EXPOSE 22
我的 docker-compose
version: '3.9'
services:
my_image:
build:
context: .
dockerfile: Dockerfile
ports:
- 30000-30007:22
当我连接到容器时
- 执行
touch test.txt - 执行
docker-compose stop my_image - 执行
docker-compose start my_image - 执行
ls -l - 我看不到文件
test.txt(实际上我看到文件夹是空的)
更新编号。 3
入口点.sh
#!/bin/sh
# Start the ssh server
/usr/sbin/sshd -D
# Execute the CMD
exec "$@"
其他详情
当容器全部启动并运行时,我选择一个容器运行
在特定端口上,例如port 30001,然后使用putty 连接到该特定容器,
执行touch test.txt
执行ls -l
我确实看到文件已创建
我执行docker-compose stop my_image
我执行docker-compose start my_image
我通过 putty 连接到 port 30001
我执行ls -l
我没有看到文件(文件夹为空)
我尝试使用其他容器来查看其中一个容器中是否存在文件,但是
我看不到文件。
【问题讨论】:
-
不应该那样做,因为它不会重新创建容器
Starts existing containers for a service.docs.docker.com/engine/reference/commandline/compose_start -
您是否在
docker-compose exec调试shell 中手动安装应用程序?不管docker-compose start的具体语义如何,这种做法丢工作是极其正常的;您应该在 Dockerfile 中为自定义映像安装应用程序,如果可能,尝试将数据存储在容器外部(例如,在数据库中),如果不能,仅将数据存储在挂载的 @987654349 @ 目录(不要使用它覆盖应用程序代码)。 -
话虽如此,您能否编辑问题以包含minimal reproducible example;你的镜像的 Dockerfile 中有什么,
docker-compose.yml中有什么,主容器进程是什么,你怎么知道事情正在丢失? -
当您再次启动容器时,入口点脚本会再次运行。你的入口点脚本是做什么的?
标签: docker docker-compose