docker容器服务器编排利器Docker-Compose

本文详细介绍了DockerCompose在容器服务器编排中的重要性、定义、使用步骤,包括其在定义复杂应用、服务配置、安装与实战中的应用。涵盖了从基础概念到实战演示的完整流程。
摘要由CSDN通过智能技术生成

在这里插入图片描述


一、Docker Compose必要性以及定义

Docker Compose是一个用来定义和运行复杂应用的Docker工具。
一个使用Docker容器的应用,通常由多个容器组成。使用Docker Compose不再需要使用shell脚本来启动容器
Compose 通过一个配置文件来管理多个Docker容器,在配置文件中,所有的容器通过services来定义,然后使用docker-compose脚本来启动,停止和重启应用,和应用中的服务以及所有依赖服务的容器,非常适合组合使用多个容器进行开发的场景。

1、Compose 使用的三个步骤:

1、使用 Dockerfile 定义应用程序的环境
2、使用 docker-compose.yml 定义构成应用程序的服务,这样它们可以在隔离环境中一起运行
3、最后,执行 docker-compose up 命令来启动并运行整个应用程序

2、Docker Compose可以做到以下几点:

①提供工具用于定义和运行多个docker容器应用;
②使用yaml文件来配置应用服务(docker-compse.yml);
③可以通过一个简单的命令docker-compse up可以按照依赖关系启动所有服务;
④可以通过一个简单的命令docker-compose down停止所有服务;
⑤当一个服务需要的时候,可以很简单地通过–scale进行扩容;

用容器运行一个服务,需要使用docker run命令。但如果我要运行多个服务呢?
假设我要运行一个web服务,还要运行一个db服务,那么是用一个容器运行,还是用多个容器运行呢?
一个容器运行多个服务会造成镜像的复杂度提高,docker倾向于一个容器运行一个应用。
那么复杂的架构就会需要很多的容器,并且需要它们之间有关联(容器之间的依赖和连接)就更复杂了
这个复杂的问题需要解决,这就涉及到了容器编排的问题了。

3、Compose

  • 编排

    • 是对多个容器进行启动和管理的方法

    • 例如:LNMT,先启动MySQL,再启动Tomcat,最后启动Nginx

4、服务架构的演进

  • 单体服务架构
  • 分布式服务架构
  • 微服务架构(容器)
  • 超微服务架构

5、容器编排工具

  • docker machine

    • 在虚拟机中部署docker容器引擎的工具
  • docker compose

    • 是一个用于定义和运行多容器Docker的应用程序工具
  • docker swarm

    • 需要在多台主机中运行容器的时候
  • mesos+marathon

    • mesos对计算机计算资源进行管理和调度

    • marathon服务发现及负载均衡的功能

  • kubernetes

    • google开源的容器编排工具

二、Docker Compose 应用参考资料

https://docs.docker.com/compose/

三、Docker-Compose应用最佳实战步骤

1、概念

工程(project)
服务(Service)
容器(Container)

2、步骤

1.定义应用的Dockerfile文件,为了任何地方进行构建。
2.使用docker-compose.yaml定义一套服务,这套服务可以一起在一个隔离环境中运行。
3.使用docker-compose up就可以启动整套服务。

四、Docker-Compose的安装

https://github.com/docker/compose/releases

五、Docker-Compose实战

1、网站文件准备

nginx下的文件信息

ubuntu@VM-24-12-ubuntu:~/ck14/manual/nginx$ ls
default.conf  dist  Dockerfile  static

default.conf文件

upstream app_server {
    server django_app:8000;
}

server {
    listen       80;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;      #访问日志

    location /static/ {
        root   /usr/share/nginx/html;   #请求对应配置文件位置(定义服务器的默认网站根目录位置)
        index  index.html index.htm;     #请求对应的文件
    }
    
    location / {
        # 检查静态文件,如果不是代理到应用
        try_files $uri @proxy_to_app;
    }

    location @proxy_to_app {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://app_server;
    }

    #error_page  404              /404.html;          #定义错误提示页面

    # redirect server error pages to the static page /50x.html  #重定向服务器错误页面

    error_page   500 502 503 504  /50x.html;         #定义错误提示页面
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}


server {
        listen 81;
        location / {
                root /usr/share/nginx/html/dist; 
                index index.html;
                try_files $uri $uri/ /index.html;
        }
}

dist:前端打包文件

ubuntu@VM-24-12-ubuntu:~/ck14/auto/nginx/dist$ ls
css  favicon.ico  fonts  index.html  js

static:后端打包文件

ubuntu@VM-24-12-ubuntu:~/ck14/auto/nginx/static$ ls
admin  rest_framework

制作nginx,Dockerfile

将前后端打包文件拷贝到容器中的/usr/share/nginx/html/目录下
暴露80(后端访问)、81(前端访问)端口

FROM nginx:alpine

COPY ./static/ /usr/share/nginx/html/static/
COPY ./dist/ /usr/share/nginx/html/dist/
COPY ./default.conf /etc/nginx/conf.d/

VOLUME /var/log/
EXPOSE 80 81




生成镜像命令

```python
sudo docker build -t ck14_web_nginx .

运行容器

sudo docker run --name ck14_nginx --network ck14 -d -p 5001:80  -p 5002:81  ck14_web_nginx
  • 93
    点赞
  • 85
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 64
    评论
评论 64
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

敲代码敲到头发茂密

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值