You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 01-contenedores/contenedores-vi/docker-compose-extension/demos/02-env-variables.md
+205-1Lines changed: 205 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,4 +30,208 @@ app.use("/api", router);
30
30
31
31
```
32
32
33
-
Create `docker-compose.envs.yml`
33
+
Create `docker-compose.envs.yml`
34
+
35
+
```yml
36
+
services:
37
+
frontend:
38
+
build:
39
+
context: ./frontend
40
+
deploy:
41
+
replicas: 1
42
+
ports:
43
+
- 3000:8080
44
+
profiles: [frontend]
45
+
46
+
backend:
47
+
build:
48
+
context: ./backend
49
+
ports:
50
+
- 8080:8080
51
+
depends_on:
52
+
- mongodb
53
+
profiles: [debug]
54
+
# diff #
55
+
environment:
56
+
- DEBUG="my hard coded value"
57
+
# diff #
58
+
59
+
mongodb:
60
+
image: mongo:latest
61
+
```
62
+
63
+
```bash
64
+
docker compose -f docker-compose.envs.yml --profile debug up --build
65
+
```
66
+
67
+
```bash
68
+
curl http://localhost:8080/api/setup
69
+
```
70
+
71
+
Instead of using hard coded values, we can use variables declare in shell. Let's try it:
72
+
73
+
```bash
74
+
docker compose -f docker-compose.envs.yml --profile debug down
75
+
```
76
+
77
+
Update `my-app/docker-compose.envs.yml`
78
+
79
+
```diff
80
+
backend:
81
+
build:
82
+
context: ./backend
83
+
ports:
84
+
- 8080:8080
85
+
depends_on:
86
+
- mongodb
87
+
profiles: [debug]
88
+
environment:
89
+
- - DEBUG="my hard coded value"
90
+
+ - DEBUG
91
+
```
92
+
93
+
```bash
94
+
export DEBUG="from shell"
95
+
```
96
+
97
+
```bash
98
+
docker compose -f docker-compose.envs.yml --profile debug up
99
+
```
100
+
101
+
```bash
102
+
curl http://localhost:8080/api/setup
103
+
```
104
+
105
+
```bash
106
+
docker compose -f docker-compose.envs.yml --profile debug down
107
+
```
108
+
109
+
We can also use a `.env` file, `docker compose` will look for this file in order to set up the ennvironment variables.
110
+
111
+
```bash
112
+
unset DEBUG
113
+
```
114
+
115
+
Create `my-app/.env`
116
+
117
+
```ini
118
+
DEBUG="from .env file"
119
+
```
120
+
121
+
```bash
122
+
docker compose -f docker-compose.envs.yml --profile debug up
123
+
```
124
+
125
+
```bash
126
+
curl http://localhost:8080/api/setup
127
+
```
128
+
129
+
What happens when the environment variables is not set? Let's remove the `.env` file, and see what happens.
130
+
131
+
```bash
132
+
docker compose -f docker-compose.envs.yml --profile debug down
133
+
```
134
+
135
+
```bash
136
+
rm my-app/.env
137
+
```
138
+
139
+
```bash
140
+
docker compose -f docker-compose.envs.yml --profile debug up
141
+
```
142
+
143
+
```bash
144
+
curl http://localhost:8080/api/setup
145
+
```
146
+
147
+
> No value is provided
148
+
149
+
```bash
150
+
docker compose -f docker-compose.envs.yml --profile debug down
151
+
```
152
+
153
+
It would be nice if docker compose notice us, that the environment variable is not set up, that is where [interpolation](https://docs.docker.com/compose/how-tos/environment-variables/variable-interpolation/#interpolation-syntax) comes in place.
154
+
155
+
Update `my-app/docker-compose.envs.yml`
156
+
157
+
```diff
158
+
....
159
+
backend:
160
+
build:
161
+
context: ./backend
162
+
ports:
163
+
- 8080:8080
164
+
depends_on:
165
+
- mongodb
166
+
profiles: [debug]
167
+
environment:
168
+
- - DEBUG
169
+
+ - DEBUG=${DEBUG}
170
+
....
171
+
```
172
+
173
+
```bash
174
+
docker compose -f docker-compose.envs.yml --profile debug up
175
+
```
176
+
177
+
```bash
178
+
docker compose -f docker-compose.envs.yml --profile debug down
179
+
```
180
+
181
+
For last we can also use `env_file` attribute. The [env_file attribute](https://docs.docker.com/reference/compose-file/services/#env_file) lest use multiple `.env` files.
182
+
183
+
Update `my-app/docker-compose.envs.yml`
184
+
185
+
```diff
186
+
backend:
187
+
build:
188
+
context: ./backend
189
+
ports:
190
+
- 8080:8080
191
+
depends_on:
192
+
- mongodb
193
+
profiles: [debug]
194
+
- environment:
195
+
- - DEBUG=${DEBUG}
196
+
+ env_file:
197
+
+ - path: ./default.env
198
+
+ required: true
199
+
+ - path: ./override.env
200
+
+ required: false
201
+
```
202
+
203
+
Create `my-app/default.env`
204
+
205
+
```ini
206
+
DEBUG="value from default.env"
207
+
```
208
+
209
+
```bash
210
+
docker compose -f docker-compose.envs.yml --profile debug up
211
+
```
212
+
213
+
```bash
214
+
curl http://localhost:8080/api/setup
215
+
```
216
+
217
+
```bash
218
+
docker compose -f docker-compose.envs.yml --profile debug down
219
+
```
220
+
221
+
Create `my-app/override.env`
222
+
223
+
```ini
224
+
DEBUG="value from override.env"
225
+
```
226
+
227
+
```bash
228
+
docker compose -f docker-compose.envs.yml --profile debug up
229
+
```
230
+
231
+
```bash
232
+
curl http://localhost:8080/api/setup
233
+
```
234
+
235
+
```bash
236
+
docker compose -f docker-compose.envs.yml --profile debug down
0 commit comments