EN: Read in English
Dunia D2: Resepi
WORKDIR, RUN, ENV dan EXPOSE
Anda akan belajar: empat lagi baris resepi, dan kenapa EXPOSE tak buka tingkap.
Teks BM belum disemak oleh penutur asli.
Disemak dengan Docker Engine 29 pada 31 Okt 2026
Masalahnya
Satu resepi tulis COPY server.js . sahaja. Asas node tak tetapkan folder kerja. Di mana server.js mendarat?
Pilih satu jawapan.
Tengok
Laptop anda~/api
- Tiada tingkap terbuka
Bekas (container)
- Belum ada container
Pek beku (image)
- node:24-alpine4 layer · 244MB
0 container (0 berjalan), 1 image, 0 loker.
WORKDIR /app ialah kaunter kerja anda. Semua baris selepasnya bekerja di situ. RUN memasak semasa membakar: di sini ia pasang pakej. pwd cetak kaunter itu: /app.
Cuba
Sesetengah baris bakar satu layer (lapisan). Yang lain cuma tampal nota pada pek. Asingkan mereka.
Seret kad ke dalam kotak. Atau ketik kad, kemudian ketik kotak.
Cuba
Image sudah dibina. Jalankan dengan tingkap 3000 terbuka, kemudian ketuk dengan curl.
Laptop anda~/api
- Tiada tingkap terbuka
Bekas (container)
- Belum ada container
Pek beku (image)
- api:1.08 layer · 244MB
- node:24-alpine4 layer · 244MB
0 container (0 berjalan), 2 image, 0 loker.
Petunjuk
-p 3000:3000 buka tingkap: port laptop, kemudian port container.Teka
Resepi tulis EXPOSE 3000. Anda jalankan image tanpa -p. Boleh laptop anda capai port 3000?
Pilih satu jawapan.
Semak
Laptop anda~/api
- Tiada tingkap terbuka
Bekas (container)
- Belum ada container
Pek beku (image)
- node:24-alpine4 layer · 244MB
0 container (0 berjalan), 1 image, 0 loker.
Ralat sebenar: Couldn't connect to server. Maksudnya: tiada tingkap terbuka. docker ps tunjuk 3000/tcp tanpa anak panah.
Cuba
ENV PORT=3000 cuma nilai asal. Bina arahan yang jalankan API di port 4000.
Ketik kepingan ikut turutan yang betul.
Anda berjaya!
Kini anda boleh guna WORKDIR, RUN, ENV dan EXPOSE.
Nak tahu lebih (pilihan)
Gabungkan arahan berkaitan dalam satu RUN dengan &&. Satu RUN, satu layer. EXPOSE juga bantu docker run -P, yang buka setiap port yang di-expose pada port laptop rawak.
Misi sebenar
docker build -t hello:1.1 .docker run --rm hello:1.1 pwd
0 daripada 3 langkah siap · Tanda anda disimpan.
English
A recipe says COPY server.js . and nothing else. The node base sets no work folder. Where does server.js land?
WORKDIR /app is your work counter. Every later line works there. RUN cooks while baking: here it installs packages. pwd prints the counter: /app.
Some lines bake a layer. Others only stick a note on the pack. Sort them.
The image is built. Run it with hatch 3000 open, then knock with curl.
-p 3000:3000 opens the hatch: laptop port, then container port.The recipe says EXPOSE 3000. You run the image with no -p. Can your laptop reach port 3000?
Real error: Couldn't connect to server. Plain words: no hatch is open. docker ps shows 3000/tcp with no arrow.
ENV PORT=3000 is only a default. Build the command that runs the API on port 4000.
You can now use WORKDIR, RUN, ENV and EXPOSE.
Anda dah siapkan ini. Latihan lagi pun bagus!
Dokumentasi rasmi: Dockerfile reference: WORKDIR · Dockerfile reference: RUN · Dockerfile reference: ENV · Dockerfile reference: EXPOSE
Anda dah belajar 25 minit. Rehat sekejap bantu otak anda.
Pecutan selesai! Fokus yang hebat. Regangkan badan seminit?