如何使用docker-compose.yml为docker中运行的应用程序指定docker容器数据库?

时间:2017-11-29 19:08:44

标签: mysql docker docker-compose mediawiki

上下文

有这个docker-compose.yml:

version: '3'
services:
  mediawiki:
    image: mediawiki
    restart: always
    ports:
      - 8080:80
    links:
      - database
    volumes:
      - /var/www/html/images
      # After initial setup, download LocalSettings.php to the same directory as
      # this yaml and uncomment the following line and use compose to restart
      # the mediawiki service
      # - ./LocalSettings.php:/var/www/html/LocalSettings.php
  database:
    image: mariadb
    restart: always
    environment:
      # @see https://phabricator.wikimedia.org/source/mediawiki/browse/master/includes/DefaultSettings.php
      MYSQL_DATABASE: my_wiki
      MYSQL_USER: wikiuser
      MYSQL_PASSWORD: example
      MYSQL_RANDOM_ROOT_PASSWORD: yes

当我运行docker ps时,我得到:

89db8794029a  mysql:latest "docker-entrypoint..."  ...  0.0.0.0:8083->3306/tcp   some-mysql

这是一个运行的mysql docker容器。

问题

如何以数据库指向mysql docker容器(89db8794029a)的方式修改docker-compose.yml

1 个答案:

答案 0 :(得分:1)

您不必在yml文件上添加数据库服务。

为了将mediawiki服务连接到some-mysql容器,mediawiki容器需要与some-mysql容器在同一网络上

假设mediawiki已经启动

首先,你需要知道some-mysql使用什么网络,

docker network ls

我猜这将是'some-mysql_default'

将媒体wiki连接到some-mysql

docker network connect some-mysql_default mediawiki

现在,在mediawiki config

中使用'some-mysql'作为主机名数据库

OR

yml文件自动连接到mysql网络

version: '3'
services:
  mediawiki:
    image: mediawiki
    restart: always
    ports:
      - 8080:80
    links:
      - database
    volumes:
      - /var/www/html/images
      # After initial setup, download LocalSettings.php to the same directory as
      # this yaml and uncomment the following line and use compose to restart
      # the mediawiki service
      # - ./LocalSettings.php:/var/www/html/LocalSettings.php
    networks:
      - default
      - some-mysql_default
networks:
  default: # this network
    driver: bridge
  some-mysql_default: # external network
    external: true