bash - sed 不能在 dockerfile 中工作,但在容器 bash 中可以

标签 bash image docker sed dockerfile

我正在创建一个 Docker 镜像,我在其中使用 sed 修改两个参数,但是当我创建镜像并检查我想要修改的文件时,它仍然是相同的。如果我以交互方式运行非常 sed 命令,它就可以工作。为什么? sombebody 可以帮助我使 ma image 工作而不必修改每个容器吗?

docker 文件

FROM python:slim-buster

WORKDIR /home/scr_dca

COPY . . 

ENV FLASK_APP Screenly.py

RUN apt-get update && \
    apt install curl gnupg -y && \
    curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - && \
    curl https://packages.microsoft.com/config/debian/10/prod.list > /etc/apt/sources.list.d/mssql-release.list && \
    apt-get update && ACCEPT_EULA=Y apt-get install msodbcsql17 unixodbc-dev -y && \
    apt-get install libgssapi-krb5-2 g++ gcc && \
    pip3 install -r requirements.txt --trusted-host pypi.python.org 

RUN sed -i "s/\(MinProtocol *= *\).*/\1TLSv1.0 /" "/etc/ssl/openssl.cnf" && \
    sed -i "s/\(CipherString *= *\).*/\1DEFAULT@SECLEVEL=1 /" "/etc/ssl/openssl.cnf" 

CMD ["gunicorn", "-b", ":8000", "scr_dca:app"]

我在做

docker run --name mycontainer -d -p 5050:8000 src_dca_v1.0 
docker container exec -it mycontainer bash
:/home/myapp#  cat /etc/ssl/openssl.cnf

我检查并发现 sed 在图像创建期间没有工作,所以我运行了以下命令:

sed -i "s/\(MinProtocol *= *\).*/\1TLSv1.0 /" "/etc/ssl/openssl.cnf"
sed -i "s/\(CipherString *= *\).*/\1DEFAULT@SECLEVEL=1 /" "/etc/ssl/openssl.cnf"

我要修改的文件的原始部分:

[system_default_sect]
MinProtocol = TLSv1.2
CipherString = @SECLEVEL=1

sed 预期结果

[system_default_sect]
MinProtocol = TLSv1.0
CipherString = DEFAULT@SECLEVEL=1 

最佳答案

我怀疑您没有看到某些东西,或者您没有在问题中解释/描述。照原样,我无法重现您的问题。

我的 MCVE,灵感来自您当前要测试的问题:

FROM python:slim-buster

RUN cp /etc/ssl/openssl.cnf /etc/ssl/openssl.cnf.ORI && \
    sed -i "s/\(MinProtocol *= *\).*/\1TLSv1.0 /" "/etc/ssl/openssl.cnf" && \
    sed -i "s/\(CipherString *= *\).*/\1DEFAULT@SECLEVEL=1 /" "/etc/ssl/openssl.cnf" && \
    (diff -u /etc/ssl/openssl.cnf.ORI /etc/ssl/openssl.cnf || exit 0)

注意:我忽略了 diff 退出状态并将其强制为 0,因为当文件之间存在差异而导致构建失败时它将以状态 1 退出。

结果:

$ docker build --no-cache -t test:test .
Sending build context to Docker daemon  4.096kB
Step 1/2 : FROM python:slim-buster
 ---> 3d8f801fc3db
Step 2/2 : RUN cp /etc/ssl/openssl.cnf /etc/ssl/openssl.cnf.ORI &&     sed -i "s/\(MinProtocol *= *\).*/\1TLSv1.0 /" "/etc/ssl/openssl.cnf" &&     sed -i "s/\(CipherString *= *\).*/\1DEFAULT@SECLEVEL=1 /" "/etc/ssl/openssl.cnf" &&     (diff -u /etc/ssl/openssl.cnf.ORI /etc/ssl/openssl.cnf || exit 0)
 ---> Running in 523ddc0f4025
--- /etc/ssl/openssl.cnf.ORI    2020-01-09 16:21:44.667348574 +0000
+++ /etc/ssl/openssl.cnf    2020-01-09 16:21:44.675348574 +0000
@@ -358,5 +358,5 @@
 system_default = system_default_sect

 [system_default_sect]
-MinProtocol = TLSv1.2
-CipherString = DEFAULT@SECLEVEL=2
+MinProtocol = TLSv1.0 
+CipherString = DEFAULT@SECLEVEL=1 
Removing intermediate container 523ddc0f4025
 ---> 88c28529ceb5
Successfully built 88c28529ceb5
Successfully tagged test:test

如您所见,diff 显示了运行 sed 之前/之后的差异以及您期望的修改。

我们还可以确保从这个镜像启动容器时这些修改仍然存在:

$ docker run -it --rm --name testcmd test:test bash -c "grep -A 2 '\[system_default_sect\]' /etc/ssl/openssl.cnf"
[system_default_sect]
MinProtocol = TLSv1.0 
CipherString = DEFAULT@SECLEVEL=1

关于bash - sed 不能在 dockerfile 中工作,但在容器 bash 中可以,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59667598/

相关文章:

docker - 在 ARM 上构建 X86 docker 镜像?

mysql - 防止公共(public)访问使用 Docker-compose 运行的 MySQL 端口

bash - 用于导入的 sqlite 字段分隔符

bash - 获取 bash 历史到 vi

linux - 如何为行编写 while 循环

ios - UI 导航栏图像标题

javascript - jQuery load() 来自不同页面的图像并在脚本中使用它们

linux - uniq 命令未检测到重复行

java - 从 2D 颜色阵列创建图像

linux - 由于用户名无效,Azure VM 创建失败