[Docker] 獲取Image的三種方式

獲取Image的三種方式:

    <div class="wp-block-group">
      <div class="wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow">
        <ol class="wp-block-list">
          <li>
            從Registry取得Image &#8211; <a href="https://docs.docker.com/registry/">registry</a><ul>
              <li>
                public(公開的,任何人都可以使用)
              </li>
              <li>
                private(私有)
              </li>
            </ul>
          </li>
          
          <li>
            文件導入(可離線時使用)
          </li>
          <li>
            從Dockerfile建造
          </li>
        </ol>
      </div>
    </div>
  </div>
</div>

從Registry取得Image

如果沒有提供registry名稱的話,預設是從DockerHub取得。
如果不指定的話會下載lates。

docker image pull nginx

如果想指定版本的話就加入:tag名稱

docker image pull nginx:1.20.1

查看已下載的Image

docker image ls

顯示Image的相關訊息

docker image inspect IMAGE_ID

刪除Image

如果有容器正在使用,無論是運行中或是關閉狀態,會無法刪除。

docker image rm IMAGE_ID

文件導入Image

其他兩種都是要在有網路的狀況下才可以使用,若是電腦沒有網路的話,可以使用導入的方式。

建構Image指令如下:

docker image save nginx[:版本] -o 檔案名稱

導入Image

docker image load -i 檔案路徑

從Dockerfile建造

Dockerfile參考

將下方兩個檔案存在同一個資料夾:
檔名:hello.py

print("Hello this python!")

檔名:dockerfile

FROM ubuntu:20.04
RUN apt-get update && \
    DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y python3.9 python3-pip python3.9-dev
ADD hello.py /
CMD ["python3", "/hello.py"]

我把這兩個檔案放在名為coding的資料夾裡面。

接下來從dockerfile建構Image:

  • -t :替image命名所需帶入
docker image build -t hello dockerfile路徑
docker image ls # 查看是否有名為hello的image

下圖紅框是我們想要為這個image的命名。
黃框是dockerfile的路徑。

docker run -it hello

看看有沒有顯示hello.py的字串。