docker-compose是docker的服务编排工具,主要用来构建基于docker的复杂应用。compose通过一个配置文件管理多个docker容器,非常适合于组合对个容器进行开发的场景。(当然k8s是当前主流编排工具)

文件格式及编写注意事项:
使用YAML是需要注意如下事项:
1.不支持使用制表符TAB键缩进,需要使用空格缩进。(严格)
2. 通常开头缩进两个空格
3. 字符后缩进一个空格,如冒号、逗号、横杠
4. 用#号注释
5. 如果包含特殊字符,要求使用单引号(‘’)引起来
6. 布尔值(true、false、yes、no、off)必须用双引号(“”)括起来,这样分析器才会将它们解释为字符串

配置常用字段描述:

字段描述
build指定dockerfile文件名
dockerfile构建镜像上下文路径
context可以是dockerfile路径也可以是Git仓库的URL
image指定镜像
command执行命令,覆盖默认命令
container_name指定容器名称。由于容器是唯一的,如果指定自定义名称,则无法使用scale命令
deploy指定部署和运行服务相关配置,只能在Swarm模式使用
environment添加环境变量
networks加入网络
ports暴露容器端口,与-p相同,但端口不能低于60
volumes挂载宿主机路径或命令卷
restart重启策略,默认值为no,可选值有always、no-failure、unless-stoped
hostname容器主机名
depends_on定义安装之前需要的依赖(优先安装依赖)

compose常用命令:

字段描述
build重新构建服务
ps列出容器
up创建容器和启动容器
exec在容器里执行命令
scale指定一个服务容器的启动数量
top显示容器进程
logs查看容器输出
down删除容器、网络、数据卷和镜像
stop、start、restart停止、启动、重启服务

实战部署LDNMP例子:

version: '3'
services:
  nginx:
    image: nginx
    container_name: dnmp_nginx
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
      - ./nginx/conf.d:/etc/nginx/conf.d:ro
      - ./logs/nginx/:/var/log/nginx/:rw
      - ./webroot/:/var/www/html/:rw
    networks:
      - my-net
    restart: always
    depends_on:
      - php-fpm
  php-fpm:
    build:
      context: ./php-fpm
      dockerfile: Dockerfile
    container_name: dnmp_php
    expose:
      - "9000"
    volumes:
      - ./logs/php/:/var/log/php/:rw
      - ./webroot/:/var/www/html/:rw
    networks:
      - my-net
    restart: always
    depends_on:
      - mysql
  mysql:
    image: mysql:5.7
    container_name: dnmp_mysql
    ports:
      - "3306:3306"
    volumes:
      - ./logs/mysql/:/var/log/mysql/:rw
      - ./db-data/:/var/lib/mysql/:rw
    environment:
      MYSQL_ROOT_PASSWORD: rootpasswd
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
    networks:
      - my-net
    restart: always
networks:
  my-net:
# 注: php-fpm 是自己写的dockerfile所以引用的上下文
# 注: compose 所用到的映射文件或目录需提前创建ok(最好一个目录下)
# 附树结构图
├── db-data
├── dnmp.sh
├── docker-compose.yml
├── logs
│   ├── mysql
│   ├── nginx
│   │   ├── access.log
│   │   └── error.log
│   └── php
├── mysql
│   └── my.cnf
├── nginx
│   ├── conf.d
│   │   └── site.conf
│   └── nginx.conf
├── php-fpm
│   └── Dockerfile
└── webroot
    └── index.php


# 跑完compose之后可以直接把DZ或WordPress丢到webroot目录下访问

注 :一些nginx必要配置文件可直接复制:

user nginx;
worker_processes 2;
worker_cpu_affinity 00000001 00000010;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
worker_rlimit_nofile 65535; 
include /usr/share/nginx/modules/*.conf;

events {
use epoll;
worker_connections 65535;
multi_accept on;
}


http {
	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  /var/log/nginx/access.log  main;

charset UTF-8;
server_tokens off;
client_header_buffer_size 4k;
client_max_body_size 1024m;

open_file_cache max=204800 inactive=20s;
open_file_cache_min_uses 1;
open_file_cache_valid 30s; 
open_file_cache_errors on;

fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 4k;
fastcgi_buffers 8 4k;
fastcgi_busy_buffers_size 8k;
fastcgi_temp_file_write_size 8k;


#Gzip Compression
gzip on;
gzip_buffers 16 8k;
gzip_comp_level 6;
gzip_http_version 1.1;
gzip_min_length 256;
gzip_proxied any;
gzip_vary on;
gzip_types
    text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml
    text/javascript application/javascript application/x-javascript
    text/x-json application/json application/x-web-app-manifest+json
    text/css text/plain text/x-component
    font/opentype application/x-font-ttf application/vnd.ms-fontobject
    image/x-icon;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";


sendfile            on;
tcp_nopush          on;
tcp_nodelay         on;
keepalive_timeout   65;
types_hash_max_size 2048;


include             /etc/nginx/mime.types;
default_type        application/octet-stream;

include /etc/nginx/conf.d/*.conf;
}


# 注 nginx conf.d目录下配置文件:
server {
listen       80;
server_name  localhost;

location / {
    root   /var/www/html;
    index  index.html index.htm index.php;
}

error_page   500 502 503 504  /50x.html;
location = /50x.html {
    root   /usr/share/nginx/html;
}

location ~ \.php$ {
    root /var/www/html;
    fastcgi_pass   dnmp_php:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
}

}
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐