Home > Net >  Redirect STDOUT of a RUN command in a Dockerfile
Redirect STDOUT of a RUN command in a Dockerfile

Time:10-07

I need to concatenate two files together during a Docker build session. The concatenated file will then be accessed within container in a subsequent step.

When I try to use this in a Dockerfile:

COPY ./a /tmp
COPY ./b /tmp
RUN ["ls", "/tmp"]
RUN ["cat", "/tmp/a", "/tmp/b", ">/tmp/c"]
RUN ["ls", "/tmp"]

and then try to build the container I get this error:

Step 3/13 : COPY ./a /tmp
 ---> Using cache
 ---> 253aad0eae1a
Step 4/13 : COPY ./b /tmp
 ---> Using cache
 ---> 0f86ea5731bf
Step 5/13 : RUN ["ls", "/tmp"]
 ---> Using cache
 ---> f9aae6a9e657
Step 6/13 : RUN ["cat", "/tmp/a", "/tmp/b", ">/tmp/c"]
 ---> Running in 3aed3ca981ab
Tue Oct  5 16:29:32 EDT 2021
Tue Oct  5 16:29:33 EDT 2021
cat: >/tmp/c: No such file or directory
The command 'cat /tmp/a /tmp/b >/tmp/c' returned a non-zero code: 1

How do I redirect STDOUT with the RUN command?

Is there another command I can use that accepts the output filename as a parameter?

I know I can create a small script and add that to the container and then run that script, but I was hoping to be able to do this within the Dockerfile.

CodePudding user response:

you are using RUN with the exec from RUN ["executable", "param1", "param2"] where shell features like expanding variables, sub commands, piping output, chaining commands together won't work, use RUN in shell form enter the command as one string RUN <command>

COPY ./a /tmp
COPY ./b /tmp
RUN ls /tmp
RUN cat /tmp/a /tmp/b > /tmp/c
RUN ls /tmp
  • Related