1

I just wondering if it was possible to print Dockerfile's CMD output when I run docker run ....

I setup jenkins on an image and it would be convenient to have the defautl admin password displayed directly when running the image. Here is what I have in mind :

# Dockerfile

...
RUN systemctl enable jenkins
CMD ["/usr/bin/systemctl", "start", "jenkins"]
CMD ["/usr/bin/echo", "Jenkins default password : "]
CMD ["/usr/bin/cat", "/var/lib/jenkins/secrets/initialAdminPassword"]

I have tried to follow this post but I may not understand the solution:

Redirecting command output in docker

My desired output would look like this:

$ docker run -tid auto-server:1.0
c93f26eb1756ce98332a9be8dd88a824e65ab9c1342df762c8a62b9d62c13dcc
Jenkins default password : 
<the-jenkins-password>
$

And above all I wonder if this follows docker's good practices.

UPDATE

# output when I only keep CMD ["/usr/bin/systemctl", "start", "jenkins"]

$ docker logs auto-server
jenkins: Correct java version found
jenkins:  * Starting Jenkins Automation Server jenkins
jenkins:    ...done.

$ docker exec -ti auto-server bash
root@42dbec3a6eaa:/home/ubuntu# systemctl status jenkins
jenkins.service - Controls Jenkins Automation Server
    Loaded: loaded (/etc/init.d/jenkins, disabled)
    Active: active (running)
root@42dbec3a6eaa:/home/ubuntu# 
blondelg
  • 916
  • 1
  • 8
  • 25
  • One approach is to use `docker exec` once the container is running. – Jose Hermosilla Rodrigo Sep 30 '20 at 15:40
  • yes, that is what i use to do but maybe I can save one command to run – blondelg Sep 30 '20 at 15:47
  • 4
    A container only runs one `CMD`; the last `CMD` wins. If you need to run multiple commands, you need a shell script to be the `CMD`. Usually you will want that `CMD` to run the single container process (Jenkins) as a foreground process. To a first approximation `systemctl` doesn't work in Docker at all. You can use `docker logs` to see what a background container has printed out, or inject the password yourself using a `docker run -v` bind mount. – David Maze Sep 30 '20 at 15:47
  • Thanks, I will consider using a script. Apparently, systemctl runs well jenkins in background (see my update) – blondelg Sep 30 '20 at 15:55

0 Answers0