我想写一个文件到Dockerfile。我不想导入文件。
我想要最简单的解决方案,就像不工作的解决方案。我想避免重复多次echo每次的文件名…
感谢您的帮助!
# This one is not working
RUN echo "[supervisord]
nodaemon=true
[program:ssh]
command=service ssh start
autorestart=true
[program:nginx]
command=service nginx start
autorestart=true" >> /etc/supervisor/conf.d/supervisord.conf
# This one is working
# RUN echo '[supervisord]' >> /etc/supervisor/conf.d/supervisord.conf
# && echo 'nodaemon=true' >> /etc/supervisor/conf.d/supervisord.conf
# && echo '[program:ssh]' >> /etc/supervisor/conf.d/supervisord.conf
# && echo 'command=service ssh start' >> /etc/supervisor/conf.d/supervisord.conf
# && echo 'autorestart=true' >> /etc/supervisor/conf.d/supervisord.conf
# && echo '[program:nginx]' >> /etc/supervisor/conf.d/supervisord.conf
# && echo 'command=service nginx start' >> /etc/supervisor/conf.d/supervisord.conf
# && echo 'autorestart=true' >> /etc/supervisor/conf.d/supervisord.conf
不工作
RUN echo $'[supervisord]n
nodaemon=truen
[program:ssh]n
command=service ssh startn
autorestart=truen
[program:nginx]n
command=service nginx startn
autorestart=true' > /etc/supervisor/conf.d/supervisord.conf
这是上面的Dockerfile使用的图像:
FROM debian:latest
# Run install with..
USER root
LABEL first_build="2021-01-28"
# Timezone Paris
ENV TZ Europe/Paris
RUN cp /usr/share/zoneinfo/Europe/Paris /etc/localtime
# Motd Dock'Ager
ADD var/motd.tar.gz /etc/update-motd.d/
COPY var/sources.list /etc/apt/sources.list
# SSH & Timezone & Munin
RUN echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections
&& apt-get update
&& apt-get upgrade -y && apt-get install -y
openssh-server
sudo
nano
zip
unzip
tar
nginx
munin
munin-node
munin-plugins-extra
figlet
ruby-full
curl
wget
&& useradd -rm -d /home/test -s /bin/bash -g root -G sudo -u 1000 test
&& echo 'test:test' | chpasswd
&& echo 'root:root' | chpasswd
&& cd /tmp
&& wget https://github.com/busyloop/lolcat/archive/master.zip
&& unzip master.zip
&& cd lolcat-master/bin
&& gem install lolcat
&& sed -i '/pam_motd.so noupdate/s/^/#/g' /etc/pam.d/sshd
&& chmod +x /etc/update-motd.d/*
&& apt-get clean
&& rm -rf /var/log/*
&& rm -rf /var/lib/apt/lists/*
COPY var/sshd_config /etc/ssh/sshd_config
备选方案
如果您以"正常方式"定义该文件,效果会更好。这样读起来容易多了。
您可以将文件传递到容器中——参见下面的示例。
supervisord.conf
[supervisord]
nodaemon=true
[program:ssh]
command=service ssh start
autorestart=true
[program:nginx]
command=service nginx start
autorestart=true
Dockerfile
# ...
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
# ...
<标题>如果你想把文件保存在dockerfile中,那么你可以这样做:
Dockerfile
# ...
RUN echo $'testn
abcn
def' > test.txt
# ...
最后一个反斜杠用于docker。前面的美元符号导致bash解释n
或t
,参见bash中前面的美元符号如何影响单引号?
编辑(for debian-image)
Debian的反应完全不同,所以你必须写:
# ...
RUN echo '[supervisord]n
nodaemon=truen
[program:ssh]n
command=service ssh startn
autorestart=truen
[program:nginx]n
command=service nginx startn
autorestart=true' > /etc/supervisor/conf.d/supervisord.conf
# ...
它是非常丑陋的,我强烈建议一个封装文件。
标题>事实上,这个问题与Docker无关,而是与您使用echo
命令的方式有关。
如果使用双引号,空行将被删除。要保留空行,需要使用简单的引号(并删除行尾的)。
你可以在你的终端上试试:
# with double quote
$ echo "hello
> World"
# result
hello World
# With simple quote
$ echo 'Hello
> World'
#result
Hello
World