30

学习 Docker(四):Docker Compose

 4 years ago
source link: http://muziyuchen.com/docker-4/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Docker Compose 是 Docker 自带的命令行工具用于定义和运行多个容器。

docker-compose.yaml

docker-compose.yaml 是 Docker Compose 配置文件。

version: '3'  
services:  
  mysql:
    image: mysql:5.7
    container_name: mysql
    hostname: mysql
    environment:
      MYSQL_ROOT_PASSWORD: secret
    volumes:
      - ./docker/init.sql:/data/application/init.sql
    ports:
      - "3306:3306"
  web:
    build:
      dockerfile: Dockerfile
      context: .
    image: web
    depends_on:
      - mysql
    container_name: web
    environment:
      JDBC_MYSQL_URL: jdbc:mysql://mysql:3306/web
      JDBC_MYSQL_USERNAME: root
      JDBC_MYSQL_PASSWORD: secret
    ports:
      - "80:80"

:point_up_2:的 docker-compose.yaml 配置文件,定义了两个服务 mysql 和 web。

services.web.depends_on 定义了 web 依赖于 mysql;

services.web.build 定义了如何构建 web 镜像;

services.mysql.imageservices.web.image 定义了容器镜像;

services.mysql.environmentservices.web.environment 定义了环境变量;

services.mysql.portsservices.web.ports 定义了端口;

services.mysql.volumes 挂载本地文件到镜像,这里挂载了 mysql 初始化脚本。

使用

构建 web 镜像:

docker-compose build web

启动服务:

docker-compose -d up

停止服务:

docker-compose down

参考


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK