Writing a DockerfileDockerfileの作成

Explanation説明

A Dockerfile is a text-based document that's used to create a container image. It provides instructions to the image builder on the commands to run, files to copy, startup command, and more.Dockerfileは、コンテナイメージを作成するために使用されるテキストベースのドキュメントです。これは、実行するコマンド、コピーするファイル、起動コマンドなどに関する指示をイメージビルダーに提供します。

As an example, the following Dockerfile would produce a ready-to-run Python application:例えば、以下のDockerfileは、実行可能なPythonアプリケーションを生成します:

FROM python:3.13
WORKDIR /usr/local/app

# Install the application dependencies
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

# Copy in the source code
COPY src ./src
EXPOSE 8080

# Setup an app user so the container doesn't run as the root user
RUN useradd app
USER app

CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8080"]

Common instructions一般的な命令

Some of the most common instructions in a Dockerfile include:Dockerfileで最も一般的な命令のいくつかは次のとおりです:

  • FROM <image> - this specifies the base image that the build will extend.FROM <image> - これは、ビルドが拡張するベースイメージを指定します。
  • WORKDIR <path> - this instruction specifies the "working directory" or the path in the image where files will be copied and commands will be executed.WORKDIR <path> - この命令は、「作業ディレクトリ」またはファイルがコピーされ、コマンドが実行されるイメージ内のパスを指定します。
  • COPY <host-path> <image-path> - this instruction tells the builder to copy files from the host and put them into the container image.COPY <host-path> <image-path> - この命令はビルダーにホストからファイルをコピーしてコンテナイメージに入れるよう指示します。
  • RUN <command> - this instruction tells the builder to run the specified command.RUN <command> - この命令はビルダーに指定されたコマンドを実行するよう指示します。
  • ENV <name> <value> - this instruction sets an environment variable that a running container will use.ENV <name> <value> - この命令は実行中のコンテナが使用する環境変数を設定します。
  • EXPOSE <port-number> - this instruction sets configuration on the image that indicates a port the image would like to expose.EXPOSE <port-number> - この命令はイメージが公開したいポートを示す設定をイメージに行います。
  • USER <user-or-uid> - this instruction sets the default user for all subsequent instructions.USER <user-or-uid> - この命令はすべての後続の命令のデフォルトユーザーを設定します。
  • CMD ["<command>", "<arg1>"] - this instruction sets the default command a container using this image will run.CMD ["<command>", "<arg1>"] - この命令はこのイメージを使用するコンテナが実行するデフォルトコマンドを設定します。

To read through all of the instructions or go into greater detail, check out the Dockerfile reference.すべての命令を読み通したり、詳細を確認したりするには、Dockerfileリファレンスをチェックしてください。

Try it out試してみる

Just as you saw with the previous example, a Dockerfile typically follows these steps:前の例で見たように、Dockerfileは通常次のステップに従います:

  1. Determine your base imageベースイメージを決定する
  2. Install application dependenciesアプリケーションの依存関係をインストールする
  3. Copy in any relevant source code and/or binaries関連するソースコードやバイナリをコピーします
  4. Configure the final image最終イメージを構成します

In this quick hands-on guide, you'll write a Dockerfile that builds a simple Node.js application. If you're not familiar with JavaScript-based applications, don't worry. It isn't necessary for following along with this guide.このクイックハンズオンガイドでは、シンプルなNode.jsアプリケーションを構築するDockerfileを作成します。JavaScriptベースのアプリケーションに不慣れな場合でも心配はいりません。このガイドに従うために必要ではありません。

Set upセットアップ

Download this ZIP file and extract the contents into a directory on your machine.このZIPファイルをダウンロードし、内容をあなたのマシンのディレクトリに展開します。

If you'd rather not download a ZIP file, clone the https://github.com/docker/getting-started-todo-app project and checkout the build-image-from-scratch branch.ZIPファイルをダウンロードしたくない場合は、https://github.com/docker/getting-started-todo-appプロジェクトをクローンし、build-image-from-scratchブランチをチェックアウトします。

Creating the DockerfileDockerfileの作成

Now that you have the project, you’re ready to create the Dockerfile.プロジェクトができたので、Dockerfileを作成する準備が整いました。

  1. Download and install Docker Desktop.Docker Desktopをダウンロードしてインストールします。

  2. Examine the project.プロジェクトを調べます。

    Explore the contents of getting-started-todo-app/app/. You'll notice that a Dockerfile already exists. It is a simple text file that you can open in any text or code editor.getting-started-todo-app/app/の内容を探索します。Dockerfileがすでに存在することに気付くでしょう。それは、任意のテキストまたはコードエディタで開くことができるシンプルなテキストファイルです。

  3. Delete the existing Dockerfile.既存のDockerfileを削除します。

    For this exercise, you'll pretend you're starting from scratch and will create a new Dockerfile.この演習では、ゼロから始めるふりをして、新しいDockerfileを作成します。

  4. Create a file named Dockerfile in the getting-started-todo-app/app/ folder.Dockerfileという名前のファイルをgetting-started-todo-app/app/フォルダーに作成します。

    Dockerfile file extensionsDockerfileのファイル拡張子

    It's important to note that the Dockerfile has no file extension. Some editors will automatically add an extension to the file (or complain it doesn't have one).Dockerfileには拡張子がないことに注意することが重要です。一部のエディタはファイルに自動的に拡張子を追加したり、拡張子がないことを不満に思ったりします。

  5. In the Dockerfile, define your base image by adding the following line:Dockerfile内で、次の行を追加してベースイメージを定義します。

    FROM node:22-alpine
  6. Now, define the working directory by using the WORKDIR instruction. This will specify where future commands will run and the directory files will be copied inside the container image.次に、WORKDIR命令を使用して作業ディレクトリを定義します。これにより、今後のコマンドが実行される場所と、コンテナイメージ内にファイルがコピーされるディレクトリが指定されます。

    WORKDIR /app
  7. Copy all of the files from your project on your machine into the container image by using the COPY instruction:COPY命令を使用して、マシン上のプロジェクトからすべてのファイルをコンテナイメージにコピーします:

    COPY . .
  8. Install the app's dependencies by using the yarn CLI and package manager. To do so, run a command using the RUN instruction:yarn CLIとパッケージマネージャーを使用してアプリの依存関係をインストールします。そのためには、RUN命令を使用してコマンドを実行します:

    RUN yarn install --production
  9. Finally, specify the default command to run by using the CMD instruction:最後に、CMD命令を使用して実行するデフォルトのコマンドを指定します:

    CMD ["node", "./src/index.js"]

    And with that, you should have the following Dockerfile:これで、次のDockerfileが完成するはずです。

    FROM node:22-alpine
    WORKDIR /app
    COPY . .
    RUN yarn install --production
    CMD ["node", "./src/index.js"]

This Dockerfile isn't production-ready yetこのDockerfileはまだ本番環境用ではありません

It's important to note that this Dockerfile is not following all of the best practices yet (by design). It will build the app, but the builds won't be as fast, or the images as secure, as they could be.このDockerfileはまだすべてのベストプラクティスに従っていないことに注意することが重要です(設計上)。アプリをビルドしますが、ビルドはそれほど速くなく、イメージもそれほど安全ではありません。

Keep reading to learn more about how to make the image maximize the build cache, run as a non-root user, and multi-stage builds.イメージをビルドキャッシュを最大化し、非ルートユーザーとして実行し、マルチステージビルドを行う方法についてさらに学ぶために読み続けてください。

Containerize new projects quickly with docker initdocker initで新しいプロジェクトを迅速にコンテナ化する

The docker init command will analyze your project and quickly create a Dockerfile, a compose.yaml, and a .dockerignore, helping you get up and going. Since you're learning about Dockerfiles specifically here, you won't use it now. But, learn more about it here.この docker init コマンドは、あなたのプロジェクトを分析し、迅速に Dockerfile、compose.yaml、および .dockerignore を作成し、あなたが 始める手助けをします。ここでは特にDockerfileについて学んでいるので、 今は使用しません。しかし、 ここで詳細を学んでください

Additional resources追加リソース

To learn more about writing a Dockerfile, visit the following resources:Dockerfileの書き方について詳しく学ぶには、以下のリソースを訪れてください:

Next steps次のステップ

Now that you have created a Dockerfile and learned the basics, it's time to learn about building, tagging, and pushing the images.Dockerfileを作成し、基本を学んだので、次はイメージのビルド、タグ付け、プッシュについて学ぶ時です。

Build, tag and publish the Image