Connettere i container con le reti Docker Compose
Usi Docker Compose per definire applicazioni composte da più container e permettere loro di comunicare su una bridge network creata automaticamente, usando i nomi dei servizi.
Connettere i container con le reti Docker Compose è una lezione Docker & Kubernetes for Developers gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Docker & Kubernetes for Developers, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Docker & Kubernetes for Developers include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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: secretConnecting 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/postgresDefining 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: trueInspecting the Network
List and inspect the network Compose created to confirm which containers are attached.
docker network ls
docker network inspect myproject_defaultQuick 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.
Domande Frequenti
La lezione «Connettere i container con le reti Docker Compose» è gratuita?
Sì — il testo completo di «Connettere i container con le reti Docker Compose» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Docker & Kubernetes for Developers, passa a CoddyKit PRO. Il corso Docker & Kubernetes for Developers include 4 lezioni in totale.
Cosa imparerò in «Connettere i container con le reti Docker Compose»?
Usi Docker Compose per definire applicazioni composte da più container e permettere loro di comunicare su una bridge network creata automaticamente, usando i nomi dei servizi. Eserciti Docker & Kubernetes for Developers con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Docker & Kubernetes for Developers?
Non è richiesta alcuna esperienza precedente. Docker & Kubernetes for Developers su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Connettere i container con le reti Docker Compose»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Docker & Kubernetes for Developers?
Sì. Ogni lezione Docker & Kubernetes for Developers include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Comprendere i tipi di rete Docker
- Configurazione delle reti dei container
- Persistenza dei dati con i volumi
- Connettere i container con le reti Docker Compose