0

I'm trying to get the docker images command output to display it on a webpage.

Ex: Docker images ouput

As you can see on the image, this is well formatted.

But what I get on my web page is one line not formatted at all;

REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 3448f27c273f 2 days ago 109.4 MB mysql latest e799c7f9ae9c 3 days ago 407.3 MB

This my script at the moment;

docker_images=$(docker images)

echo $docker_images

How can I get the well formatted output through my echo ?

Cyrus
  • 84,225
  • 14
  • 89
  • 153
Martinouh
  • 21
  • 4

2 Answers2

1

This is related to how shell treats newlines, whitespaces, tabs, etc. when printing a variable with and without double-quotes. To get the desired output, please replace

echo $docker_images

with

echo "$docker_images"

Please refer Capturing multiple line output into a Bash variable for a much more detailed explanation.

Community
  • 1
  • 1
Amit
  • 1,006
  • 1
  • 7
  • 13
  • Thank you for your answer. You're right this will work in shell, but this isn't working with php I still get it echo'ed in one line. – Martinouh May 13 '17 at 13:17
0

So I tried an echo nl2br($output) which gave me this;

HTML OUTPUT

This is a bit better but still not well formatted. Any ideas how to improve this ?

Edit :

This is working now, i'm getting the output that I want. I juste had to do this;

echo "<pre>";
echo nl2br($output) or $output;
echo "</pre>";

Well it is in plain text. If anyone want to manipulate these, you can use the docker API. https://docs.docker.com/engine/api/v1.24/

Martinouh
  • 21
  • 4