Roman Karpyshyn
Roman Karpyshyn

Reputation: 199

What are docker networks are needed for?

Please explain why docker network is needed? I read some documentation, but on a practical example, I don't understand why we need to manipulate the network. Here I have a docker-compose in which everything works fine with and without network. Explain me please, what benefits will be in practical use if you uncomment docker-compose in the right places? Now my containers interact perfectly, there are migrations from the ORM to the database, why do I need a networks?

 version: '3.4'

services:
  main:
    container_name: main
    build:
      context: .
      target: development
    volumes:
      - .:/usr/src/app
      - /usr/src/app/node_modules
    ports:
      - ${PORT}:${PORT}
    command: npm run start:dev
    env_file:
      - .env
#     networks:
#       - webnet
    depends_on:
      - postgres
  postgres:
    container_name: postgres
    image: postgres:12
#     networks:
#       - webnet
    environment:
      POSTGRES_PASSWORD: ${DB_PASSWORD}
      POSTGRES_USER: ${DB_USERNAME}
      POSTGRES_DB: ${DB_DATABASE_NAME}
      PG_DATA: /var/lib/postgresql/data
    ports:
      - 5432:5432
    volumes:
      - pgdata:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 1m30s
      timeout: 10s
      retries: 3
# networks:
#   webnet:
volumes:
  pgdata:

Upvotes: 3

Views: 1505

Answers (3)

Ali Azadi
Ali Azadi

Reputation: 41

Without config network for containers, all container IPs are in one range (Ex. 172.17.0.0/16). also, they can see each other by name of the container (Docker internal DNS).

Simple usage of networking in docker:

When you want to have numbers of containers in a specific network range isolated, you must use the network of docker.

Upvotes: 1

fireman777
fireman777

Reputation: 181

Here explained Docker network - Networking overview, and here are tutorials:

Macvlan network tutorial,

Overlay networking tutorial,

Host networking tutorial,

Bridge network tutorial.

Upvotes: 0

Brady Dean
Brady Dean

Reputation: 3573

If no networks are defined, docker-compose will create a default network with a generated name. Otherwise you can manually specify the network and its name in the compose file.

You can read more at Networking in Compose

Upvotes: 2

Related Questions