Skip to content
An`s Weblog
Go back

Entrypoint vs Command

Thông thường ENTRYPOINT sẽ định nghĩa path tới process sẽ được thực thi, CMD định nghĩa param (nếu có).

Mặc định không định nghĩa ENTRYPOINT, docker sẽ hiểu entry point là

/bin/sh -c

CMD – Khi nào dùng?

Ví dụ:

CMD ["npm", "start"]
docker run myimage ["npm", "run", "test"] # Ghi đè CMD

ENTRYPOINT – Khi nào dùng?

ENTRYPOINT làm cho container có thể hoạt động như một command riêng biệt (executable).

Ví dụ:

ENTRYPOINT ["./bin/www"]

Dùng kết hợp ENTRYPOINT + CMD

CMD sẽ được append vào ENTRYPOINT


Ví dụ với Prometheus

ENTRYPOINT ["/bin/prometheus"]
CMD ["--config.file=/etc/prometheus/prometheus.yml", "--storage.tsdb.path=/prometheus"]

Khi chạy:

docker run prom-image

sẽ thực hiện:

/bin/prometheus --config.file=... --storage.tsdb.path=...

Thực hành với Weather App

1. Tạo Dockerfile mới:

ENTRYPOINT ["./bin/www"]

2. Build image:

docker image build -t linuxacademy/weather-app:v4 .

3. Chạy container:

docker container run -d --name weather-app4 linuxacademy/weather-app:v4

4. Kiểm tra bằng docker container inspect weather-app4

- Không có `Cmd`, chỉ có `Entrypoint`
- `Args` chứa `["-c", "./bin/www"]`

5. Append thêm command:

docker container run --name weather-app5 linuxacademy/weather-app:v4 echo hello world

hello world được thêm vào cuối entrypoint


Ví dụ thực tế: Prometheus

1. Tạo 2 volume:

docker volume create prometheus-conf
docker volume create prometheus-data

2. Gán quyền nobody (UID 65534):

sudo chown -R 65534:65534 /var/lib/docker/volumes/prometheus-*/_data

3. Run container với volume và flags:

docker container run -d --name prometheus \
  -v prometheus-conf:/etc/prometheus \
  -v prometheus-data:/prometheus \
  prom/prometheus \
  --config.file=/etc/prometheus/prometheus.yml \
  --storage.tsdb.path=/prometheus

4. Inspect:

docker container inspect prometheus

Thấy Path: /bin/prometheus, Args: là các flags được thêm vào

Tóm tắt

Thành phầnCMDENTRYPOINT
Ghi đè được?Có thể ghi đè qua CLIKhông (trừ khi dùng --entrypoint)
Vai tròThiết lập lệnh mặc địnhThiết lập command chính cho container
Thường dùngTùy biến, linh hoạtKhi container cần cố định behavior
Kết hợpVới ENTRYPOINT để thêm tham sốGọi command chính (executable)

Share this post:

Previous Post
Using dockerignore
Next Post
Using the Volume Instruction