Useful Commands Used In a Dockerfile

Useful commands which I have used in Dockerfiles. You can get new ideas by analyzing Dockerfiles on Docker Hub.

Source code is from working example which is accessible on GitHub.

Install new package with APT

When using apt-get install, use option -y. In this case you won’t be prompted with question “Do you want to continue” and build will continue.

Example of use apt-get:

RUN apt-get update && \
    apt-get install -y libapache2-mod-jk openssl && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

Append new lines to a file with echo

Example of adding configuration at the end of file httpd.conf:

RUN echo "LoadModule ssl_module modules/" >> /usr/local/apache2/conf/httpd.conf && \
    echo "Listen 443" >> /usr/local/apache2/conf/httpd.conf && \
    echo "<VirtualHost *:443>" >> /usr/local/apache2/conf/httpd.conf && \
    echo "    ServerName" >> /usr/local/apache2/conf/httpd.conf && \
    echo "    SSLEngine on" >> /usr/local/apache2/conf/httpd.conf && \
    echo '    SSLCertificateFile "/usr/local/apache2/cert/mysitename.crt"' >> /usr/local/apache2/conf/httpd.conf && \
    echo '    SSLCertificateKeyFile "/usr/local/apache2/cert/mysitename.key"' >> /usr/local/apache2/conf/httpd.conf && \
    echo "</VirtualHost>" >> /usr/local/apache2/conf/httpd.conf  

Change a text file with sed

Example of adding property to httpd.conf at specific place:

RUN sed -i 's/\(<VirtualHost \*:443>\)/\1\n    JkMount \/* secure\n/' /usr/local/apache2/conf/httpd.conf 

Download and unpack an installation package with wget and tar

Example of installing maven:

RUN mkdir /opt/maven && \
    wget && \
    tar -xzvf apache-maven-3.3.9-bin.tar.gz -C /opt/maven && \
    rm apache-maven-3.3.9-bin.tar.gz

ENV PATH=/opt/maven/apache-maven-3.3.9/bin:$PATH 

Leave a Reply