Docker-Compose

Docker-Compose

  • [1. 核心概念](#1. 核心概念)
  • [2. compose使用的三个流程](#2. compose使用的三个流程)
  • [3. 案例](#3. 案例)
    • [3.1 步骤](#3.1 步骤)
    • [3.2 测试](#3.2 测试)

dockerCompose是Docker公司推出的一个工具软件,可以管理多个Docker容器组成一个应用。你需要定义一个YAML格式的配置文件docker-compose.yml,写好多个容器之间的调用关系。然后只需要一个命令,就能同时启动/关闭这些容器。

官网:https://docs.docker.com/compose/compose-file/compose-file-v3/

官网下载:https://docs.docker.com/compose/install/

新版的docker自带的有compose,不用下载

1. 核心概念

一文件:docker-compose.yml

两要素:

  • 服务:一个个应用容器实例,比如订单微服务、库存微服务、mysql容器等
  • 工程:由一组关联的应用容器组成的一个完整业务单元,在docker-compose.yml文件中定义

工程=多个服务(容器应用实例)

不使用compose,会有什么问题?

  • 先后顺序要求固定,先mysql+redis才能微服务访问成功
  • 多个run命令...
  • 容器间的启停或宕机,有可能导致IP地址对应的容器实例变化,映射出错,要么生产IP写死(可以但是不推荐),要么通过服务调用

2. compose使用的三个流程

  1. 编写Dockerfile定义各个微服务应用并构建出对应的镜像
  2. 使用docker-compose.yml定义一个完整业务单元,安排好整体应用中的各个容器服务
  3. 最后执行docker-compose up命令来启动并运行整个应用程序,完成一键部署上线

3. 案例

3.1 步骤

使用dockercompose安装mysql,redis和nginx,并完成持久化操作

需要的目录:

mkdir -p /myfile/docker_test
cd /myfile/docker_test

在docker_test存放docker-compose.yml文件和相应的容器挂载目录

需要的镜像:

docker pull mysql:8.0.33
docker pull nginx
docker pull redis:6.0.8

编写的docker-compose.yml文件

你们用的时候可以改一下挂载目录和mysql密码啥的

version: "3"

services:
  # Redis服务
  redis:
    image: redis:6.0.8
    ports:
      - "6379:6379"
    volumes:
      - /myfile/docker_test/redis/redis.conf:/etc/redis/redis.conf
      - /myfile/docker_test/redis/data:/data
    command: redis-server /etc/redis/redis.conf

  # MySQL服务
  mysql:
    image: mysql:8.0.33
    environment:
      MYSQL_ROOT_PASSWORD: '123456'
      MYSQL_ALLOW_EMPTY_PASSWORD: 'no'
      MYSQL_DATABASE: 'db2023'
      MYSQL_USER: 'zhuyi'
      MYSQL_PASSWORD: '123456'
    ports:
      - "3306:3306"
    volumes:
      - /myfile/docker_test/mysql/db:/var/lib/mysql
      - /myfile/docker_test/mysql/conf/my.cnf:/etc/my.cnf
      - /myfile/docker_test/mysql/init:/docker-entrypoint-initdb.d
    command: --default-authentication-plugin=mysql_native_password # 解决外部无法访问

  # Nginx服务
  nginx:
    image: nginx:latest
    restart: always # 开机自动重启
    ports:
      - '80:80'
      - '443:443'
    volumes:
      - /myfile/docker_test/nginx/conf/nginx.conf:/etc/nginx/nginx.conf
      - /myfile/docker_test/nginx/log:/var/log/nginx
      - /myfile/docker_test/nginx/html:/etc/nginx/html

检查docker-compose.yml配置有没有问题,有问题才输出

docker compose config -q

启动所有docker-compose服务

docker compose up -d

一些配置文件

  1. nginx的配置文件

nginx.conf

properties 复制代码
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  192.168.223.129;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #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   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;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

nginx的默认启动页面,放在html目录里

index.html

html 复制代码
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
  1. redis的配置文件

redis我只简单开启了持久化的设置

redis.conf

appendonly yes

3.2 测试

mysql能远程连接上

mysql能远程连接上

nginx的默认页面也能出来

  • 如果远程连接不上,应该是你的docker网络的问题,可以试试重新来一遍compose的流程,重启docker或者检查一下docker网络
  • 通过docker compose down可以停止并删除编排后产生的容器,网络,卷,镜像

经过测试,删除容器并再一次创建容器后,原来的数据还在

相关推荐
-SGlow-4 分钟前
Linux相关概念和重要知识点(4)(自举、vim)
linux·运维·vim
多多*29 分钟前
OJ在线评测系统 登录页面开发 前端后端联调实现全栈开发
linux·服务器·前端·ubuntu·docker·前端框架
卑微的码蚁31 分钟前
服务器相关问题
运维·服务器
博洋科技33 分钟前
网站建设的服务器该如何选择?
运维·服务器·网站建设·保定响应式网站建设·保定h5网站建设·保定网站建设
人类群星闪耀时37 分钟前
服务器管理:从零开始的服务器安装与配置指南
运维·服务器
陈大爷(有低保)1 小时前
UDP Socket聊天室(Java)
java·网络协议·udp
kinlon.liu1 小时前
零信任安全架构--持续验证
java·安全·安全架构·mfa·持续验证
NiNg_1_2341 小时前
使用Docker Compose一键部署
运维·docker·容器
萠哥啥都行1 小时前
Linux安装Docker以及Docker入门操作
运维·docker·容器
王哲晓1 小时前
Linux通过yum安装Docker
java·linux·docker