Dockerfileの読み方とコンテナの実行

コンテナ化と仮想化の基礎概念

Julia Ostheimer

Freelance AI Consultant

Docker用語の復習

 

DockerfileからDockerコンテナへ

コンテナ化と仮想化の基礎概念

Docker命令とDockerコマンドの違い

  • Docker命令:Dockerイメージのビルド方法を記述

DockerfileのDocker命令

  • Dockerコマンド:CLIから実行するコマンド

CLIのDockerコマンド

コンテナ化と仮想化の基礎概念

Dockerfileの書式

 

Dockerfile - コメント

コンテナ化と仮想化の基礎概念

Dockerfileの書式

 

Dockerfile - 命令

コンテナ化と仮想化の基礎概念

Dockerfileの書式

 

Dockerfile - Docker命令のコマンド

コンテナ化と仮想化の基礎概念

Dockerfileの書式

 

Dockerfile - Docker命令の引数

コンテナ化と仮想化の基礎概念

Dockerfileの順次実行

  • 順次実行
  • Dockerfileの先頭:
    • メタデータ
    • コメント
    • 引数
    • FROM 命令
コンテナ化と仮想化の基礎概念

Docker命令の概要

  • 主要なDocker命令
    • FROM
    • COPY
    • RUN
    • ENTRYPOINT
コンテナ化と仮想化の基礎概念

FROM命令

  • 既存のDockerイメージを指定
  • ビルドの基盤となるイメージを定義
    • 「出発点」  

構文:

FROM <name_of_image>

例:

# Define the image on which to build
FROM python:3.10
コンテナ化と仮想化の基礎概念

COPY命令

  • ファイルやディレクトリをコピー
    • ソース (<source>) から宛先 (<destination>) へ
    • 後続のDocker命令で必要なファイル  

構文:

COPY <source> <destination>

例:

# Copy files/folders to the main folder of the container
COPY . .
コンテナ化と仮想化の基礎概念

RUN命令

  • コンテナ内でコマンドを実行
    • CLIで実行可能な任意のコマンドを使用可能

 

構文:

RUN <command>

例:

# Install the application's dependencies
RUN pip install -r requirements.txt
コンテナ化と仮想化の基礎概念

ENTRYPOINT命令

  • コンテナのデフォルト動作を定義
    • 起動時に実行するコマンドを指定
    • コンテナの主目的  

構文:

ENTRYPOINT ["command", "argument"]

例:

# Run the script when the container starts
ENTRYPOINT ["python", "hello_world.py"]
コンテナ化と仮想化の基礎概念

Dockerfileへの命令の組み立て

# Define the image on which to build
FROM python:3.10
コンテナ化と仮想化の基礎概念

Dockerfileへの命令の組み立て

# Define the image on which to build
FROM python:3.10

# Copy files/folders to the main folder of the container
COPY . .
コンテナ化と仮想化の基礎概念

Dockerfileへの命令の組み立て

# Define the image on which to build
FROM python:3.10

# Copy files/folders to the main folder of the container
COPY . .

# Install the application's dependencies
RUN pip install -r requirements.txt
コンテナ化と仮想化の基礎概念

Dockerfileへの命令の組み立て

# Define the image on which to build
FROM python:3.10

# Copy files/folders to the main folder of the container
COPY . .

# Install the application's dependencies
RUN pip install -r requirements.txt

# Run the script when the container starts
ENTRYPOINT ["python", "hello_world.py"]
コンテナ化と仮想化の基礎概念

docker buildコマンド

  • DockerfileからDockerイメージをビルド
    • Dockerfileはビルドのコンテキスト (<context>) に配置が必要
    • CLIからDockerクライアント経由で実行

 

構文:

docker build <context>
コンテナ化と仮想化の基礎概念

docker runコマンド

  • DockerイメージからDockerコンテナを作成・実行
    • Dockerイメージを引数 (<name_of_image>) として指定
    • CLIからDockerクライアント経由で実行

 

構文:

docker run <name_of_image>
コンテナ化と仮想化の基礎概念

では、練習しましょう!

コンテナ化と仮想化の基礎概念

Preparing Video For Download...