Peta dunia

 

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

cat tunjuk Dockerfile tiga baris: FROM node:24-alpine, COPY hello.js /hello.js, CMD node /hello.js. Transkrip Docker: $ cat Dockerfile FROM node:24-alpine COPY hello.js /hello.js CMD ["node", "/hello.js"]

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"]

Tengok

docker build bakar dua langkah: FROM node:24-alpine, kemudian COPY hello.js. docker images kini senaraikan hello:1.0. Transkrip Docker: $ docker build -t hello:1.0 . [+] Building 0.1s (7/7) FINISHED docker:default => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 107B 0.0s => [internal] load metadata for docker.io/library/node:24-alpine 0.0s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [1/2] FROM docker.io/library/node:24-alpine@sha256:8a32c8cf649a49915a6 0.0s => => resolve docker.io/library/node:24-alpine@sha256:8a32c8cf649a49915a6 0.0s => [internal] load build context 0.0s => => transferring context: 141B 0.0s => [2/2] COPY hello.js /hello.js 0.0s => exporting to image 0.1s => => exporting layers 0.0s => => exporting manifest sha256:44e03045f4c8b90ad6efee4446fa5b308798c4009 0.0s => => exporting config sha256:9c60021cdc96a7a0c482f11cac463756a3dab506a44 0.0s => => exporting attestation manifest sha256:069105ba22e077bc68b712e352a68 0.0s => => exporting manifest list sha256:7063ee281c8ba71514cc623bfa4a8d36ca2d 0.0s => => naming to docker.io/library/hello:1.0 0.0s => => unpacking to docker.io/library/hello:1.0 0.0s $ docker images hello i Info → U In Use IMAGE ID DISK USAGE CONTENT SIZE EXTRA hello:1.0 7063ee281c8b 244MB 62.5MB

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.

Teka

Anda tukar mesej dalam hello.js. Kemudian anda jalankan hello:1.0 lagi. Apa yang keluar?

Pilih satu jawapan.

Semak

Selepas hello.js berubah, hello:1.0 masih cetak Hello from Kedai Kancil. Binaan baharu, hello:1.1, cetak Hai lagi. Transkrip Docker: $ docker build -t hello:1.0 . [+] Building 0.1s (7/7) FINISHED docker:default => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 107B 0.0s => [internal] load metadata for docker.io/library/node:24-alpine 0.0s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [1/2] FROM docker.io/library/node:24-alpine@sha256:8a32c8cf649a49915a6 0.0s => => resolve docker.io/library/node:24-alpine@sha256:8a32c8cf649a49915a6 0.0s => [internal] load build context 0.0s => => transferring context: 141B 0.0s => [2/2] COPY hello.js /hello.js 0.0s => exporting to image 0.1s => => exporting layers 0.0s => => exporting manifest sha256:44e03045f4c8b90ad6efee4446fa5b308798c4009 0.0s => => exporting config sha256:9c60021cdc96a7a0c482f11cac463756a3dab506a44 0.0s => => exporting attestation manifest sha256:069105ba22e077bc68b712e352a68 0.0s => => exporting manifest list sha256:7063ee281c8ba71514cc623bfa4a8d36ca2d 0.0s => => naming to docker.io/library/hello:1.0 0.0s => => unpacking to docker.io/library/hello:1.0 0.0s $ echo "console.log('Hai lagi!');" > hello.js $ docker run --rm hello:1.0 Hello from Kedai Kancil! $ docker build -t hello:1.1 . [+] Building 0.1s (7/7) FINISHED docker:default => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 107B 0.0s => [internal] load metadata for docker.io/library/node:24-alpine 0.0s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [1/2] FROM docker.io/library/node:24-alpine@sha256:8a32c8cf649a49915a6 0.0s => => resolve docker.io/library/node:24-alpine@sha256:8a32c8cf649a49915a6 0.0s => [internal] load build context 0.0s => => transferring context: 126B 0.0s => [2/2] COPY hello.js /hello.js 0.0s => exporting to image 0.1s => => exporting layers 0.0s => => exporting manifest sha256:c6ef1a96f61d1ec6804b4ba4bfeb1e1ed2accc40f 0.0s => => exporting config sha256:d242cdff7c93b75afc3520cbd6f47f57a0df440c8d9 0.0s => => exporting attestation manifest sha256:b6268118f4da4929ec6a35bb5cf10 0.0s => => exporting manifest list sha256:b602534f7b4cc1dc5d80849b1a58d9348608 0.0s => => naming to docker.io/library/hello:1.1 0.0s => => unpacking to docker.io/library/hello:1.1 0.0s $ docker run --rm hello:1.1 Hai lagi!

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

  1. mkdir hello && cd hello
  2. docker build -t hello:1.0 .
  3. docker run --rm hello:1.0

0 daripada 4 langkah siap · Tanda anda disimpan.