docker 编排工具使用docker-compose

安装docker-compose

yum install -y epel-release
yum install -y python-pip
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple docker-compose==1.24.1

# 如果python-pip报错
vim /etc/yum.repos.d/epel.repo 修改配置文件,注释掉metalink ,取消注释 baseurl

操作命令

compose操作容器(一定要进入配置文件目录)

后台启动容器:docker-compose up -d
查看容器运行情况:docker-compose ps
停止并删除容器:docker-compose down
停止并删除容器并删除volume:docker-compose down --volumes
停止启动容器:docker-compose stop;docker-compose start
docker-compose exec的使用:docker-compose exec redis bash

总结:
操作docker-compose一定要在配置文件docker-compose.yml文件路径下操作
格式一定要注意,该空格要空格

配置文件

docker-compose.yml

version: '3'
services:
  nginx:
    image: mycentos:nginx
    ports:
    - "8080:80"
    volumes:
    - /home:/usr/local/nginx/html
    - /var/logs/nginx/logs:/usr/local/nginx/logs
    command: /usr/local/nginx/sbin/nginx -g "deamon off;"
    
  redis:
    image: mycentos:redis
    ports:
    - "6380:6379"

如果改成主机模式,去掉ports, 加上 network_mode: "host",默认为桥接

实战: 模拟搭建个人博客

wordpress 免费的博客平台

docker-compose.yml

version: '3.3'
services:
  db:
    image: mysql:5.7
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      # 指定环境变量 docker -itd -e MYSQL_ROOT_PASSWORD= somewordpress
      MYSQL_ROOT_PASSWORD: somewordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
    
  wordpress:
    depends_on: # 1.先启动上面的db(依赖),才能被安装 2.docker link
      - db
    image: wordpress:latest
    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: {}
  # 对应最上面的 volumes:

查找卷标

docker volume ls
docker volume inspect <volume-id>

Mountpoint 宿主机路径
对应的是  /var/lib/mysql
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 撸撸猫 设计师:C马雯娟 返回首页