如下定義摘自docker官網:https://docs.docker.com/compose/overview/php
Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration. html
譯文:python
Compose是一個用來定義、運行多容器Docker應用的工具。經過compose,你使用YAML文件來定義你的應用服務。而後,經過一個簡單的命令,就能建立和啓動你配置的全部服務。mysql
從定義能夠看出,Compose是一個工具,用來定義多個容器服務,而後經過一個命令一鍵啓停全部服務,方便一次性管理多個有關聯關係的容器。nginx
若是說docker run、docker exec 是shell命令的話,那麼compose就是shell腳本!只不過他能夠經過YAML配置文件的形式簡寫命令。sql
Using Compose is basically a three-step process:docker
Define your app’s environment with a Dockerfile
so it can be reproduced anywhere.shell
Define the services that make up your app in docker-compose.yml
so they can be run together in an isolated environment.app
Run docker-compose up
and Compose starts and runs your entire app.wordpress
譯文:
Compose使用基本三步驟:
官網學習連接:
https://docs.docker.com/compose/
compose file version 3 語法介紹:
https://docs.docker.com/compose/compose-file/
compose命令行介紹:
https://docs.docker.com/compose/reference/
按照上面所說的步驟:
一、編寫服務的Dockerfile
Nginx、php的Dockerfile已經在前面實踐過了,直接拿來使用便可。
二、定義docker-compose.yml
version: '3'
services:
nginx:
hostname: nginx
build:
context: ./nginx
dockerfile: Dockerfile
ports:
- 81:80
networks:
- lnmp
volumes:
- ./wwwroot:/usr/local/nginx/html
php:
hostname: php
build:
context: ./php
dockerfile: Dockerfile
networks:
- lnmp
volumes:
- ./wwwroot:/usr/local/nginx/html
mysql:
hostname: mysql
image: mysql:5.6
ports:
- 3306:3306
networks:
- lnmp
volumes:
- ./mysql/conf:/etc/mysql/conf.d
- ./mysql/data:/var/lib/mysql
command: --character-set-server=utf8
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_DATABASE: wordpress
MYSQL_USER: user
MYSQL_PASSWORD: user123
networks:
lnmp:
三、啓動docker-compose
docker-compose -f docker-compose.yml up -d
注意:-d是在後臺啓動,且要在docker-compose.yml文件目錄下啓動。
四、查看結果
訪問url:http://120.92.*.*:81/wordpress