1

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?

1
  • That is clear now. So if I want to configure a server inside the container like create an Oracle WebLogic Domain (in case of JEE app server) OR create a new database user in case of if the image contains a database server OR install a new deb package, and I only would like to execute this command once I need to use RUN. But if I want to start the server automatically inside the container when I start the container then I need to use CMD. Is that correct? Commented Dec 9, 2019 at 22:25

2 Answers 2

2

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 
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.