【发布时间】:2021-10-10 11:04:19
【问题描述】:
在这个 Postgres Docker 映像上,我复制了几个文件来配置容器。
(1) init.sh - 复制到 Docker 入口点 (2) data.txt - 包含敏感信息
在 init.sh 结束时我想删除 data.txt,但文件永远不会被删除。
docker-compose.yml
version: '3.6'
services:
postgres_test:
container_name: postgres_13_3
image: postgres_13_3
restart: unless-stopped
build:
context: ./postgres
dockerfile: postgres_13_test.dk
environment:
POSTGRES_PASSWORD: 'test'
postgres_13_test.dk
FROM postgres:13.3-alpine3.14
# copy files over
COPY ./data.txt /tmp/data.txt
RUN chmod 777 /tmp/data.txt
COPY ./init.sh /docker-entrypoint-initdb.d/init.sh
init.sh
# ... do other things first
# then in the end, delete file
rm -rf /tmp/data.txt # <-- file never gets deleted
我错过了什么?
更新
现在用 --no-cache 重建容器 fesh,现在它显示此错误消息
postgres_13_3 | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init.sh
postgres_13_3 | rm: can't remove '/tmp/data.txt': Operation not permitted
我怎样才能防止这个错误?
【问题讨论】:
-
你确定你的入口点设置正确并且脚本正在执行吗?
-
@Elia 是的,脚本正在执行。它正在创建数据库用户和表,它们都在那里。我在示例代码中省略了它。
-
我假设通过 dockerfile 复制文件是由 root 用户执行的,但 init.sh 是由 postgress 用户运行的,这就是你没有权限删除它的原因
标签: docker delete-file postgresql-13