EN: Read in English
Dunia D4: Set Hidangan
.env dan pemboleh ubah
Anda akan belajar: isi tempat kosong dalam compose.yaml dari fail .env.
Teks BM belum disemak oleh penutur asli.
Disemak dengan Docker Engine 29 pada 31 Okt 2026
Masalahnya
compose.yaml anda tulis POSTGRES_PASSWORD: example. Anda push ke repo awam. Siapa boleh baca kata laluan itu sekarang?
Pilih satu jawapan.
Tengok
db:
image: postgres:18-alpine
environment:
POSTGRES_PASSWORD: ${DB_PASSWORD:?add DB_PASSWORD to .env}Laptop anda~/kedai-kancil
- Tiada tingkap terbuka
Bekas (container)
- Belum ada container
Pek beku (image)
- kedai-kancil-api:latest6 layer · 244MB
- kedai-kancil-web:latest9 layer · 92.6MB
- nginx:1.30-alpine8 layer · 93.5MB
- node:24-alpine4 layer · 244MB
- postgres:18-alpine9 layer · 433MB
- redis:8-alpine7 layer · 162MB
0 container (0 berjalan), 6 image, 0 loker.
Slip pesanan dengan tempat kosong. ${DB_PASSWORD} ialah tempat kosong yang Compose isi. Itulah interpolation: Tempat kosong seperti ${DB_PASSWORD} dalam compose.yaml yang Compose isi dari shell atau fail .env. (isi tempat kosong). :? maksudnya: berhenti kalau ia kekal kosong.
Cuba
Isi tempat kosong itu. Letak kata laluan dalam fail .env (.env file): Fail di sebelah compose.yaml dengan baris seperti DB_PASSWORD=devpass. Jangan masukkan ke Git. di sebelah compose.yaml. Kemudian hidupkan kedai.
Laptop anda~/kedai-kancil
- Tiada tingkap terbuka
Bekas (container)
- Belum ada container
Pek beku (image)
- kedai-kancil-api:latest6 layer · 244MB
- kedai-kancil-web:latest9 layer · 92.6MB
- nginx:1.30-alpine8 layer · 93.5MB
- node:24-alpine4 layer · 244MB
- postgres:18-alpine9 layer · 433MB
- redis:8-alpine7 layer · 162MB
0 container (0 berjalan), 6 image, 0 loker.
Petunjuk
Satu baris buat fail itu:echo "DB_PASSWORD=devpass" > .envTeka
Tingkap web ialah tempat kosong dengan nilai lalai: "${WEB_PORT:-8080}:80". Fail .env anda tiada baris WEB_PORT. Tingkap mana yang dibuka?
Pilih satu jawapan.
Semak
Leret ke kanan kalau selamat untuk commit ke Git. Leret ke kiri kalau tidak.
Leret ke kanan untuk bagus, ke kiri untuk perlu baiki. Atau guna butang.
compose.yaml yang ada ${DB_PASSWORD}
Anda berjaya!
Kini anda boleh isi tempat kosong dalam compose.yaml dari fail .env.
Nak tahu lebih (pilihan)
Pemboleh ubah shell menang atas .env: WEB_PORT=9090 docker compose up -d buka tingkap 9090. --env-file prod.env baca fail lain. env_file: pada service lain cerita: ia beri pemboleh ubah kepada bekas itu sendiri. docker compose config tunjuk setiap tempat kosong yang diisi. Rahsia sebenar: Dunia C3.
Misi sebenar
echo "DB_PASSWORD=devpass" > .envecho ".env" >> .gitignoredocker compose config
0 daripada 3 langkah siap · Tanda anda disimpan.
English
Your compose.yaml says POSTGRES_PASSWORD: example. You push it to a public repo. Who can read the password now?
db:
image: postgres:18-alpine
environment:
POSTGRES_PASSWORD: ${DB_PASSWORD:?add DB_PASSWORD to .env}An order slip with a blank. ${DB_PASSWORD} is a blank that Compose fills in. That is interpolation. :? means: stop if it stays empty.
Fill the blank. Put the password in a .env file next to compose.yaml. Then start the shop.
echo "DB_PASSWORD=devpass" > .envThe web hatch is a blank with a default: "${WEB_PORT:-8080}:80". Your .env has no WEB_PORT line. Which hatch opens?
Swipe right if it is safe to commit to Git. Swipe left if not.
You can now fill blanks in compose.yaml from a .env file.
Anda dah siapkan ini. Latihan lagi pun bagus!
Dokumentasi rasmi: Interpolation · Environment variables precedence · docker compose config
Anda dah belajar 25 minit. Rehat sekejap bantu otak anda.
Pecutan selesai! Fokus yang hebat. Regangkan badan seminit?