Reputation: 1
What I'm trying is to define an ENV
variable that will hold current time inside a docker container. Using bash, I can simply state timenow=$(date+"%y%m%d")
and voila, if I echo $timenow
I see the captured time in the specified format print to the console.
Here goes my Dockerfile: What is written below does not work, any tips on how to do it will be very much appreciated sirs.
FROM ubuntu
MAINTAINER Me
ENV timenow ""
RUN /bin/bash -c "timenow=$(date +'%y-%m-%d')"
CMD ["sh", "-c", "echo $timenow"]
Upvotes: 0
Views: 1929
Reputation: 5063
I'd go with
FROM ubuntu
LABEL maintainer="Me"
ARG TIMENOW
ENV TIMENOW $TIMENOW
CMD ["sh", "-c", "echo $TIMENOW"]
and then
docker build --build-arg TIMENOW=$(date +'%y-%m-%d') --tag my_image:latest .
docker run -it --rm my_image:latest
which should display the time the image was built, which is what I belive you want.
Edit:
If you want the current time at the moment the contaier was executed, then the following should work
FROM ubuntu
LABEL maintainer="Me"
CMD TIMENOW=$(date+"%y%m%d") && echo $TIMENOW
and then
docker build --tag my_image:latest .
docker run -it --rm my_image:latest
Upvotes: 1