ARG and ENV
How to use?
We have a dockerfile like this
FROM ubuntu:latest RUN apt-get update && \ apt-get install -y wget && \ wget https://github.com/ipinfo/cli/releases/download/ipinfo-2.0.1/ipinfo_2.0.1_linux_amd64.tar.gz && \ tar zxf ipinfo_2.0.1_linux_amd64.tar.gz && \ mv ipinfo_2.0.1_linux_amd64 /usr/bin/ipinfo && \ rm -rf ipinfo_2.0.1_linux_amd64.tar.gz
The version of ipinfo in this file is ipinfo-2.0.1, which may be changed. 2.0.1 has appeared five times in the file. It is troublesome to modify it. If it occurs more times, it will almost become unmaintainable. Therefore, you need to define a variable to facilitate future maintenance.
First modify the variable in the form of ENV (there is a small pit to tell you, that is, when writing the variable, the value should not have any spaces, otherwise it will fail during packaging.)
dockerfile.ENV
FROM ubuntu:latest ENV VERSION=2.0.1 RUN apt-get update && \ apt-get install -y wget && \ wget https://github.com/ipinfo/cli/releases/download/ipinfo-${VERSION}/ipinfo_${VERSION}_linux_amd64.tar.gz && \ tar zxf ipinfo_${VERSION}_linux_amd64.tar.gz && \ mv ipinfo_${VERSION}_linux_amd64 /usr/bin/ipinfo && \ rm -rf ipinfo_${VERSION}_linux_amd64.tar.gz
After writing this, if the later version changes, we can complete all the changes as long as we modify one place.
Now let's build the ENV image.
docker image build -t <Image tag> -f dockerfile.ENV .
Then prepare a docker file using ARG
dockerfile.ARG
FROM ubuntu:latest ARG VERSION=2.0.1 RUN apt-get update && \ apt-get install -y wget && \ wget https://github.com/ipinfo/cli/releases/download/ipinfo-${VERSION}/ipinfo_${VERSION}_linux_amd64.tar.gz && \ tar zxf ipinfo_${VERSION}_linux_amd64.tar.gz && \ mv ipinfo_${VERSION}_linux_amd64 /usr/bin/ipinfo && \ rm -rf ipinfo_${VERSION}_linux_amd64.tar.gz
Also use docker image build - T < image tag > - F dockerfile.arg. To build
After the two images are packaged, use docker image ls to view the two images. You can see that the size of the two images is the same. All 121M.
Let's see the difference between ARG and ENV.
Differences between ARG and ENV
In general, ARG and ENV are two different,
- The first point is that the scope of declared variables is different
- The second point is that after ARG declaration, variables can be modified at build time.
1. ENV can be brought to the image
We start the image through the command of interactive mode and enter the env test image built by EVN
docker container run -it -p 3000:3000 env-test sh
perhaps
docker container run -it -p 3000:3000 env-test /bin/bash
Then check through the env command. You can see that there will be a VERSION variable inside.
ARG is not
2.ARG can change the variable value when building the image
During construction, you can use the - build Arg parameter to change the value of the variable. For example, to modify the value of the variable VERSION, you can use the following command.
docker image build -f dockerfile.ARG -t test-arg-2.0.0 --build-arg VERSION=2.0.0 .
After the image is built, use the interactive command to enter
docker container run -it -p 3000:3000 test-arg-2.0.0 /bin/bash
View the dependent versions installed in our image
You can see that the specified version is built successfully! Then check the version of ipinfo via the shell command ipinfo verison. You can see that the version has become 2.0.0.