docker-撰写停止/开始my_image

docker-compose stop / start my_image

执行docker-compose stop my_imagedocker-compose start my_image时容器内丢失所有数据、安装的应用程序和创建的文件夹是否正常?

我正在使用 docker-compose up --scale my_image=4

创建容器

更新号1

我的容器中有 sshd 服务器 运行。当我连接到容器执行 touch test.txt 时,我看到文件已创建。 但是,在执行 docker-compose stop my_imagedocker-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-撰写

version: '3.9'
services:
  my_image:
    build: 
      context: .
      dockerfile: Dockerfile
    ports:
    - 30000-30007:22 

当我连接到容器时

  1. 执行touch test.txt
  2. 执行docker-compose stop my_image
  3. 执行docker-compose start my_image
  4. 执行ls -l
  5. 我没有看到文件 test.txt(实际上我看到文件夹是空的)

更新号3

entrypoint.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

我没有看到文件(文件夹是空的)

我尝试其他容器以查看其中一个容器中是否存在文件,但是

我没有看到任何文件。

所以,经过残酷的暴力调试后,我意识到我丢失了数据 仅当我在停止/重新启动之前无法与 ssh 断开连接时 容器。当我断开连接时,停止/重新启动后数据不会消失