M
M
MikUrrey2021-05-06 23:05:25
Computer networks
MikUrrey, 2021-05-06 23:05:25

How to make the network of the host machine "visible" in the docker network?

Greetings!
On a host with Ubuntu 20, a cluster of several containers was raised, interconnected into one network.
The cluster is an nginx + php-fpm + mysql + memcached + redis development stack.
Nginx "twirls" several "sites", their names (test.local, project.loc, etc.) are registered in the hosts file and are easily opened in the host browser. But there is also a need for these names to be available from scripts hosted on these "sites" themselves in order to perform, for example, or run REST API acceptance testing. Roughly speaking, how to make test.local, hosted in a cluster, "see itself from the outside"? Preferably without having to add hosts manually.
getimagesize("http://project.loc/image.jpg");

docker-compose

version: '3'
services:
  #PHP
  php:
    build:
      context: ./image/php/${PHP_VERSION}/.
    image: openweb/php:${PHP_VERSION}
    container_name: os-php${PHP_VERSION}
    restart: unless-stopped
    tty: true
    environment:
      SERVICE_NAME: app
      SERVICE_TAGS: dev
    working_dir: /var/www
    volumes:
      - ./www:/var/www
      - ./config/php/php.ini:/usr/local/etc/php/php.ini
      - ./scripts:/bin/app
  #Nginx Service
  webserver:
    image: nginx:alpine
    container_name: os-nginx
    restart: unless-stopped
    tty: true
    ports:
      - "${WWW_PORT}:80"
      - "${WWW_SECURITY_PORT}:443"
    volumes:
      - ./www:/var/www
      - ./config/nginx/:/etc/nginx/conf.d/
      - ./scripts:/bin/app
  #MySQL Service
  mysql:
    image: mysql:5.7.24
    container_name: os-mysql5.7.24
    restart: unless-stopped
    tty: true
    ports:
      - "${MYSQL_PORT}:3306"
    volumes:
      - ./db/mysql:/var/lib/mysql
      - ./scripts:/bin/app
    environment:
      MYSQL_ROOT_PASSWORD: "${MYSQL_PASSWORD}"
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql
  #Redis Service
  redis:
    image: redis
    container_name: os-redis
    restart: unless-stopped
    tty: true
    ports:
      - "${REDIS_PORT}:6379"
    volumes:
      - ./db/redis:/var/lib/redis
      - ./scripts:/bin/app
    environment:
      - REDIS_REPLICATION_MODE=master
  #Memcached
  memcached:
    image: memcached
    container_name: os-memcached
    restart: unless-stopped
    tty: true
    ports:
      - "${MEMCACHED_PORT}:11211"
#Volumes
volumes:
  dbdata:
    driver: local

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2021-05-06
@MikUrrey

This is called network aliases
Example:

services:
  proxy:
    image: jwilder/nginx-proxy
    ports:
      - 0.0.0.0:80:80
      - 0.0.0.0:443:443
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock
      - ./certs/:/etc/nginx/certs:ro
      - ./jira-nginx.conf:/etc/nginx/vhost.d/jira.local.net
    networks:
      proxy:
        aliases:
          - "site.local.net"

Another container will be able to access it by container name ( proxy ) - as usual, as well as by alias - site.local.net
That is, the line with the address and name of this container will be in the hosts files of all other stack containers.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question