skip to Main Content

I would like to write a file into a Dockerfile.
I don’t want to import the file.

I would like the simplest solution, something like the solution that is not working.
I would like to avoid to repeat many echo with each time the name of the file…

Thanks for help !

# 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 

Not working

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 

This is the image used by the Dockerfile above :

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

2

Answers


  1. In fact, the problem has nothing to do with Docker but with the way you’re using the echo command.
    If you use double-quote, the blank lines will be removed. To keep blank lines, you need to use simple quote (and remove the at the end of the lines).

    You can try on your terminal :

    # with double quote
    $ echo "hello 
    > World"
    
    # result
    hello World
    
    # With simple quote
    $ echo 'Hello
    > World'
    
    #result
    Hello
    World
    
    
    Login or Signup to reply.
  2. Alternative solution

    It would be better, when you define the file in "a normal way". So it is a lot easier to read.
    You can pass the file into your container – see for example below.

    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
    
    # ...
    

    Question

    If you want to keep the file in dockerfile, then you could do something like this:

    Dockerfile

    # ...
    
    RUN echo $'testn
        abcn
        def' > test.txt
    # ...
    

    The last backslash is for docker. The leading dollar-sign causes bash to interpret n or t, see How does the leading dollar sign affect single quotes in Bash?

    Edit (for debian-image)

    Debian react quite different, so you have to wrote:

    # ...
    
    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 
    
    # ...
    

    It is very ugly and I strongly recommend a encapsulated file.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search