简体   繁体   中英

multiple docker compose container - single ip

I have this application.

version: "2"
services:
  web:
   image: kartoffeltoby/typo3:latest
   hostname: localhost
   container_name: web
   ports:
    - 80:80
   link:
    - db:database
   volumes:
    - ./www/:/var/www/
   environment:
    - DOCROOT=/var/www/web
    - PAGESPEED=Off


  db:
   image: mysql:5.6
   hostname: database
   volumes:
    - ./DB/:/var/lib/mysql/
    - ./db/:/var/tmp/db
   environment:
    - MYSQL_ROOT_PASSWORD=passwort

The solution is:

network_mode: service:web

version: "2"
  services:
    typo3:
     image: kartoffeltoby/typo3:latest
     hostname: axdbw.vagrant
     container_name: typo3
     volumes:
      - ./www/:/var/www/
     environment:
      - DOCROOT=/var/www/web
      - PAGESPEED=Off
     networks:
      mynet:
        ipv4_address: 172.16.47.14

    db:
     image: mysql:5.6
     volumes:
      - ./DB/:/var/lib/mysql/
      - ./db/:/var/tmp/db
     environment:
      - MYSQL_ROOT_PASSWORD=passwort
     network_mode: service:web


  networks:
    mynet:
      driver: bridge
      ipam:
        driver: default
        config:
        - subnet: 172.16.47.0/24

There is no reason to use a specific IP. The solution can be as simple as this:

version: "3"
  services:
    typo3:
     image: kartoffeltoby/typo3:latest
     container_name: typo3
     networks:
      - mynet

    db:
     image: mysql:5.6
     network_mode: "service:typo3"
  networks:
    mynet:
      driver: bridge

This will place the db container onto the same IP as the typo3 container. Beware of possible port collisions.

In my use-case, I had to avoid nasty cross origin errors. Hosting the database on the same IP as my frontend container helped get around those issues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM