dockerredhawksdr

How can I use a several line command in a Dockerfile in order to create a file within the resulting Image


I'm following installation instructions for RedhawkSDR, which rely on having a Centos7 OS. Since my machine uses Ubuntu 22.04, I'm creating a Docker container to run Centos7 then installing RedhawkSDR in that.

One of the RedhawkSDR installation instructions is to create a file with the following command:

cat<<EOF|sed 's@LDIR@'`pwd`'@g'|sudo tee /etc/yum.repos.d/redhawk.repo
[redhawk]
name=REDHAWK Repository
baseurl=file://LDIR/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhawk
EOF

How do I get a Dockerfile to execute this command when creating an image?

(Also, although I can see that this command creates the file /etc/yum.repos.d/redhawk.repo, which consists of the lines from [redhawk] to gpgkey=...., I have no idea how to parse this command and understand exactly why it does that...)


Solution

  • Using the text editor of your choice, create the file on your local system. Remove the word sudo from it; give it an additional first line #!/bin/sh. Make it executable using chmod +x create-redhawk-repo.

    Now it is an ordinary shell script, and in your Dockerfile you can just RUN it.

    COPY create-redhawk-repo ./
    RUN ./create-redhawk-repo
    

    But! If you look at what the script actually does, it just writes a file into /etc/yum.repos.d with a LDIR placeholder replaced with some other directory. The filesystem layout inside a Docker image is fixed, and there's no particular reason to use environment variables or build arguments to hold filesystem paths most of the time. You could use a fixed path in the file

    [redhawk]
    name=REDHAWK Repository
    baseurl=file:///redhawk-yum/
    enabled=1
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhawk
    

    and in your Dockerfile, just COPY that file in as-is, and make sure the downloaded package archive is in that directory. Adapting the installation instructions:

    ARG redhawk_version=3.0.1
    RUN wget https://github.com/RedhawkSDR/redhawk/releases/download/$redhawk_version/\
    redhawk-yum-$redhawk_version-el7-x86_64.tar.gz \
     && tar xzf redhawk-yum-$redhawk_version-el7-x86_64.tar.gz \
     && rm redhawk-yum-$redhawk_version-el7-x86_64.tar.gz \
     && mv redhawk-yum-$redhawk_version-el7-x86_64 redhawk-yum \
     && rpm -i redhawk-yum/redhawk-release*.rpm
    COPY redhawk.repo /etc/yum.repos.d/
    

    Remember that, in a Dockerfile, you are root unless you've switched to another USER (and in that case you can use USER root to switch back); you do not need generally sudo in Docker at all, and can just delete sudo where it appears in these instructions.