EN: Read in English
Dunia D2: Resepi
Resepi pertama: FROM, COPY, CMD
Anda akan belajar: tulis Dockerfile tiga baris dan bakar menjadi image.
Teks BM belum disemak oleh penutur asli.
Disemak dengan Docker Engine 29 pada 31 Okt 2026
Masalahnya
Aplikasi hello anda jalan di laptop anda. Di laptop Aina, ia gagal. Node dia versi lama. Apa yang boleh baiki untuk selamanya?
Pilih satu jawapan.
Tengok
Laptop anda~/hello
- Tiada tingkap terbuka
Bekas (container)
- Belum ada container
Pek beku (image)
- node:24-alpine4 layer · 244MB
0 container (0 berjalan), 1 image, 0 loker.
Dockerfile: Resepi untuk buat image. ialah resepi untuk image. FROM pilih pek beku asas. COPY masukkan makanan anda. CMD beritahu apa nak buat bila bekas dibuka.
Cuba
Susun resepi ini. Baris paling atas jalan dulu.
Baris pertama di atas. Susun ikut turutan. Guna anak panah, atau seret.
- COPY hello.js /hello.js
- FROM node:24-alpine
- CMD ["node", "/hello.js"]
Petunjuk
Anda perlu pek asas dulu sebelum boleh tambah makanan.Tengok
Laptop anda~/hello
- Tiada tingkap terbuka
Bekas (container)
- Belum ada container
Pek beku (image)
- node:24-alpine4 layer · 244MB
0 container (0 berjalan), 1 image, 0 loker.
docker build -t hello:1.0 . bakar resepi menjadi image. -t beri nama dan tag. Titik itu maksudnya “guna folder ini”.
Cuba
Giliran anda. Bina image itu, kemudian jalankan sekali.
Laptop anda~/hello
- Tiada tingkap terbuka
Bekas (container)
- Belum ada container
Pek beku (image)
- node:24-alpine4 layer · 244MB
0 container (0 berjalan), 1 image, 0 loker.
Petunjuk
Taipdocker build -t hello:1.0 . dulu. Kemudian docker run --rm hello:1.0.Teka
Anda tukar mesej dalam hello.js. Kemudian anda jalankan hello:1.0 lagi. Apa yang keluar?
Pilih satu jawapan.
Semak
Laptop anda~/hello
- Tiada tingkap terbuka
Bekas (container)
- Belum ada container
Pek beku (image)
- node:24-alpine4 layer · 244MB
0 container (0 berjalan), 1 image, 0 loker.
Pek lama masih kata hello. Binaan baharu bungkus fail baharu. Tukar resepi atau makanan? Bina semula.
Anda berjaya!
Kini anda boleh tulis resepi tiga baris dan bakar menjadi image.
Nak tahu lebih (pilihan)
Laman web atas asas nginx tak perlu CMD. Pek asas itu sudah ada satu. docker build baca fail bernama Dockerfile dalam folder yang anda beri.
Latihan sebenar (pilihan), di komputer anda sendiri:
Misi sebenar
mkdir hello && cd hellodocker build -t hello:1.0 .docker run --rm hello:1.0
0 daripada 4 langkah siap · Tanda anda disimpan.
English
Your hello app works on your laptop. On Aina’s laptop it crashes. She has an older Node. What fixes this for good?
A Dockerfile is a recipe for an image. FROM picks a frozen base pack. COPY adds your food. CMD says what to do when the box opens.
Put the recipe in order. The top line runs first.
docker build -t hello:1.0 . bakes the recipe into an image. -t gives it a name and a tag. The dot means “use this folder”.
Your turn. Build the image, then run it once.
docker build -t hello:1.0 . first. Then docker run --rm hello:1.0.You change the message in hello.js. Then you run hello:1.0 again. What prints?
The old pack still says hello. A new build packs the new file. Change the recipe or the food? Build again.
You can now write a recipe and bake it into an image.
Anda dah siapkan ini. Latihan lagi pun bagus!
Dokumentasi rasmi: Docker docs: Writing a Dockerfile · Dockerfile reference · docker build
Anda dah belajar 25 minit. Rehat sekejap bantu otak anda.
Pecutan selesai! Fokus yang hebat. Regangkan badan seminit?