sed在dockerfile中不起作用,但在容器bash中起作用

altdave

我正在创建一个Docker映像,其中使用sed修改了两个参数,但是当我创建映像并检查文件时,我想修改它的余量是一样的。如果我以交互方式运行非常sed命令,则它可以工作。为什么?sombebody可以帮助我使ma图像工作而不必修改每个容器。

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 
Zeitounator

我怀疑您看不到某些内容,或者您​​没有在问题中进行解释/描述。照原样,我无法重现您的问题。

我的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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章