审核中~
溜忙之道
首页
溜忙手册
offer之道
项目Git
写文章
登录
·
注册
登录
没有账号,去注册
注册
已有账号,去登录
博客
Docker:Docker Compose 详解
南易_站长
阅读量:7809
关注
Docker:Docker Compose 详解
前面我们使用 Docker 的时候,定义 Dockerfile 文件,然后使用 docker build、docker run 等命令操作容器。然而微服务架构的应用系统一般包含若干个微服务,每个微服务一般都会部署多个实例,如果每个微服务都要手动启停,那么效率之低,维护量之大可想而知 使用 Docker Compose 可以轻松、高效的管理容器,它是一个用于定义和运行多容器 Docker 的应用程序工具 安装 Docker Compose 安装 Docker Compose 可以通过下面命令自动下载适应版本的 Compose,并为安装脚本添加执行权限 ``` sudo curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose sudo chmod +x /usr/local/bin/docker-compose 查看安装是否成功 docker-compose -v 快速入门 打包项目,获得 jar 包 docker-demo-0.0.1-SNAPSHOT.jar mvn clean package 在 jar 包所在路径创建 Dockerfile 文件,添加以下内容 FROM java:8 VOLUME /tmp ADD docker-demo-0.0.1-SNAPSHOT.jar app.jar RUN bash -c 'touch /app.jar' EXPOSE 9000 ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","app.jar"] 在 jar 包所在路径创建文件 docker-compose.yml,添加以下内容 version: '2' # 表示该 Docker-Compose 文件使用的是 Version 2 file services: docker-demo: # 指定服务名称 build: . # 指定 Dockerfile 所在路径 ports: # 指定端口映射 - "9000:8761" 在 docker-compose.yml 所在路径下执行该命令 Compose 就会自动构建镜像并使用镜像启动容器 docker-compose up docker-compose up -d // 后台启动并运行容器 访问 http://localhost:9000/hello 即可访问微服务接口 工程、服务、容器 Docker Compose 将所管理的容器分为三层,分别是工程(project)、服务(service)、容器(container) Docker Compose 运行目录下的所有文件(docker-compose.yml)组成一个工程,一个工程包含多个服务,每个服务中定义了容器运行的镜像、参数、依赖,一个服务可包括多个容器实例 Docker Compose 常用命令与配置 常见命令 ps:列出所有运行容器 docker-compose ps logs:查看服务日志输出 docker-compose logs port:打印绑定的公共端口,下面命令可以输出 eureka 服务 8761 端口所绑定的公共端口 docker-compose port eureka 8761 build:构建或者重新构建服务 docker-compose build start:启动指定服务已存在的容器 docker-compose start eureka stop:停止已运行的服务的容器 docker-compose stop eureka rm:删除指定服务的容器 docker-compose rm eureka up:构建、启动容器 docker-compose up kill:通过发送 SIGKILL 信号来停止指定服务的容器 docker-compose kill eureka pull:下载服务镜像 scale:设置指定服务运气容器的个数,以 service=num 形式指定 docker-compose scale user=3 movie=3 run:在一个服务上执行一个命令 docker-compose run web bash docker-compose.yml 属性 version:指定 docker-compose.yml 文件的写法格式 services:多个容器集合 build:配置构建时,Compose 会利用它自动构建镜像,该值可以是一个路径,也可以是一个对象,用于指定 Dockerfile 参数 build: ./dir --------------- build: context: ./dir dockerfile: Dockerfile args: buildno: 1 command:覆盖容器启动后默认执行的命令 command: bundle exec thin -p 3000 ---------------------------------- command: [bundle,exec,thin,-p,3000] dns:配置 dns 服务器,可以是一个值或列表 dns: 8.8.8.8 ------------ dns: - 8.8.8.8 - 9.9.9.9 dns_search:配置 DNS 搜索域,可以是一个值或列表 dns_search: example.com ------------------------ dns_search: - dc1.example.com - dc2.example.com environment:环境变量配置,可以用数组或字典两种方式 environment: RACK_ENV: development SHOW: 'ture' ------------------------- environment: - RACK_ENV=development - SHOW=ture env_file:从文件中获取环境变量,可以指定一个文件路径或路径列表,其优先级低于 environment 指定的环境变量 env_file: .env --------------- env_file: - ./common.env expose:暴露端口,只将端口暴露给连接的服务,而不暴露给主机 expose: - "3000" - "8000" image:指定服务所使用的镜像 image: java network_mode:设置网络模式 network_mode: "bridge" network_mode: "host" network_mode: "none" network_mode: "service:[service name]" network_mode: "container:[container name/id]" ports:对外暴露的端口定义,和 expose 对应 ports: # 暴露端口信息 - "宿主机端口:容器暴露端口" - "8763:8763" - "8763:8763" links:将指定容器连接到当前连接,可以设置别名,避免ip方式导致的容器重启动态改变的无法连接情况 links: # 指定服务名称:别名 - docker-compose-eureka-server:compose-eureka volumes:卷挂载路径 volumes: - /lib - /var logs:日志输出信息 --no-color 单色输出,不显示其他颜. -f, --follow 跟踪日志输出,就是可以实时查看日志 -t, --timestamps 显示时间戳 --tail 从日志的结尾显示,--tail=200 Docker Compose 其它 更新容器 当服务的配置发生更改时,可使用 docker-compose up 命令更新配置 此时,Compose 会删除旧容器并创建新容器,新容器会以不同的 IP 地址加入网络,名称保持不变,任何指向旧容起的连接都会被关闭,重新找到新容器并连接上去 links 服务之间可以使用服务名称相互访问,links 允许定义一个别名,从而使用该别名访问其它服务 version: '2' services: web: build: . links: - "db:database" db: image: postgres ``` 这样 Web 服务就可以使用 db 或 database 作为 hostname 访问 db 服务了
评论
表情
发送
0
评论