Docker Compose-Docker

导读:本篇文章讲解 Docker Compose-Docker,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

概述

之前使用Dockerfile构建镜像时,编写好Dockerfile文件以及其他必要文件后,需要使用docker build、docker run等命令操作。
在微服务架构体系中,一个应用系统一般包含若干个服务器,每个微服务还会被部署多个实例,如果每个服务都要手动启停,效率太低了,维护很不方便。
因此,便有了docker compose,它可以轻松、高效的管理容器,是一个用于定义和运行多个容器docker的应用程序工具。

Docker Compose安装

方法一,安装到/usr/local/bin下面

# 下载docker-compose
curl -SL -k https://get.daocloud.io/docker/compose/releases/download/1.25.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
# 文件添加执行权限
chmod +x /usr/local/bin/docker-compose
# 添加软连接, 可以在任何地方访问
ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
# 检测按照是否成功
docker-compose --version

方法二,作为插件安装

下载docker compose的软件包,并设置执行权限。

$ mkdir -p ~/.docker/cli-plugins/
$ curl -SL https://github.com/docker/compose/releases/download/v2.15.1/docker-compose-linux-x86_64 -o /usr/local/lib/docker/cli-plugins
$ chmod +x /usr/local/lib/docker/cli-plugins

需要把/usr/local/lib/docker/cli-plugins添加到环境变量中(PATH)
查看是否安装成功

[root@localhost ~]# docker-compose version
Docker Compose version v2.15.1
[root@localhost ~]#

Docker-compose 容器编排

YUML语言结束

YUML是一种 标记语言很直观的数据序列化格式,文件格式及编写注意事项。

  • 不支持制表符tab键缩进,需要使用空格缩进
  • 通常开头缩进两个空格
  • 字符后缩进1个空格,如冒号、逗号、横杆
  • 用#号注释
  • 如果包含特殊字符用单引号引起来
  • 布尔值必须用引号括起来

Docker Compose配置常用字段

在这里插入图片描述

Docker Compose常用命令

docker-compose -h # 查看帮助
docker-compose up # 创建并运行所有容器
docker-compose up -d # 创建并后台运行所有容器
docker-compose ps # 显示所用容器信息
docker-compose -f docker-compose.yml up -d # 指定模板
docker-compose down # 停止并删除容器、网络、卷、镜像。
docker-compose rm # 删除容器
docker-compose logs # 查看容器输出日志
docker-compose pull # 拉取依赖镜像
dokcer-compose config # 检查配置
dokcer-compose config -q # 验证yaml配置文件是否正确,有问题才有输出
docker-compose pause # 暂停容器
docker-compose unpause # 恢复暂停
docker-compose restart # 重启服务
docker-compose start # 启动服务
docker-compose stop # 停止服务

docker compose 命令说明

  • 基本使用格式
    docker-compose [options] [command] [ARGS…]
  • docker-compose选项
    –verbose:输出更多调试信息
    –version:打印版本并退出
    -f,–file:FILE使用特定的compose模板文件,默认为docker-compose.yml
    -p,–project-name:Name指定项目名称,默认使用目录名称

编排容器

linux服务器上编排容器

  1. 先打包本地一个项目包 httpUtils-0.0.1-SNAPSHOT.jar 到服务器上,放在/home/images下面
  2. 在/home/images下面编写一个Dockerfile文件,用来构建镜像
    Dockerfile内容:
FROM openjdk:8-jdk-alpine
MAINTAINER docker
WORKDIR /
# 把target下的httpUtils-0.0.1-SNAPSHOT.jar拷贝的docker的/目录下
ADD httpUtils-0.0.1-SNAPSHOT.jar /httpUtils-0.0.1-SNAPSHOT.jar
# 对外暴露的端口
EXPOSE 8097
ENTRYPOINT ["java","-jar","/httpUtils-0.0.1-SNAPSHOT.jar"]
  1. 在/home下创建一个docker-compose.yml文件。
    文件里展示两种方式的编排。
    第一种是已存的镜像,直接构建容器并启动。
    第二种是不存在的镜像,需要通过DockerFile先build镜像再构建容器启动。
    compose的编排文件内容:
version: '3' # compose 文件版本(1.2.x+用3即可)
services:
  httputils-server:
    container_name: httputils-docker-test
    build:
      context: ./images
      dockerfile: Dockerfile
    ports:
      - '8099:8080'

  redis-server-compose:
    image: redis:latest
    ports:
      - '63790:6379'
  1. 执行编排文件,启动容器
    通常使用docker-compose -f docker-compose.yml up -d 进行后台启动,
    这里我们使用docker-compose -f docker-compose.yml up 查询命令的执行过程。
[root@localhost home]# docker-compose -f docker-compose.yml up
[+] Running 7/7
 ⠿ redis-server-compose Pulled                                                                                                                                                         114.4s
   ⠿ e5ae68f74026 Pull complete                                                                                                                                                         77.9s
   ⠿ 37c4354629da Pull complete                                                                                                                                                         78.0s
   ⠿ b065b1b1fa0f Pull complete                                                                                                                                                         78.1s
   ⠿ 6954d19bb2e5 Pull complete                                                                                                                                                         78.4s
   ⠿ 6333f8baaf7c Pull complete                                                                                                                                                         78.4s
   ⠿ f9772c8a44e7 Pull complete                                                                                                                                                         78.5s
Sending build context to Docker daemon  17.91MB
Step 1/6 : FROM openjdk:8-jdk-alpine
 ---> a3562aa0b991
Step 2/6 : MAINTAINER docker
 ---> Using cache
 ---> f64a3b9bd3dd
Step 3/6 : WORKDIR /
 ---> Using cache
 ---> b532b7229908
Step 4/6 : ADD httpUtils-0.0.1-SNAPSHOT.jar /httpUtils-0.0.1-SNAPSHOT.jar
 ---> 79f482f02a9f
Step 5/6 : EXPOSE 8097
 ---> Running in 6a3233d8a991
 ---> 96f0d02958b3
Step 6/6 : ENTRYPOINT ["java","-jar","/httpUtils-0.0.1-SNAPSHOT.jar"]
 ---> Running in 19dbb7fce54c
 ---> 38ae18b60da5
Successfully built 38ae18b60da5
Successfully tagged home_httputils-server:latest

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
[+] Running 3/3
 ⠿ Network home_default                   Created                                                                                                                                        0.1s
 ⠿ Container httputils-docker-test        Created                                                                                                                                        0.0s
 ⠿ Container home-redis-server-compose-1  Created                                                                                                                                        0.0s
Attaching to home-redis-server-compose-1, httputils-docker-test
home-redis-server-compose-1  | 1:C 10 Dec 2021 06:40:41.155 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
home-redis-server-compose-1  | 1:C 10 Dec 2021 06:40:41.155 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=1, just started
home-redis-server-compose-1  | 1:C 10 Dec 2021 06:40:41.155 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
home-redis-server-compose-1  | 1:M 10 Dec 2021 06:40:41.156 * monotonic clock: POSIX clock_gettime
home-redis-server-compose-1  | 1:M 10 Dec 2021 06:40:41.156 * Running mode=standalone, port=6379.
home-redis-server-compose-1  | 1:M 10 Dec 2021 06:40:41.156 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
home-redis-server-compose-1  | 1:M 10 Dec 2021 06:40:41.156 # Server initialized
home-redis-server-compose-1  | 1:M 10 Dec 2021 06:40:41.156 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
home-redis-server-compose-1  | 1:M 10 Dec 2021 06:40:41.156 * Ready to accept connections
httputils-docker-test        | 
httputils-docker-test        |   .   ____          _            __ _ _
httputils-docker-test        |  /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
httputils-docker-test        | ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
httputils-docker-test        |  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
httputils-docker-test        |   '  |____| .__|_| |_|_| |_\__, | / / / /
httputils-docker-test        |  =========|_|==============|___/=/_/_/_/
httputils-docker-test        |  :: Spring Boot ::                (v2.5.4)
httputils-docker-test        | 
httputils-docker-test        | 2021-12-10 06:40:42.060  INFO 1 --- [           main] c.p.httputils.HttpUtilsApplication       : Starting HttpUtilsApplication v0.0.1-SNAPSHOT using Java 1.8.0_212 on adc2020f4046 with PID 1 (/httpUtils-0.0.1-SNAPSHOT.jar started by root in /)
httputils-docker-test        | 2021-12-10 06:40:42.063  INFO 1 --- [           main] c.p.httputils.HttpUtilsApplication       : No active profile set, falling back to default profiles: default
httputils-docker-test        | 2021-12-10 06:40:43.698  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
httputils-docker-test        | 2021-12-10 06:40:43.716  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
httputils-docker-test        | 2021-12-10 06:40:43.716  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.52]
httputils-docker-test        | 2021-12-10 06:40:43.783  INFO 1 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
httputils-docker-test        | 2021-12-10 06:40:43.783  INFO 1 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1637 ms
httputils-docker-test        | 2021-12-10 06:40:44.175  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
httputils-docker-test        | 2021-12-10 06:40:44.186  INFO 1 --- [           main] c.p.httputils.HttpUtilsApplication       : Started HttpUtilsApplication in 2.566 seconds (JVM running for 3.055)
httputils-docker-test        | 2021-12-10 06:41:08.780  INFO 1 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
httputils-docker-test        | 2021-12-10 06:41:08.781  INFO 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
httputils-docker-test        | 2021-12-10 06:41:08.783  INFO 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 2 ms
httputils-docker-test        | docker test
  1. 查看执行情况
[root@localhost home]# docker-compose ps
NAME                          COMMAND                  SERVICE                STATUS              PORTS
home-redis-server-compose-1   "docker-entrypoint.s…"   redis-server-compose   running             0.0.0.0:63790->6379/tcp, :::63790->6379/tcp
httputils-docker-test         "java -jar /httpUtil…"   httputils-server       running             0.0.0.0:8099->8080/tcp, :::8099->8080/tcp
[root@localhost home]# docker images
REPOSITORY              TAG            IMAGE ID       CREATED         SIZE
home_httputils-server   latest         38ae18b60da5   9 minutes ago   125MB
httputils-docker        latest         f53bf4ae017a   26 hours ago    125MB
httputils-docker        test           f53bf4ae017a   26 hours ago    125MB
<none>                  <none>         69b5ea54db4f   27 hours ago    125MB
redis                   latest         aea9b698d7d1   7 days ago      113MB
hello-world             latest         feb5d9fea6a5   2 months ago    13.3kB
portainer/portainer     latest         580c0e4e98b0   8 months ago    79.1MB
openjdk                 8-jdk-alpine   a3562aa0b991   2 years ago     105MB
[root@localhost home]# docker container ps
CONTAINER ID   IMAGE                   COMMAND                  CREATED              STATUS              PORTS                                                 NAMES
31304106770a   redis:latest            "docker-entrypoint.s…"   About a minute ago   Up About a minute   0.0.0.0:63790->6379/tcp, :::63790->6379/tcp           home-redis-server-compose-1
adc2020f4046   home_httputils-server   "java -jar /httpUtil…"   10 minutes ago       Up About a minute   8097/tcp, 0.0.0.0:8099->8080/tcp, :::8099->8080/tcp   httputils-docker-test
[root@localhost home]# 

docker-compose ports和expose的区别

docker-compose中有两种方式可以暴露容器的端口:ports和expose。

ports

ports暴露容器端口到主机的任意端口或指定端口,用法:

ports:
  - "80:80"         # 绑定容器的80端口到主机的80端口
  - "9000:80"       # 绑定容器的80端口到主机的9000端口
  - "443"           # 绑定容器的443端口到主机的任意端口,容器启动时随机分配绑定的主机端口号

不管是否指定主机端口,使用ports都会将端口暴露给主机和其他容器。

expose

expose暴露容器给link到当前容器的容器,或者暴露给同一个networks的容器,用法:

expose:
  - "3000"
  - "8000"

以上指令将当前容器的端口3000和8000暴露给其他容器。
和ports的区别是,expose不会将端口暴露给主机,主机无法访问expose的端口。

idea整合代码编排容器

  1. 准备
    一方面我们需要docker安装环境,也需要安装docker compose工具;
    下载docker并安装,地址为
    https://desktop.docker.com/win/main/amd64/Docker%20Desktop%20Installer.exe
    配置:
    在这里插入图片描述
  2. 创建docker-compose.yml文件
    在springboot项目的根目录pom.xml文件所在目录下,一般docker-compose.yml与父工程的pom.xml是同级的,使用其对子工程进行构建镜像和容器编排
    docker-compose.yml:
version: '3' # compose 文件版本(1.2.x+用3即可)
services:
  httputils-server:
    container_name: httputils-docker-test
    build:
      context: ./httpUtils
      dockerfile: Dockerfile
    ports:
      - '8099:8080'

  redis-server-compose:
    image: redis:latest
    ports:
      - '63790:6379'

创建一个docker-compose configrations执行就ok了
在这里插入图片描述
第一个容器:通过dockerfile文件先构建镜像再创建启动容器。
第二个容器:直接采用服务器上的镜像构建的容器。

参考

docker-compose.yml 配置文件编写详解

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/100201.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!