###### tags: `Study Circle Note`, `Docker in Action 2ed` # Docker in Action 2ed 第八次 2021-09-25 - Chapter 8. Building images automatically with Dockerfiles ## 討論 - 「單純 expose 一個 port 可以幹嘛?」 > The EXPOSE allows you to define private (container) and public (host) ports to expose at image build time for when the container is running if you run the container with -P. > > [官方文件](https://docs.docker.com/engine/reference/builder/#expose) - 「為何在 docker 中不要用 root?」 > 怕危險,如果後續程式有漏洞,很容易擴大傷害範圍。 - 「CMD 中 exec form VS shell form?」 > https://stackoverflow.com/questions/42805750/differences-between-dockerfile-instructions-in-shell-and-exec-form > https://stackoverflow.com/questions/47904974/what-are-shell-form-and-exec-form - 「何時會需要用到 ONBUILD 功能?」 > 多個專案共用一個自己編譯的 base image,這樣可以抽象多個專案的重複步驟。例如多個專案都要拷貝自己目錄下個 packages.json 到 node image,但是 base image 並不知道這個映像會被誰使用(當時編譯時更不會有未來的 packages.json),就可以使用 `ONBUILD COPY package.json /app` - 「ARG VS ENV?」 > build 的時候只能帶入 ARG 參數 > ![](https://blog.scottchayaa.com/post/2018/11/04/1.png) - 「build 的時候可以吃到 shell 的環境變數嗎?」 以下同等於在 dockerfile 中設定 ARG 變數(但來源從環境變數) `docker build --build-arg foo=${ENV_VARIABLE}` - 「heavyweight VS lightweight init ?」 > 功能多寡:是否能使用 cgorup、是否能完整使用 kernel 功能。 > 舉例:systemd-resolved、systemd-nspawn 等功能,其他 init 程式就沒有 - 「為何本機 build 的 image 沒有 digest?」 > 因為沒有 [Manifest](https://docs.docker.com/engine/reference/commandline/manifest/) > manifest也是一個 json 檔案,media type 為application/vnd.oci.image.manifest.v1+json,這個檔案包含了對前面 filesystem layers 和 image config 的描述 > The "digest" is a hash of the manifest, introduced in Docker registry v2. > The image ID is a hash of the local image JSON configuration. > https://stackoverflow.com/questions/56364643/whats-the-difference-between-a-docker-images-image-id-and-its-digest > https://docs.docker.com/registry/spec/api/#content-digests - 「沒有這個 UID 的狀況」 > https://stackoverflow.com/questions/58167623/running-gather-facts-true-fails-with-uid-not-found-1001