patternMinor
docker-compose local variable declare and use
Viewed 0 times
localdockerdeclarecomposeandusevariable
Problem
In the programming language we can define and declare a variable and use it later in the program, can we do same in docker-compose file?
For example in Java:
int x = 100;
System.out.println("Number is: "+ x);
Similarly, in docker-compose file, can we declare a local variables and use it later.
docker-compose file
In the docker-compose file we want to declare tomcat_home as a variable and use it later as shown in example.
For example in Java:
int x = 100;
System.out.println("Number is: "+ x);
Similarly, in docker-compose file, can we declare a local variables and use it later.
docker-compose file
In the docker-compose file we want to declare tomcat_home as a variable and use it later as shown in example.
values:
- tomcat_home: "/usr/local/tomcat"
# Creating Tomcat Server 1
Server1:
image: tomcat
hostname: TomcatServer1
container_name: TomcatServer1
volumes:
- ${PWD}/tomcat-users.xml:${tomcat_home}/conf/tomcat-users.xml
- ${PWD}/server.xml:${tomcat_home}/conf/server.xml
Solution
https://docs.docker.com/compose/environment-variables/#the-env-file
One could set a variable in an .env file:
and call it in a docker-compose file
docker-compose.yml
docker-compose up
returns:
No need to run docker-compose to check the config
According to the documentation one could run:
to verify the docker-compose config
One could set a variable in an .env file:
TAG=1.2.3and call it in a docker-compose file
docker-compose.yml
version: "3"
services:
web:
image: nginx:${TAG}
ports:
- 80:80docker-compose up
returns:
Pulling web (nginx:1.2.3)...
ERROR: manifest for nginx:1.2.3 not foundNo need to run docker-compose to check the config
According to the documentation one could run:
docker-compose configto verify the docker-compose config
networks: {}
services:
web:
image: nginx:1.2.3
ports:
- 80:80/tcp
version: '3.0'
volumes: {}Code Snippets
version: "3"
services:
web:
image: nginx:${TAG}
ports:
- 80:80Pulling web (nginx:1.2.3)...
ERROR: manifest for nginx:1.2.3 not founddocker-compose confignetworks: {}
services:
web:
image: nginx:1.2.3
ports:
- 80:80/tcp
version: '3.0'
volumes: {}Context
StackExchange DevOps Q#4079, answer score: 2
Revisions (0)
No revisions yet.