sed内联替换不适用于Dockerfile [英] sed inline replacement not working from Dockerfile

查看:176
本文介绍了sed内联替换不适用于Dockerfile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为杯子创建一个Docker映像,我要执行的步骤之一是注释掉 Listen指令。示例构建的输出结果说明了这个问题:

I'm trying to create a Docker image for cups, and one of the steps I want to perform is to comment out the "Listen" directives. This output from a sample build demonstrates the issue:

Step 8 : RUN sed "s/^Listen /#Listen /" /etc/cups/cupsd.conf | grep Listen
 ---> Running in 6b9dfeeaec7a
#Listen localhost:631
#Listen /var/run/cups/cups.sock
 ---> 1737a534589d
Step 9 : RUN sed -i "s/^Listen /#Listen /" /etc/cups/cupsd.conf
 ---> Running in aacd768eb94c
 ---> dbe12c2073ef
Step 10 : RUN grep "Listen" /etc/cups/cupsd.conf
 ---> Running in 650b27ecf7c4
Listen localhost:631
Listen /var/run/cups/cups.sock
 ---> bf03b9f5de35
Successfully built bf03b9f5de35

为什么第一次调用 sed可以通过管道传递到 grep 正常工作,但是带有 -i 标志的第二个则不能进行任何更改吗?

Why does the first call to sed that gets piped to grep work perfectly, but the second with the -i flag not make any changes?

如果我以交互方式运行容器( docker run -it cups sh )并复制并粘贴命令( sed -i s / ^ Listen /#Listen / /etc/cups/cupsd.conf ),然后按预期进行更改。

If I run the container interactively (docker run -it cups sh) and copy and paste the command (sed -i "s/^Listen /#Listen /" /etc/cups/cupsd.conf) then the change is made as expected.

以下是完整的Dockerfile:

Here's the full Dockerfile:

FROM ubuntu:16.04

RUN apt-get --quiet update && apt-get --quiet --assume-yes --allow-downgrades --allow-remove-essential --allow-change-held-packages dist-upgrade

# Install cups
RUN apt-get install --quiet --assume-yes --allow-downgrades --allow-remove-essential --allow-change-held-packages cups

VOLUME /etc/cups/

EXPOSE 631

RUN apt-get install --quiet --assume-yes --allow-downgrades --allow-remove-essential --allow-change-held-packages less vim

RUN sed -i "s/^Listen /#Listen /" /etc/cups/cupsd.conf

构建命令是: docker build --tag cups:test。

运行命令是: docker run -it cups:test bash

docker信息输出:

Containers: 9
 Running: 6
 Paused: 0
 Stopped: 3
Images: 61
Server Version: 1.12.1
Storage Driver: aufs
 Root Dir: /mnt/storage/var/lib/docker/aufs
 Backing Filesystem: extfs
 Dirs: 65
 Dirperm1 Supported: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: host bridge null overlay
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Security Options: apparmor
Kernel Version: 4.4.0-31-generic
Operating System: Ubuntu 16.04.1 LTS
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 3.555 GiB
Name: servy
ID: LJZV:A2VY:BZPY:HHQX:DWSP:IAMK:XI43:Q7BA:YKVU:ONQX:VBHJ:GXRT
Docker Root Dir: /mnt/storage/var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
WARNING: No swap limit support
Insecure Registries:
 127.0.0.0/8

docker --version 的输出:

Docker version 1.12.1, build 23cf638

谢谢

推荐答案

问题出在:

VOLUME /etc/cups/

在此行之后,可能不会在最终容器中看到对/ etc / cups的任何更改(某些单文件复制命令确实适用,因此并不完美)。

After that line, any changes to /etc/cups may not be seen in the final container (some single file copy commands do apply, so it's not perfect).

将音量线移至末尾,或者最好将其从图像中完全删除。这些条目阻止以后通过更改此文件夹来扩展图像的功能。而且,您始终可以使用 docker run -v ... 在自己需要的位置(或在docker-compose.yml中)制作自己的卷。在映像中创建卷意味着运行映像后,您将在 docker volume ls 中列出匿名卷。

Either move your volume line to the end, or preferably remove it from your image entirely. These entries block the ability to extend your image with changes to this folder later. And you can always make your own volume with the docker run -v ... where you want it (or in your docker-compose.yml). Creating volumes in the image means you'll get anonymous volumes listed in docker volume ls after running your image.

这篇关于sed内联替换不适用于Dockerfile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆