Search code examples
unixdockerdockerfilecat

launch a CAT command unix into Dockerfile


I would like to launch this vagrant command cat(run perfectly!) to provisionning my container with a Dockerfile :

# Configure Virtualenvwrapper.
RUN cat <<EOF >> /home/docker/.bashrc
# Virtualenvwrapper configuration.
export WORKON_HOME=\$HOME/.virtualenvs
export PROJECT_HOME=\$HOME/Devel
source /usr/local/bin/virtualenvwrapper.sh
EOF

But I have this error return when I launch my building image docker :

 ---> 40f9ed8e187d
Removing intermediate container 85f6c8536520
Step 69 : RUN cat <<EOF >> /home/docker/.bashrc
 ---> Running in dcbb3d441f79
 ---> 78acd9c2e5d5
Removing intermediate container dcbb3d441f79
Step 70 : EXPORT
Unknown instruction: EXPORT

What is the trick for run a cat command unix into image with Dockerfile ?


Solution

  • Update [08/03/2022]: As of dockerfile/dockerfile:1.4.0, the Here-Document support has been promoted from labs channel to stable. #2589.

    You need to use Docker Buildkit by setting DOCKER_BUILDKIT=1 in your environment, set the syntax parser directive to use dockerfile/dockerfile:1.4.0, and swap the position of the here delimeter with cat. The rest is used like normal.

    Dockerfile Example:

    # syntax = docker/dockerfile:1.4.0
    
    ...
    
    RUN <<EOF cat >> /home/docker/.bashrc
    # Virtualenvwrapper configuration.
    export WORKON_HOME=\$HOME/.virtualenvs
    export PROJECT_HOME=\$HOME/Devel
    source /usr/local/bin/virtualenvwrapper.sh
    EOF
    

    Prior to dockerfile/dockerfile:1.4.0, Here-Document syntax was supported in the labs channel dockerfile/dockerfile:1.3.0-labs release.

    Simply replace version used in the syntax parser directive from the above example.


    Prior to Docker BuildKit labs channel dockerfile/dockerfile:1.3.0-labs release, instead of using cat, try using echo instead!

    Turn this shell cat example...

    #!/usr/bin/env sh
    cat <<EOF > /tmp/example.txt
    line 1
    line 2
    line 3
    EOF
    

    ... into this Dockerfile echo example!

    RUN echo -e '\
    line 1\n\
    line 2\n\
    line 3\
    ' > /tmp/example.txt
    

    Note the pair of single quotes (') in the echo example.

    Also note the -e flag for echo to support the escaped newlines (\n).

    Caution: Unfortunately, the -e flag may or may not be required depending on the version of echo your image has installed. For example, the npm:16 image's echo does not require -e and actually will print the -e along with the single-quoted lines. On the other hand, the ubuntu:20.04 image's echo does require -e.

    The same example could be written on one line as: RUN echo -e 'line 1\nline 2\nline 3' >> /tmp/example.txt, but I find the above example more readable.

    To answer the OP's question, use this:

    # Configure Virtualenvwrapper.
    RUN echo -e '\
    # Virtualenvwrapper configuration.\n\
    export WORKON_HOME=\$HOME/.virtualenvs\n\
    export PROJECT_HOME=\$HOME/Devel\n\
    source /usr/local/bin/virtualenvwrapper.sh\
    ' >> /home/docker/.bashrc
    

    Caution: The escape character can be redefined by the escape directive. If your Dockerfile has a different escape character set, you'll need to modify the examples accordingly.