Set latest tomcat version to an ENV variable in DockerFile

Here I am trying to fetch and assign the latest patch version (10.0.*) of tomcat to a ENV variable through wget.
And this env version is used to download the tomcat.

But I am unable to fetch and set the version to the ENV.
Please suggest how can I achieve this ? Not sure whether I am doing right way.

FROM alpine:latest

# Install Tomcat
ENV version $(wget --quiet -O - https://archive.apache.org/dist/tomcat/tomcat-10/ | grep v10 | awk '{split($5,c,">v") ; split(c[2],d,"/") ; print d[1]}')

RUN echo $version

RUN wget https://archive.apache.org/dist/tomcat/tomcat-10/v${version}/bin/apache-tomcat-${version}.tar.gz --no-check-certificate \
    && tar -xvzf apache-tomcat-${version}.tar.gz -C /opt

From the above code, echo is printing entire command ($(wget --quiet -O - Index of /dist/tomcat/tomcat-10 | grep v10 | awk ‘{split($5,c,“>v”) ; split(c[2],d,“/”) ; print d[1]}’))

Any solution would be much appreciated.

There is no way to do that. The ENV instruction does not accept executable commands, only static values or build arguments.

When you work with Docker, you should do it the other way around. Set a static version in the environment variable and use that in a RUN instruction to download a specific version of tomcat.

If you still want to download the latest patch version, you need to detect the latest version on the host and generate the Dockerfile dinamically or the easier way is to use build arguments:

#!/bin/bash


version=$(wget --quiet -O - https://archive.apache.org/dist/tomcat/tomcat-10/ | grep v10 | awk '{split($5,c,">v") ; split(c[2],d,"/") ; print d[1]}')

docker build . -t imagename --build-arg version=$version ...

Dockerfile:

FROM alpine:latest

ARG version

ENV version=$version

RUN echo $version

RUN wget https://archive.apache.org/dist/tomcat/tomcat-10/v${version}/bin/apache-tomcat-${version}.tar.gz --no-check-certificate \
    && tar -xvzf apache-tomcat-${version}.tar.gz -C /opt

I haven’t tested the code

This way you will have a reproducable code and if something goes wrong, you can set the build argument statically instead of fetching the version from the web.

Thank you so much for your response. It’s working for me.

But I figured out other way which is mentioned as below:

RUN version=$(curl --silent http://mirror.vorboss.net/apache/tomcat/tomcat-10/ | grep v10.0 | awk '{split($0,c,">v") ; split(c[2],d,"/") ; print d[1]}') \
&& echo $version \
&& wget https://archive.apache.org/dist/tomcat/tomcat-10/v$version/bin/apache-tomcat-$version.tar.gz --no-check-certificate \
&& tar -xvzf apache-tomcat-$version.tar.gz -C /opt
1 Like