EN: Read in English
Dunia D4: Set Hidangan
Misi capstone 1: Bungkus
Anda akan belajar: bungkus Kedai Kancil supaya satu arahan hidupkannya di mana-mana mesin.
Teks BM belum disemak oleh penutur asli.
Disemak dengan Docker Engine 29 pada 31 Okt 2026
Masalahnya
Misi 1: bungkus! Kedai Kancil dapat repo sendiri, kedai-kancil. Mula-mula, letak setiap fail dalam foldernya.
Seret kad ke dalam kotak. Atau ketik kad, kemudian ketik kotak.
Tengok
<h1>Kedai Kancil</h1>FROM nginx:1.30-alpine
COPY index.html /usr/share/nginx/html/Laptop anda~/kedai-kancil
- Tiada tingkap terbuka
Bekas (container)
- Belum ada container
Pek beku (image)
- 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), 4 image, 0 loker.
Atas: web/index.html. Bawah: web/Dockerfile. Dua baris: mula dari nginx, salin laman anda masuk.
Tengok
Kod API ada di bawah: api/server.js, kemudian api/package.json. Resepinya nanti tiada langkah npm ci. Kenapa tak apa?
Pilih satu jawapan.
const http = require('node:http');
const port = process.env.PORT || 3000;
http.createServer((req, res) => {
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ ok: true, shop: 'Kedai Kancil' }));
}).listen(port, () => console.log(`API listening on port ${port}`));{
"name": "kedai-api",
"version": "1.0.0",
"private": true,
"scripts": {
"start": "node server.js",
"test": "node --test"
}
}Tengok
FROM node:24-alpine
WORKDIR /app
COPY package.json server.js ./
USER node
EXPOSE 3000
CMD ["node", "server.js"]Laptop anda~/kedai-kancil
- Tiada tingkap terbuka
Bekas (container)
- Belum ada container
Pek beku (image)
- 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), 4 image, 0 loker.
Ini api/Dockerfile. USER node jalankan API sebagai pengguna biasa, bukan root.
Cuba
Sekarang set hidangan. Tambah web, api dan db. Beri db loker db-data. Buat api tunggu sampai db sihat. Cache pilihan sahaja.
Bina set makanan: tambah service, kemudian sambung talian telefon dan loker.
Menu (tekan atau seret)
Set makanan anda
Kosong. Tekan hidangan pada menu, atau seret ke sini.
compose.yaml 1 baris
services: {}
Petunjuk
Tiga hidangan atas meja. db dapat db-data. api tunggu db: sihat.Cuba
Sekarang buat betul-betul, langkah demi langkah. Tanda anda disimpan.
Misi sebenar
mkdir -p kedai-kancil/web kedai-kancil/api && cd kedai-kancil && git initdocker compose config --quietdocker compose up -d --builddocker compose pscurl localhost:8080git remote add origin https://github.com/YOUR-NAME/kedai-kancil.gitgit add . && git commit -m "Pack the shop" && git branch -M main && git push -u origin maindocker compose down -vgit clone https://github.com/YOUR-NAME/kedai-kancil.git fresh && cd fresh && docker compose up -d
0 daripada 11 langkah siap · Tanda anda disimpan.
Semak
GitHub boleh sahkan sebahagiannya. Taip nama pengguna anda dan tekan Semak sekarang.
Semak di GitHub
- Repo kedai-kancil anda awamBelum disemak
- Ia ada compose.yamlBelum disemak
- Ia ada web/DockerfileBelum disemak
- Ia ada api/DockerfileBelum disemak
Semak
Selebihnya hanya anda boleh lihat. Tanda yang betul.
Semak sendiri
0 daripada 5 langkah siap · Tanda anda disimpan.
Anda berjaya!
Kini anda boleh bungkus seluruh aplikasi supaya satu arahan hidupkannya di mana-mana.
Nak tahu lebih (pilihan)
Di mana cerita ini tak tepat. Set hidangan sebenar datang atas satu dulang. Hidangan Compose ialah bekas berasingan. Mereka cuma kongsi talian telefon dan loker. Hidangan dimakan sekali; bekas boleh berhenti dan hidang semula. Compose juga masak di satu dapur sahaja. Banyak mesin perlukan medan selera: Bahagian 3.
Misi 2 (Dunia C2) tambah talian hijau.
English
Mission 1: pack it! Kedai Kancil gets its own repo, kedai-kancil. First, put each file in its folder.
<h1>Kedai Kancil</h1>FROM nginx:1.30-alpine
COPY index.html /usr/share/nginx/html/Top: web/index.html. Below: web/Dockerfile. Two lines: start from nginx, copy your page in.
The API code is below: api/server.js, then api/package.json. Its recipe will have no npm ci step. Why is that fine?
const http = require('node:http');
const port = process.env.PORT || 3000;
http.createServer((req, res) => {
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ ok: true, shop: 'Kedai Kancil' }));
}).listen(port, () => console.log(`API listening on port ${port}`));{
"name": "kedai-api",
"version": "1.0.0",
"private": true,
"scripts": {
"start": "node server.js",
"test": "node --test"
}
}
FROM node:24-alpine
WORKDIR /app
COPY package.json server.js ./
USER node
EXPOSE 3000
CMD ["node", "server.js"]This is api/Dockerfile. USER node runs the API as a normal user, not root.
Now the set meal. Add web, api and db. Give db the locker db-data. Make api wait until db is healthy. The cache is optional.
Now do it for real, step by step. Your ticks are saved.
GitHub can confirm part of it. Type your username and press Check now.
The rest only you can see. Tick what is true.
You can now pack a whole app so one command starts it anywhere.
Anda dah siapkan ini. Latihan lagi pun bagus!
Dokumentasi rasmi: Compose quickstart · Dockerfile reference · Compose file reference
Anda dah belajar 25 minit. Rehat sekejap bantu otak anda.
Pecutan selesai! Fokus yang hebat. Regangkan badan seminit?