Home  >  Article  >  Operation and Maintenance  >  What is the difference between arg and env in docker

What is the difference between arg and env in docker

WBOY
WBOYOriginal
2022-01-28 10:40:206520browse

The difference between arg and env in docker is: arg exists during build and can be used as a variable in the Dockerfile, while env is an environment variable after the container is built and cannot be used as a parameter in the Dockerfile use.

What is the difference between arg and env in docker

The operating environment of this tutorial: linux7.3 system, docker-1.13.1 version, Dell G3 computer.

What is the difference between arg and env in docker

When you use docker-compoe to build an image, you will feel that the functions of ARG and ENV are very similar, but these two existences definitely have their own advantages. The reason

The timing of their effect

  • arg exists during build and can be used as a variable in the Dockerfile

  • env is the environment variable after the container is built and cannot be used as a parameter in the Dockerfile

It can be seen from here that ARG is specially designed for building images

Take a specific example

# Dockerfile
FROM redis:3.2-alpine
LABEL maintainer="GPF <5173180@qq.com>"
ARG REDIS_SET_PASSWORD=developer
ENV REDIS_PASSWORD ${REDIS_SET_PASSWORD}
VOLUME /data
EXPOSE 6379
CMD ["sh", "-c", "exec redis-server --requirepass \"$REDIS_PASSWORD\""]

This is a file for building redis. There is such a sentence in the middle

ARG REDIS_SET_PASSWORD=developer
ENV REDIS_PASSWORD ${REDIS_SET_PASSWORD}

It serves the sentence

CMD ["sh", "-c", "exec redis-server --requirepass \"$REDIS_PASSWORD\""]

. This sentence The password is set when starting redis, because when CMD is executed, it means that the container has been successfully built and run. At this time, CMD executes the commands in the container in the container, so the variables in CMD are environment variables instead of variables in the Dockerfile, so the value in ARG needs to be assigned to ENV during construction

Another example of using ARG

FROM nginx:1.13.1-alpine
LABEL maintainer="GPF <5173180@qq.com>"
#https://yeasy.gitbooks.io/docker_practice/content/image/build.html
RUN mkdir -p /etc/nginx/cert \
    && mkdir -p /etc/nginx/conf.d \
    && mkdir -p /etc/nginx/sites
COPY ./nginx.conf /etc/ngixn/nginx.conf
COPY ./conf.d/ /etc/nginx/conf.d/
COPY ./cert/ /etc/nginx/cert/
COPY ./sites /etc/nginx/sites/
ARG PHP_UPSTREAM_CONTAINER=php-fpm
ARG PHP_UPSTREAM_PORT=9000
RUN echo "upstream php-upstream { server ${PHP_UPSTREAM_CONTAINER}:${PHP_UPSTREAM_PORT}; }" > /etc/nginx/conf.d/upstream.conf
VOLUME ["/var/log/nginx", "/var/www"]
WORKDIR /usr/share/nginx/html

Here we just use ARG

ARG PHP_UPSTREAM_CONTAINER=php-fpm
ARG PHP_UPSTREAM_PORT=9000
RUN echo "upstream php-upstream { server ${PHP_UPSTREAM_CONTAINER}:${PHP_UPSTREAM_PORT}; }" > /etc/nginx/conf.d/upstream.conf

The variables here are ARG instead of ENV, because this command is run in the Dockerfile. It is suitable to use ARG for temporary use of variables without the need to store the value of the environment variable

Recommended learning: "docker video tutorial"

The above is the detailed content of What is the difference between arg and env in docker. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn