<p><a href="https://docs.docker.com/compose/environment-variables/#the-env-file" rel="nofollow noreferrer">https://docs.docker.com/compose/environment-variables/#the-env-file</a></p>

<p>One could set a variable in an .env file:</p>

<pre><code>TAG=1.2.3
</code></pre>

<p>and call it in a docker-compose file</p>

<p>docker-compose.yml</p>

<pre><code>version: "3"
services:
  web:
    image: nginx:${TAG}
    ports:
    - 80:80
</code></pre>

<p>docker-compose up</p>

<p>returns:</p>

<pre><code>Pulling web (nginx:1.2.3)...
ERROR: manifest for nginx:1.2.3 not found
</code></pre>

<p>No need to run docker-compose to check the config</p>

<p>According to the documentation one could run:</p>

<pre><code>docker-compose config
</code></pre>

<p>to verify the docker-compose config</p>

<pre><code>networks: {}
services:
  web:
    image: nginx:1.2.3
    ports:
    - 80:80/tcp
version: '3.0'
volumes: {}
</code></pre>

CC BY-SA 4.0
2018-05-10T07:35:35.397Z
2018-05-10T07:35:35.397Z
030
<p>For <code>docker-compose</code>, you can use either the environment or a <code>.env</code> file. For swarm mode, you can only use the environment. So for portability, I like to export variables into the shell or environment of the command being run. </p>

<p>From a bash shell, this would look like:</p>

<pre><code>$ export tomcat_home=/usr/local/tomcat
$ docker-compose up
</code></pre>

<p>Depending on what you use to deploy your containers (shell script, custom code, some other CI/CD tool), exporting your environment variable will vary.</p>

CC BY-SA 4.0
2018-05-11T12:49:40.513Z
2018-05-11T12:49:40.513Z
BMitch
<p>In the programming language we can define and declare a <b>variable</b> and use it later in the program, can we do same in <b>docker-compose</b> file?  </p>

<h3>For example in Java:</h3>

<p>int <b>x</b> = 100; 
System.out.println("Number is: "+ <b>x</b>); <br/>
Similarly, in docker-compose file, can we declare a local variables and use it later. </p>

<h3>docker-compose file</h3>

In the docker-compose file we want to declare <b>tomcat_home</b> as a variable and use it later as shown in example.

<p><code>
values:
  - <b>tomcat_home</b>: "/usr/local/tomcat" </p>

# Creating Tomcat Server 1

<p>Server1:
  image: tomcat
  hostname: TomcatServer1
  container_name: TomcatServer1
  volumes:
    - ${PWD}/tomcat-users.xml:<b>${tomcat_home}</b>/conf/tomcat-users.xml
    - ${PWD}/server.xml:<b>${tomcat_home}</b>/conf/server.xml
</code></p>

<docker-compose>
docker-compose local variable declare and use
Nigam Rout
# docker-compose local variable declare and use

(Asked by: username_0 on 2018-05-10)

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.

`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

**Tags:** docker-compose


---

## 2 answers

### Answer by username_1 on 2018-05-10. Score: 2 | Accepted answer

https://docs.docker.com/compose/environment-variables/#the-env-file
One could set a variable in an .env file:
TAG=1.2.3

and call it in a docker-compose file
docker-compose.yml
version: "3"
services:
  web:
    image: nginx:${TAG}
    ports:
    - 80:80

docker-compose up
returns:
Pulling web (nginx:1.2.3)...
ERROR: manifest for nginx:1.2.3 not found

No need to run docker-compose to check the config
According to the documentation one could run:
docker-compose config

to verify the docker-compose config
networks: {}
services:
  web:
    image: nginx:1.2.3
    ports:
    - 80:80/tcp
version: '3.0'
volumes: {}


---

### Answer by username_2 on 2018-05-11. Score: 1

For docker-compose, you can use either the environment or a .env file. For swarm mode, you can only use the environment. So for portability, I like to export variables into the shell or environment of the command being run. 
From a bash shell, this would look like:
$ export tomcat_home=/usr/local/tomcat
$ docker-compose up

Depending on what you use to deploy your containers (shell script, custom code, some other CI/CD tool), exporting your environment variable will vary.


---

