DockerCompose

官方介绍:定义、运行多个容器。YAML file配置文件。所有环境都可以使用。

使用步骤

1.编写Dockerfile
2.编写docker-compose.yml
3.运行docker-compose up

安装compose

官网地址:https://docs.docker.com/compo...

sudo curl -L "https://get.daocloud.io/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

sudo chmod +x /usr/local/bin/docker-compose

docker-compose version

快速开始

官方文档:https://docs.docker.com/compo...

mkdir composetest
cd composetest

vim app.py
vim requirements.txt
vim Dockerfile
vim docker-compose.yml

docker-compose up

测试

curl localhost:5000

停止

docker-compose down

扩展

docker images
CTRL + C

默认服务名:文件名_服务名_num

docker network ls

项目中的内容都在同个网络下,通过域名访问(如localhost=redis)

YAML配置编写规则

官方文档:https://docs.docker.com/compo...

#1.版本
version: '' 
#2.服务
services: 
  服务1: web
    #服务配置(build,ports,volumes...)
  服务2: redis,db...
#3.其他配置(网络,卷挂载,全局配置...)  
volumes:
network:
config:  

Compose一键部署WordPress

官方文档:https://docs.docker.com/sampl...

vim docker-compose.yml

version: "3.9"
    
services:
  db:
    image: mysql:5.7
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: somewordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
    
  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    volumes:
      - wordpress_data:/var/www/html
    ports:
      - "8000:80"
    restart: always
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_NAME: wordpress
volumes:
  db_data: {}
  wordpress_data: {}

docker-compose up -d

-d 后台启动

IT小马
1.2k 声望166 粉丝

Php - Go - Vue - 云原生