zappee
zappee

Reputation: 22668

redirect stdout and stderr while executing docker build > CMD

I am learning Docker and building a new image. During the build process, I copy a bash script into the docker (let's call it hello.sh) image and then I execute it. This bash script writes something to the output with echo.

I would like to see somehow the output of the hello.sh on my console while building the image.

This is my Dockerfile:

FROM ...
COPY .../hello.sh /u01/oracle/
CMD ["/u01/oracle/hello.sh"]

Docker build:

docker build -t my-helloworld:1.0 .

Output:

Step 6/14 : COPY .../hello.sh /u01/oracle/
 ---> 06e7428586b3
...
Step 14/14 : CMD ["/u01/oracle/hello.sh"]
 ---> Running in fcd495c2e52a
Removing intermediate container fcd495c2e52a
 ---> d21b20deaa26
Successfully built d21b20deaa26
Successfully tagged my-helloworld:1.0

hello.sh

#!/bin/bash
echo "I want to see this line on my console while executing docker build..."

So I would like to see somehow the output of this internal bash script on my console or at least in a logfile inside the image.

At first, I just started the image with docker run and tried to see the output with docker logs. There was nothing there to see.

Then I gave up to redirect the container output to my console and tried to log the output into a logfile:

CMD ["/bin/bash", "/u01/oracle/hello.sh > hello.log 2>&1"]

I tried this as well, hello.sh:

#!/bin/bash
exec > hello.log
exec 2>&1
echo "I want to see this line on my console while executing docker build..."

No one from the above works.

Any suggestions, please?

Upvotes: 1

Views: 2415

Answers (2)

Shubham
Shubham

Reputation: 2855

As pointed out by Jan in his answer, CMD is executed when you start the container not when you're building the image. If you want to see the output while building the image, you need to use RUN instruction in your Dockerfile. But they both serve different purpose.

RUN is used to execute a script during the build process. Imagine you want to install something in the final image you're building, you would use RUN here.

CMD instruction is the purpose for which you built the container (ex - starting an application like java -jar something.jar)

Maybe, if you could post the whole Dockerfile and explain a bit what you're trying to achieve, we would be able to help more.

Upvotes: 0

Jan Garaj
Jan Garaj

Reputation: 28646

CMD command is not executed during docker build. You need to use RUN. CMD is definition what should be executed, when you are starting container by default (docker run).

RUN /u01/oracle/hello.sh

Upvotes: 2

Related Questions