Docker environment variables and scripting effects

Source: Internet
Author: User

Docker's role is to provide an isolated environment for the application to facilitate deployment.

Dockerfile has an env option that lets us add some environment variables to the generated image, and what these environment variables do.


Can be used as a flag to represent some parameters for the deployed environment.

For example, we need a docker to deploy a MySQL. Then we can add an identity to indicate how this MySQL is started, such as can be started by cluster mode, or can be started by a single machine, it is obvious that the two modes of the start command is different.

Then we can use the Dockerfile env to implement this automated startup application process.

For example, we can provide a default env in Dockerfile with a value of non-cluster, which means that the default startup mode is standalone.

Thus, when we run a container, if we do not overwrite this parameter, then the environment variable is non-cluster. Instead, we can override this parameter to cluster by using the-e parameter of Run or docker-compose environment, and the environment variable in the container that starts is cluster.



This is the role of environment variables, and we can specify the container's environment variables when we run a container to represent different startup parameters.


But the light has this environment variable and there is no egg, because it is only a variable or identification, and further we need a shell script to read the environment variables to determine how we start, and thus run the corresponding command. This script can be added to the container via the Add command, and then run with cmd or entrypoint.


Therefore, personal feeling, through the environment variable + start the script to maximize the implementation of the deployment of the application automation. The real boot process command is in the script, and the script reads the environment variables we set.


A small example:

Dockerfile

From Ubuntu:latest  

ADD./run.sh/
run chmod 777/run.sh
CMD ["/run.sh"]


Docker-compose:

Version: ' 3.2 '
services:
  app1:
    image:test-env
    Environment:
      -e1=10

Run.sh:

#!/bin/bash

If [$E 1-lt] then
	echo "yes"
else 
	echo "No"
fi

The output content is judged by passing different E1.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.