How do you print to console from a docker file during build?
Asked Answered
S

3

67

Suppose you have some Dockerfile. What needs to be added to that file such that a string (ie "Hello World") is printed to the console during build?

docker build .

Surgeon answered 14/5, 2021 at 21:7 Comment(4)
What research did you do? What have you tried? What documentation did you read?Onida
I guess you can add RUN echo "Hello World" in your Dockerfile. Is this what you want ?Chops
Possible duplicate: Docker build not showing any output from commandsUsurp
@Chops This does not work unfortunately.Undergrown
T
106

It's fairly simple actually. If you just want to print stuff using in the build proccess you could just add the following line to your Dockerfile:

RUN echo "hello there"

And then add these options to your docker build command:

--progress=plain --no-cache

EDIT:

as noted by @SoftwareEngineer, when used for logging or tooling purposes you should append the echo command to the one you want to check if were successful. for example when downloading packages and wanting to get a print statement when finished: example from nginx official image dockerfile

RUN apt-get install -y whatever && echo "installed package"
Theft answered 15/5, 2021 at 15:22 Comment(5)
Though, I wouldn't recommend actually doing thisDevour
I agree it's a bit odd to say the least but other than it's not best practice, why wouldn't you recommend this @SoftwareEngineer ? for testing purposes for exampleTheft
Sorry, I should have been a little clearer. My objection is to running this in its own RUN command and creating an extra layer just to produce output. For debugging and audit purposes there is nothing intrinsically wrong with producing logging output. I just wouldn't recommend doing it in this specific way.Devour
The --progress=plain --no-cache is key.Remorseful
@DanielViglione what OS and docker installation method are you using?Theft
Z
0

I like to keep notes to remind myself what I am doing and display logs to myself to verify that it happens in this version. RUN it is

# Copy local config into container
COPY config/kibana.yml /opt/kibana/config/kibana.yml

# Verify that local configuration file is copied in & enables SSL/TLS
RUN ls -l /opt/kibana/config/
Zebrass answered 20/4, 2023 at 16:53 Comment(0)
D
0

This might not be what you want, but if you have Docker desktop you can select the Builds tab, search for the image name:tag you created and select the logs tab. All the output from all steps should be there.

Diaspore answered 21/8 at 14:45 Comment(1)
Your answer is true, but as you mentioned, it is not related to the question because Docker Desktop is different from the Docker CLI, and they can be installed separatelyNady

© 2022 - 2024 — McMap. All rights reserved.