Docker将数据库从容器(laravel)连接到其他容器(wordpress)

时间:2017-05-10 09:53:09

标签: docker

我有两个使用docker的项目,第一个项目是laravel,第二个是wordpress。在laravel我想连接两个数据库(将laravel数据库转换为wordpress数据库)。

但我不知道如何连接它: 这是两个docker-compose.yml文件:

in laravel:

version: '2'
services:

# The Application
app:
  build:
  context: ./
  dockerfile: app.dockerfile
working_dir: /var/www
volumes:
  - ./:/var/www
environment:
  - "DB_PORT=3306"
  - "DB_HOST=database"

# The Web Server
web:
  build:
    context: ./
    dockerfile: web.dockerfile
    working_dir: /var/www
    volumes_from:
       - app
    ports:
      - 8081:80

 # The Database
  database:
    image: mysql:5.6
    volumes:
     - dbdata:/var/lib/mysql
    environment:
      - "MYSQL_DATABASE=homestead"
      - "MYSQL_USER=homestead"
      - "MYSQL_PASSWORD=secret"
      - "MYSQL_ROOT_PASSWORD=secret"
    ports:
       - "33061:3306"
 volumes:
   dbdata:

和我在wordpress中的docker-compose.yml文件:

version: '2'

services:
  db:
    image: mysql:5.7
    volumes:
      - db_data:/var/lib/mysql
    restart: always
   environment:
      MYSQL_ROOT_PASSWORD: 123456
      MYSQL_DATABASE: wpshop
      MYSQL_USER: root
      MYSQL_PASSWORD: 123456

  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    volumes:
       - ./:/var/www/html
    ports:
      - "8080:80"
    restart: always
    environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: root
       WORDPRESS_DB_PASSWORD: 123456
   volumes:
      db_data:

我cd到每个项目并运行docker-compose up -d

请帮助我!

1 个答案:

答案 0 :(得分:1)

创建外部网络,并将该网络用作所有容器的默认网络。这样,您就可以通过其名称访问所有容器。

看看Docker容器网络:https://docs.docker.com/engine/userguide/networking/