docker容器-compose单机容器编排

目录

yaml文件使用的注意事项

yaml文件的基本数据结构

docker-compose单机容器编排

docker-compose的常用命令

安装docker-compose

docker-compose部署nginx

nginx文件

yml文件

docker-compose创建镜像和容器

docker-compose构建lnmp

nginx文件

mysql文件

php文件

yml文件

查看镜像

查看容器

配置mysql容器


yaml文件:是一种标记语言,以竖列的形式展示序列化的数据格式,可读性高

类似于json格式,语法简单

yaml通过缩进来表示数据结构,连续的项目用-减号来表示

yaml文件使用的注意事项

1、大小写敏感

2、通过缩进表示层级关系

3、只能用空格缩进不能用tab缩进

4、可以使用#注释

5、符号字符后面都有一个空格 test

6、单引号引起来的字符串,会被当做普通字符串处理,"#&*" 双引号里面是特殊字符,就是本义

'$'

"$"

yaml文件的基本数据结构

1、声明变量(标量,单个的,不可再分的值 整数,布尔类型)

name:xiaobu
age:20
tall:yes
handsan:yes
wan:no

2、list(列表)表示有序的元素集合

class:
  - xiaobu
  - xiaobu2
  - xiaobu3

3、键值对映射

student:
  name:xiaobu
  age:20
student:
  name:xiaobu2
  age:18

4、注释

#test

5、引用

student:&id1
  name:xiaobu
  age:20
student1:*id1
用&来定义锚点,用*来进行引用

6、字符串引号

name: 'xiaobu'
name: "xiaobu"
name: '$'
docker-compose单机容器编排

Dockerfile:先配置好文件,然后build,镜像----->容器

docker-compose 既可以基于Dockerfile也可以基于镜像,一键式拉起镜像和容器

docker-compose核心就是yaml文件,可以定义容器一切,通过yml配置,直接运行容器

docker:单节点,docker没有统一对外提供服务的端口和配置,docker一般都是内部访问,无需使用https

如果要对外以集群和分布式的方式提供统一访问的端口,k8s+ingress

docker-compose的常用命令
build:构建服务
ps: 列出由docker-compose编排出来的容器
up	创建和启动容器
top	显示容器的进程
logs	查看容器的输出日志
down	删除容器,网络,数据卷
exec	在容器中执行命令
stop/start/reraet	停止,启动,重启容器
scale	指定启动的容器数量(副本)
安装docker-compose
把源码文件拖到opt目录下
mv docker-compose-linux-x86_64 docker-compose
mv docker-compose  /usr/local/bin/
chmod  777 /usr/local/bin/docker-compose
docker-compose部署nginx
nginx文件
安装插件
yum -y install tree
在opt目录下创建compose-nginx
在ngin-compose下创建nginx
把nginx的源码包拖到nginx目录下
FROM centos:7
RUN yum -y install gcc pcre-devel openssl-devel zlib-devel openssl openssl-devel
ADD nginx-1.22.0.tar.gz /usr/local/src/
RUN useradd -M -s /sbin/nologin nginx
WORKDIR /usr/local/src/nginx-1.22.0
RUN ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module && make -j 4 && make install
ENV PATH /usr/local/nginx/sbin:$PATH
RUN chmod -R 777 /usr/local/nginx/html
EXPOSE 80
VOLUME ["/usr/local/nginx/html/"]
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]

在nginx目录下创建www目录
echo "this is nginx1" >> index.html
yml文件
vim docker-compose.yml
version: '3'
services:
  nginx:
    container_name: web1
    hostname: nginx
    build:
      context: /opt/compose-nginx/nginx
      dockerfile: Dockerfile
    ports:
      - 1213:80
    networks:
      mynet:
        ipv4_address: 172.18.0.10
    volumes:
      - /opt/compose-nginx/www:/usr/local/nginx/html
networks:
  mynet:
    driver: bridge
    ipam:
      config:
        - subnet: 172.18.0.0/16
    
     
version: '3'
#指定docker-compose的文件版本
services:
#定义需要运行的服务
  nginx:
    container_name: web1
#指定创建容器的名字
    hostname: nginx
    #指定容器的主机名
    build:
#指定构建nginx容器的上下文以及Dockerfile
      context: /opt/compose-nginx/nginx
#指定Dockerfile的位置,如果用镜像换成image:
      dockerfile: Dockerfile
    ports:
#直接指定宿主机和容器的端口映射关系
      - 1213:80
    networks:
#声明给容器指定的IP地址
      mynet:
#自定义网络
        ipv4_address: 172.18.0.10
    volumes:
      - /opt/compose-nginx/www:/usr/local/nginx/html
networks:
#创建自定义网桥接口配置
  mynet:
    driver: bridge
#声明网段的类型
    ipam:
#声明网段,IP地址的管理和配置
      config:
#子网配置,指定IP地址的范围
        - subnet: 172.18.0.0/16
docker-compose创建镜像和容器
docker-compose -f docker-compose.yml up -d
docker-compose	命令的开头,使用docker-compose进行容器编排
-f	使用特定的compose的模版文件,一般都用docker-compose.yml
-p	project-name:指定项目名称,如果不加,会使用目录名称
-d 后台运行

页面访问

docker容器-compose单机容器编排_第1张图片

docker-compose构建lnmp
nginx文件
在opt目录下创建lnmp
cd /lnmp
mkdir nginx mysql php
配置nginx
cd /nginx
把源码文件拖到nginx目录下
vim nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        charset utf-8;
        location / {
            root   html;
            index  index.html index.php;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        location ~ \.php$ {
            root           html;
            fastcgi_pass   172.111.0.30:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
            include        fastcgi_params;
        }
}
}

vim Dockerfile
FROM centos:7
RUN yum -y install gcc pcre-devel openssl-devel zlib-devel openssl openssl-devel
ADD nginx-1.22.0.tar.gz /usr/local/src/
RUN useradd -M -s /sbin/nologin nginx
WORKDIR /usr/local/src/nginx-1.22.0
RUN ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module && make -j 4 && make install
ENV PATH /usr/local/nginx/sbin:$PATH
COPY nginx.conf /usr/local/nginx/conf/
ADD wordpress-6.4.2-zh_CN.tar.gz /usr/local/nginx/html
RUN chmod -R 777 /usr/local/nginx/html
EXPOSE 80
VOLUME ["/usr/local/nginx/html/"]
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]
mysql文件
配置mysql
把源码包拖到mysql目录下

cd /mysql
vim Dockerfile
FROM centos:7
RUN yum -y install ncurses ncurses-devel bison cmake pcre-devel zlib-devel gcc gcc-c++ make && useradd -M -s /sbin/nologin mysql
ADD mysql-boost-5.7.20.tar.gz /usr/local/src
WORKDIR /usr/local/src/mysql-5.7.20/
RUN cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8  \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1 && make -j 4 && make install
COPY my.cnf /etc/my.cnf
EXPOSE 3306
RUN chown -R mysql:mysql /usr/local/mysql && chown mysql:mysql /etc/my.cnf
WORKDIR /usr/local/mysql/bin/
RUN ./mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data && cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/ && systemctl enable mysqld
ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
VOLUME ["/usr/local/mysql"]
ENTRYPOINT ["/usr/sbin/init"]

vim my.cnf
[client]
port = 3306
socket=/usr/local/mysql/mysql.sock

[mysqld]
user = mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
character-set-server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket=/usr/local/mysql/mysql.sock
bind-address = 0.0.0.0
skip-name-resolve
max_connections=2048
default-storage-engine=INNODB
max_allowed_packet=16M
server-id = 1
general_log=ON
general_log_file=/usr/local/mysql/data/mysql_general.log

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
php文件
配置php
把源码包和源码文件拖到php目录下
vim Dockerfile
FROM centos:7
RUN yum -y install gd \
libjpeg libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel \
gcc gcc-c++ make pcre-devel && useradd -M -s /sbin/nologin nginx
ADD php-7.1.10.tar.bz2 /usr/local/src
WORKDIR /usr/local/src/php-7.1.10
RUN ./configure \
--prefix=/usr/local/php \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-fpm \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip && make -j 4 && make install
ENV PATH /usr/local/php/bin:/usr/local/php/sbin:$PATH
COPY php.ini /usr/local/php/lib
COPY php-fpm.conf /usr/local/php/etc/
COPY www.conf /usr/local/php/etc/php-fpm.d/
EXPOSE 9000
ENTRYPOINT ["/usr/local/php/sbin/php-fpm","-F"]
yml文件
yml文件在lnmp目录下创建
vim 
version: '3'
services:
  nginx:
    hostname: nginx
    build:
      context: /opt/lnmp/nginx/
      dockerfile: Dockerfile
    ports:
      - 1314:80
    container_name: nginx_server
    networks:
      lnmp:
        ipv4_address: 172.111.0.10
    volumes:
          #声明挂载目录
      - "nginx-data:/usr/local/nginx/html"
  mysql:
     hostname: mysql
     build:
       context: /opt/lnmp/mysql/
       dockerfile: Dockerfile
     ports:
       - 3306:3306
     container_name: mysql_server
     networks:
       lnmp:
         ipv4_address: 172.111.0.20
     privileged: true
     #索然我们登录容器,显示的是root用户,伪管理员,一旦privileged: true,容器内的root用户和宿主机的root用户,拥有一样的权限,在生产中能不给就不给
     volumes:
      - "mysql-data:/usr/local/mysql/"
  php:
    hostname: php
    build:
      context: /opt/lnmp/php
      dockerfile: Dockerfile
    ports:
      - 9000:9000
    container_name: php_server
    networks:
      lnmp:
        ipv4_address: 172.111.0.30
    volumes:
      - "nginx-data:/usr/local/nginx/html"
      - "mysql-data:/usr/local/mysql"
networks:
  lnmp:
    driver: bridge
    ipam:
      config:
        - subnet: 172.111.0.0/16
volumes:
  nginx-data:
  mysql-data:
  
docker-compose -f docker-compose.yml up -d
查看镜像
[root@docker2 compose-nginx]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED          SIZE
compose-nginx-nginx   latest    6862f228cb29   15 minutes ago   557MB
lnmp-mysql            latest    77a863e4d607   37 minutes ago   10.1GB
lnmp-php              latest    777e020fb38a   39 minutes ago   1.37GB
lnmp-nginx            latest    e82d70afe57e   47 minutes ago   702MB
centos                7         eeb6ee3f44bd   2 years ago      204MB
查看容器
[root@docker2 compose-nginx]# docker ps -a
CONTAINER ID   IMAGE                 COMMAND                  CREATED          STATUS          PORTS                                       NAMES
f526cb362100   compose-nginx-nginx   "/usr/local/nginx/sb…"   16 minutes ago   Up 16 minutes   0.0.0.0:1213->80/tcp, :::1213->80/tcp       web1
141ef4a1f02d   lnmp-nginx            "/usr/local/nginx/sb…"   37 minutes ago   Up 37 minutes   0.0.0.0:1314->80/tcp, :::1314->80/tcp       nginx_server
9b26ed6b2b3e   lnmp-php              "/usr/local/php/sbin…"   37 minutes ago   Up 37 minutes   0.0.0.0:9000->9000/tcp, :::9000->9000/tcp   php_server
12c7a3e7295c   lnmp-mysql            "/usr/sbin/init"         37 minutes ago   Up 37 minutes   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp   mysql_server
配置mysql容器
mysql -uroot -p
create database wordpress;
grant all privileges on wordpress.* to 'wordpress'@'%' identified by '123456';
grant all privileges on *.* to 'root'@'%' identified by '123456';
flush privileges;

页面访问

docker容器-compose单机容器编排_第2张图片

你可能感兴趣的:(运维,服务器,docker)