0Pricing
Flask Academy · レッスン

本番用Dockerfileを書く

軽量で再現可能なコンテナーイメージを構築します

「本番用Dockerfileを書く」はCoddyKit上の無料Flask Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはFlask Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Flask Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Why Containerize

A Docker image packs your code, Python, and every dependency into one unit that runs the same on any machine. No more works on my laptop. 🐳

The Dockerfile

A Dockerfile is a recipe. Each line is a step Docker runs to build your image, from base layer to final run command.

Pick a Slim Base

Start from an official python image. The slim variant keeps the image small while still giving you a working Python runtime.

FROM python:3.12-slim

Set a Working Directory

Use WORKDIR to choose where your code lives inside the image. Every later command runs relative to this folder.

WORKDIR /app

Copy Requirements First

Copy requirements.txt before your code. This lets Docker cache the install layer and skip it when only your code changes.

COPY requirements.txt .

Install Dependencies

Run pip install with no cache to keep the layer lean. This bakes Flask and Gunicorn right into the image.

RUN pip install --no-cache-dir -r requirements.txt

Copy the Rest of the Code

Now COPY the rest of your project in. Because it comes after the install, edits to your code do not bust the dependency cache.

COPY . .

Document the Port

Use EXPOSE to declare which port your app listens on. It is documentation plus a hint for tooling that wires things up.

EXPOSE 8000

Define the Start Command

The CMD line is what runs when the container starts. Point it at Gunicorn so the container boots straight into your server.

CMD ["gunicorn", "--bind", "0.0.0.0:8000", "wsgi:app"]

Ignore the Junk

Add a .dockerignore file so caches, venvs, and git data never enter the image. Smaller builds are faster and safer.

__pycache__/
.venv/
.git/

Build and Run It

Build with docker build, then run the image and map a port. Your packaged Flask app is now portable to any host.

docker build -t myapp .
docker run -p 8000:8000 myapp

Quick Check

Why copy requirements.txt before the rest of your code?

Recap

Your Dockerfile builds from a slim base, installs deps, copies code, and launches Gunicorn. One image runs anywhere. Next: put Nginx in front. 🔧

よくある質問

「本番用Dockerfileを書く」レッスンは無料ですか?

はい。「本番用Dockerfileを書く」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Flask Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Flask Academyコースには全4レッスンが含まれています。

「本番用Dockerfileを書く」で何を学びますか?

軽量で再現可能なコンテナーイメージを構築します ブラウザで直接実行するハンズオンコードでFlask Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Flask Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのFlask Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。

「本番用Dockerfileを書く」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このFlask Academyレッスンでコードを書いて実行できますか?

はい。すべてのFlask Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. 組み込み開発サーバーを使わない理由
  2. GunicornでFlaskを実行する
  3. 本番用Dockerfileを書く
  4. NginxとComposeを前段に置く
← Flask Academyに戻る