Home > Software engineering >  What are docker networks are needed for?
What are docker networks are needed for?

Time:09-24

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:

CodePudding user response:

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

CodePudding user response:

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

Macvlan network tutorial,

Overlay networking tutorial,

Host networking tutorial,

Bridge network tutorial.

  • Related