docker-compose安装部署

安装compse请先 安装docker

一、docker-compose安装

1.1 Compose的介绍

docker-compose:是一个用于定义和运行多容器 Docker 的应用程序工具,可以帮助我们可以轻松、高效的管
理容器

1.2 Compose和Docker兼容性

compose文件格式版本

docker版本

3.4

17.09.0+

3.3

17.06.0+

3.2

17.04.0+

3.1

1.13.1+

3.0

1.13.0+

2.3

17.06.0+

2.2

1.13.0+

2.1

1.12.0+

2.0

1.10.0+

1.0

1.9.1.+

1.3 compose的安装

  • 第一种安装方式:
yum install -y py-pip, python-dev, libffi-dev, openssl-dev, gcc, libc-dev, make
sudo curl -L "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

参考:https://docs.docker.com/compose/install/

  • 第二种安装方式
    1)下载docker-compose包并上传到linux下: 2)mv docker-compose-Linux-x86_64 /usr/local/bin/docker-compose
    3)chomd -R 777 /usr/local/bin/docker-compose
  • 第三种安装方式:

安装pip工具

yum install -y epel-release
yum install -y python-pip

安装pip报错解决:

[root@localhost yum.repos.d]# yum install python-pip -y
已加载插件:fastestmirror
One of the configured repositories failed (未知),
and yum doesn't have enough cached data to continue. At this point the only
safe thing yum can do is fail. There are a few ways to work "fix" this:
 - Contact the upstream for the repository and get them to fix the problem.
 - Reconfigure the baseurl/etc. for the repository, to point to a working
upstream. This is most often useful if you are using a newer
distribution release than is supported by the repository (and the
packages for the previous distribution release still work).
 - Disable the repository, so yum won't use it by default. Yum will then
just ignore the repository until you permanently enable it again or use
--enablerepo for temporary usage:
yum-config-manager --disable <repoid>
 - Configure the failing repository to be skipped, if it is unavailable.
Note that yum will try to contact the repo. when it runs most commands,
so will have to try and fail each time (and thus. yum will be be much
slower). If it is a very temporary problem though, this is often a nice
compromise:
yum-config-manager --save --setopt=<repoid>.skip_if_unavailable=true
Cannot retrieve metalink for repository: epel/x86_64. Please verify its path and
try again

解决: vi /etc/yum.repos.d/epel.repo 修改配置文件,注释掉metalink ,取消注释 baseurl

修改前如图:

docker compose的yum源 docker-compose -f_nginx


改后如下图:

docker compose的yum源 docker-compose -f_docker compose的yum源_02

  • 安装docker-compose
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple docker-compose==1.26.1
如果安装报错:
------------------------------------------------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-ZCtlD9/bcrypt/
You are using pip version 8.1.2, however version 20.2.4 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
-------------------------------------------------------------------
执行:pip install --upgrade pip
然后重新install docker-compose
  • 查看docker-compose版本:
    docker-compose version

二、docker-compose常用命令

命令

解释

docker-compose up -d nginx

构建建启动nignx容器

docker-compose exec nginx bash

登录到nginx容器中

docker-compose down

删除所有nginx容器,镜像

docker-compose ps

显示所有容器

docker-compose restart nginx

重新启动nginx容器

docker-compose run --no-deps --rm php-fpm php -v

在php-fpm中不启动关联容器,并容器执行php -v 执行完成后删除容器

docker-compose build nginx

构建镜像 。

docker-compose build --no-cache nginx

不带缓存的构建。

docker-compose logs nginx

查看nginx的日志

docker-compose logs -f nginx

查看nginx的实时日志

docker-compose config -q

验证(docker-compose.yml)文件配置,当配置正确时,不输出任何内容,当文件配置错误,输出错误信息。

docker-compose events --json nginx

以json的形式输出nginx的docker日志

docker-compose pause nginx

暂停nignx容器

docker-compose unpause nginx

恢复ningx容器

docker-compose rm nginx

删除容器(删除前必须关闭容器)

docker-compose stop nginx

停止nignx容器

docker-compose start nginx

启动nignx容器

三、docker-compose.yml命令详解

version: '3.3'
services:
db:
image: mysql:5.7 #docker run -itd mysql:5.7
volumes:
- db_data:/var/lib/mysql #采用的是卷标的形式挂载(注意:- db_data是参数,可以变,
自定义,必须与下面对应)
restart: always #自动重启,保证服务在线
environment:
MYSQL_ROOT_PASSWORD: somewordpress #指定环境变量 docker -itd -e
MYSQL_ROOT_PASSWORD= somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db # - db 是参数,合起来的意思是只有当上面的mysql数据库安装成功后,这个wordpress才可以
被安装,还有一个功能,就是docker --link 将上面的mysql数据库,与这个wordpress应用连起来
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: {}
  • version:指定 docker-compose.yml 文件的写法格式
  • services:多个容器集合environment:环境变量配置,可以用数组或字典两种方式
environment:
    RACK_ENV: "development"
    SHOW: "ture"
  • image:指定服务所使用的镜像
version: '2'
services:
  redis:
    image: redis:alpine
  • expose:定义容器用到的端口(一般用来标识镜像使用的端口,方便用ports映射)
expose:
    - "3000"
    - "8000"
  • ports:定义宿主机端口和容器端口的映射,可使用宿主机IP+宿主机端口进行访问 宿主机端口:容器端口
  • volumes:卷挂载路径,定义宿主机的目录/文件和容器的目录/文件的映射 宿主机路径:容器路径
volumes:
  - "/var/lib/mysql"
  - "hostPath:containerPath"
  - "root/configs:/etc/configs"
  • depend_on: 规定service加载顺序,例如数据库服务需要在后台服务前运行
  • restart: always :配置重启,docker每次启动时会启动该服务
restart: always
  • privileged: true :开启特权模式
  • user: root :指定容器运行的用户名
  • links:将指定容器连接到当前连接,可以设置别名,已废弃,推荐使用networks
  • logging:日志服务
  • driver:默认json-file,可选syslog
logging:
  driver: syslog
  options:
    syslog-address: "tcp://192.168.0.42:123"
  • network_mode:设置网络模式
network_mode: "bridge"
network_mode: "host"
network_mode: "none"
network_mode: "service:[service name]"
network_mode: "container:[container name/id]"
  • bridge:默认,需要单独配置ports映射主机port和服务的port,并且开启了容器间通信
  • host:和宿主机共享网络,比如service是8081端口,无需配置ports,直接可以用主机IP:8081访问
  • cap_add cap_drop:赋予/删除 容器某些能力
  • build:配置构建时,Compose 会利用它自动构建镜像,该值可以是一个路径,也可以是一个对象,用于指定 Dockerfile 路径