Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 552
darkomen Avatar asked Nov 01 '16 11:11

darkomen


People also ask

How do I run a shell script in Dockerfile entrypoint?

Step 1: Create a script.sh file and copy the following contents. Step 2: You should have the script.sh is the same folder where you have the Dockerfile. Create the Dockerfile with the following contents which copy the script to the container and runs it part of the ENTRYPOINT using the arguments from CMD.

How do I use CAT file in Dockerfile?

Run docker build to create a docker image with tag service (call it v1) Change a file(say prod. json ) that required the 3rd step in the Dockerfile to rerun (thus failing the cache) Run docker build to create docker image with tag service (call it v2)

What is shell command in Dockerfile?

Docker Dockerfiles SHELL Instruction The SHELL instruction allows the default shell used for the shell form of commands to be overridden. The default shell on Linux is ["/bin/sh", "-c"] , and on Windows is ["cmd", "/S", "/C"] . The SHELL instruction must be written in JSON form in a Dockerfile.

How do I run a command in Dockerfile?

Running Commands in an Alternate Directory in a Docker Container. To run a command in a certain directory of your container, use the --workdir flag to specify the directory: docker exec --workdir /tmp container-name pwd.


2 Answers

Update [07/29/2021]: As of dockerfile/dockerfile:1.3.0-labs, the Here-Document syntax is now supported with the Docker BuildKit backend enabled.

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

Dockerfile Example:

# syntax = docker/dockerfile:1.3-labs

...

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

like image 56
bhass1 Avatar answered Sep 20 '22 16:09

bhass1


Based on this comment to an issue posted on Github, this works:

RUN echo 'All of your\n\
multiline that you ever wanted\n\
into a dockerfile\n'\
>> /etc/example.conf
like image 32
w1100n Avatar answered Sep 18 '22 16:09

w1100n