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 ?
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.
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)
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With