0Pricing
Docker & Kubernetes for Developers · レッスン

Docker Composeネットワークでコンテナを接続する

Docker Composeで複数コンテナのアプリを定義し、自動作成されたブリッジネットワーク上でサービス名を使って通信させます。

「Docker Composeネットワークでコンテナを接続する」はCoddyKit上の無料Docker & Kubernetes for Developersレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはDocker & Kubernetes for Developers学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Docker & Kubernetes for Developersコースには全4レッスンが含まれています。

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

Why Compose for Networking

Running many containers and wiring their networks by hand is tedious. Docker Compose describes your whole stack in one YAML file and creates a dedicated network so services can find each other.

The Default Compose Network

When you run docker compose up, Compose creates a single user-defined bridge network for the project. Every service joins it automatically, so no manual docker network create is needed.

Service Names as Hostnames

On a Compose network, each service is reachable by its service name via built-in DNS. A service named db is reachable at the hostname db from any other service.

A Minimal Compose File

Two services on one network: a web app and a database.

version: "3.9"
services:
  web:
    image: myapp:latest
    ports:
      - "8080:80"
  db:
    image: postgres:16
    environment:
      POSTGRES_PASSWORD: secret

Connecting to db by Name

Inside the web container, the connection string points to the service name, not an IP:

DATABASE_URL=postgres://postgres:secret@db:5432/postgres

Defining Custom Networks

You can declare named networks to isolate groups of services.

services:
  web:
    image: myapp
    networks: [frontend]
  api:
    image: myapi
    networks: [frontend, backend]
  db:
    image: postgres
    networks: [backend]
networks:
  frontend:
  backend:

Network Isolation in Practice

In the example above, web can reach api but NOT db, because they share no network. This limits the blast radius if a frontend container is compromised.

Exposing vs Publishing Ports

expose makes a port reachable only to other containers on the network. ports publishes it to the host. Internal services like databases should usually only be exposed, never published.

Aliases for Services

You can give a service extra DNS names with aliases, handy when migrating from an old hostname.

services:
  db:
    image: postgres
    networks:
      backend:
        aliases:
          - database
          - legacy-db
networks:
  backend:

Connecting to External Networks

Set a network as external: true to attach services to a network created outside this Compose file, sharing it across projects.

networks:
  shared:
    external: true

Inspecting the Network

List and inspect the network Compose created to confirm which containers are attached.

docker network ls
docker network inspect myproject_default

Quick Check

Test what you have learned.

Recap

You learned that Docker Compose creates a network per project, lets services talk by service name, and supports custom networks for isolation, expose vs ports, aliases, and external networks.

よくある質問

「Docker Composeネットワークでコンテナを接続する」レッスンは無料ですか?

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

「Docker Composeネットワークでコンテナを接続する」で何を学びますか?

Docker Composeで複数コンテナのアプリを定義し、自動作成されたブリッジネットワーク上でサービス名を使って通信させます。 ブラウザで直接実行するハンズオンコードでDocker & Kubernetes for Developersを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Docker & Kubernetes for Developersを始めるのに経験は必要ですか?

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

「Docker Composeネットワークでコンテナを接続する」レッスンにはどのくらい時間がかかりますか?

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

このDocker & Kubernetes for Developersレッスンでコードを書いて実行できますか?

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

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

  1. Dockerネットワークの種類を理解する
  2. コンテナネットワークの設定
  3. ボリュームによるデータ永続化
  4. Docker Composeネットワークでコンテナを接続する
← Docker & Kubernetes for Developersに戻る