Home > OS >  Run Docker Inspect from Dockerfile
Run Docker Inspect from Dockerfile

Time:03-02

I am creating a simple HTML and want it to display the time docker container is built. I would like to use Docker inspect, Since docker inspect is run from inside a docker container am I correct that it is not possible to use it in Docker file?

My html file is

<!DOCTYPE html>
<html>
<body>

<h1>Welcome to Nginx Web Server</h1>

<p>Date/Time: <span id="datetime"></span></p>

<script>
var dt = new Date();
document.getElementById("datetime").innerHTML = (("0" dt.getDate()).slice(-2))  "."  (("0" (dt.getMonth() 1)).slice(-2))  "."  (dt.getFullYear())  " "  (("0" dt.getHours()).slice(-2))  ":"  (("0" dt.getMinutes()).slice(-2));
</script>

<p>This server is running through Docker container deployed:</p>

</body>
</html>

MY DOCKER FILE is

FROM nginx
COPY index.html /usr/share/nginx/html
EXPOSE 8080
RUN docker inspect -f '{{ .Created }}'

CodePudding user response:

I'm not sure I understand what you're trying to measure. But if docker is not installed within your image, then you cannot invoke it.

CodePudding user response:

It's possible to run docker inspect in a container using some form of docker-in-docker (like the docker image). However, I don't think this will do what you want to do - you can't get the creation date of an image which hasn't actually been created yet.

You could instead use the date command to get a timestamp for the current time:

$ date
Tue Mar  1 23:43:44 -03 2022

However, I'd recommend against doing this. It would interact weirdly with docker's caching - that date would be cached for subsequent builds, unless you change your index.html.

CodePudding user response:

Docker Inspect might get the build time, but it

  1. Requires invoking docker command, introducing too much dependency, and hence too 'heavy' for such trivil task.
  2. Can only run after the image build.

The simpler and lighter way, is to replace the html file when building the docker.

  1. Define a place holder(a unique constant string to be replaced) to be replaced in html file.
<!DOCTYPE html>
<html>
<body>

<h1>Welcome to Nginx Web Server</h1>

<p>Date/Time: <span id="datetime"></span></p>

<script>
var dt = new Date();
document.getElementById("datetime").innerHTML = (("0" dt.getDate()).slice(-2))  "."  (("0" (dt.getMonth() 1)).slice(-2))  "."  (dt.getFullYear())  " "  (("0" dt.getHours()).slice(-2))  ":"  (("0" dt.getMinutes()).slice(-2));
</script>

<p>This server is running through Docker container deployed:</p>
<p>DATEPLACEHOLDER</p>
</body>
</html>
  1. Substitute the place holder with date variable.
FROM nginx
COPY index.html /usr/share/nginx/html
EXPOSE 8080
RUN export NOW=$(date); sed -i "s/DATEPLACEHOLDER/$(NOW)/g" "/usr/share/nginx/html"

Or you can use RUN echo date > /tmp/date.txt, and subsitute the value to html when docker startup (can be done in CMD section of dockerfile).

This method, however, also has its drawbacks. Even nothing in dockerfile changed, a new image will be built for each build. And if there are other important steps after the substitution, docker build caches are invalid, resulting longer build time and longer push time.

  • Related