docker compose
- docker compose简介
- docker compose yaml格式
- 1、docker-compose部署tomcat
- 2、docker-compose部署mysql
- 3、docekr-compose部署lnmp
- 项目需求
- 准备依赖文件、配置nginx
- 配置mysql
- 配置php
- 编写docker-compose.yml
- 配置mysql
- 4、容器部署registry,进行容器上传下载试验
- 下载镜像
- 在 daemon.json 文件中添加私有镜像仓库的地址并重启
- 创建registry容器并开放端口
- 给镜像打标签后上传到私有仓库
- 验证:从私有仓库下载镜像
docker compose简介
Docker Compose是一个用于定义和运行多容器Docker应用程序的工具。它使用YAML文件来配置应用程序的服务、网络和卷,并使用单个命令即可启动、停止和管理应用程序。通过使用Docker Compose,开发人员可以轻松地在多个容器之间建立连接、定义关系和扩展应用程序。
Docker Compose的主要特性包括:
-
定义服务:使用YAML文件定义应用程序的各个服务,包括镜像、容器参数、环境变量等。
-
管理多容器:通过单个命令即可启动、停止、重启和删除多个容器,而不需要逐个处理。
-
连接容器:可以通过环境变量和链接来轻松地在多个容器之间建立连接和通信。
-
管理网络和卷:可以定义网络和卷,以便在容器之间共享数据和资源。
-
扩展应用程序:可以使用多个容器来扩展应用程序,以实现负载均衡和高可用性。
总之,Docker Compose是一个简单而强大的工具,可以帮助开发人员轻松地管理和运行多容器Docker应用程序。
docker compose yaml格式
Docker Compose 是一种用于定义和运行多个容器的工具。它使用 YAML 文件来配置应用程序的服务、网络和卷等方面。
以下是 Docker Compose 文件的基本格式:
version: '3' # 指定 Compose 文件的版本
services: # 定义各个服务的配置
service1: # 服务1的名称
image: image1:tag # Docker 镜像的名称和版本
ports:
- host_port:container_port # 映射容器的端口到主机的端口
volumes:
- host_path:container_path # 挂载主机的路径到容器的路径
environment: # 设置容器的环境变量
- key=value
service2: # 服务2的名称
build: # 构建 Docker 镜像
context: path_to_context # 上下文路径,Dockerfile 所在的目录
dockerfile: Dockerfile_name # Dockerfile 的名称
depends_on:
- service1 # 依赖于服务1
networks: # 定义网络配置
network1: # 网络1的名称
driver: bridge # 网络驱动
volumes: # 定义卷配置
volume1: # 卷1的名称
driver: local # 卷的驱动
上述示例中,services
部分定义了两个服务,分别为 service1
和 service2
。每个服务都可以指定镜像、端口映射、卷挂载、环境变量和容器之间的依赖关系等配置。
networks
部分定义了网络配置,可以设置网络的驱动类型。
volumes
部分定义了卷配置,可以设置卷的驱动类型。
Compose 文件中的其他配置项包括 version
(Compose 文件的版本)和 build
(构建 Docker 镜像时的配置)等。
通过编写 Docker Compose 文件,可以定义和管理多个容器之间的关系和配置,方便地进行容器化应用程序的部署和管理。
1、docker-compose部署tomcat
文件目录:
cd /opt/homework/tomcat
wget https://mirrors.huaweicloud.com/apache/tomcat/tomcat-8/v8.5.99/bin/apache-tomcat-8.5.99.tar.gz
tdc.yaml
version: '3'
services:
tomcat0815:
container_name: tomcat0815
hostname: tomcat0815
build:
context: ./tomcat
dockerfile: Dockerfile
ports:
- 8444:8080
volumes:
- ./webapps:/usr/local/tomcat/webapps
networks:
tdc:
ipv4_address: 172.80.0.2
networks:
tdc:
driver: bridge
ipam:
config:
- subnet: 172.80.0.0/16
Dockerfile
FROM centos:7
RUN sed -i -e "s|#baseurl=http://mirror.centos.org|baseurl=http://mirrors.aliyun.com|g" /etc/yum.repos.d/CentOS-*
RUN yum clean all && yum makecache && yum install -y wget rpm
RUN wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u131-linux-x64.rpm -O /opt/jdk-8u131-linux-x64.rpm
RUN rpm -ivh /opt/jdk-8u131-linux-x64.rpm
ADD apache-tomcat-8.5.99.tar.gz /opt/javaweb/
RUN mkdir -p /usr/local/tomcat/
RUN mv /opt/javaweb/apache-tomcat-8.5.99 /usr/local/tomcat/
RUN chmod +x /usr/local/tomcat/apache-tomcat-8.5.99/bin/catalina.sh
ENV CATALINA_HOME /usr/local/tomcat/apache-tomcat-8.5.99/
# 设置启动命令
ENTRYPOINT [ "/usr/local/tomcat/apache-tomcat-8.5.99/bin/catalina.sh", "run"]
# 暴露 HTTP 端口
EXPOSE 8080
docker-compose -f ./tdc.yaml up -d
2、docker-compose部署mysql
version: '3'
services:
mysqldb:
image: mysql:5.7
container_name: db
hostname: mysql57
environment:
MYSQL_ROOT_PASSWORD: root
volumes:
- ./data:/var/lib/mysql
ports:
- 33062:3306
restart: always
networks:
mdc:
ipv4_address: 172.36.0.2
networks:
mdc:
driver: bridge
ipam:
config:
- subnet: 172.36.0.0/16
docker-compose -f ./mdc.yaml up -d
3、docekr-compose部署lnmp
项目需求
(1)使用Docker构建LNMP环境并运行Wordpress网站平台。
(2)为了方便运维和统一管理使用进行docker-compose编排
(3)将所有容器进行快照,然后将Docker镜像打包成tar包备份到本地。
(4)把docker-compose.yml
下载到本地
准备依赖文件、配置nginx
#创建项目目录 compose_nginx;
mkdir /opt/compose_nginx/
cd /opt/compose_nginx
mkdir nginx mysql php wwwroot
#上传wordpress软件包
cd /opt/compose_nginx/wwwroot/
ls wordpress-4.9.4-zh_CN.tar.gz
tar xf wordpress-4.9.4-zh_CN.tar.gz
#上传nginx软件包
cd /opt/compose_nginx/nginx/
ls nginx-1.12.0.tar.gz
nginx-1.12.0.tar.gz
cd /opt/compose_nginx/nginx/
vim Dockerfile
#编写nginx的Dockerfile文件
FROM centos:7
MAINTAINER this is nginx image <zs>
RUN sed -i -e "s|mirrorlist=http://mirror.centos.org|mirrorlist=http://mirrors.aliyun.com|g" /etc/yum.repos.d/CentOS-*
RUN sed -i -e "s|#baseurl=http://mirror.centos.org|baseurl=http://mirrors.aliyun.com|g" /etc/yum.repos.d/CentOS-*
RUN yum clean all
RUN yum makecache
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make && useradd -M -s /sbin/nologin nginx
ADD nginx-1.12.0.tar.gz /usr/local/src/
WORKDIR /usr/local/src/nginx-1.12.0
RUN ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module && make -j2 && make install
ENV PATH /usr/local/nginx/sbin:$PATH
ADD nginx.conf /usr/local/nginx/conf/
#ADD wordpress-4.9.4-zh_CN.tar.gz /usr/local/nginx/html/
RUN chmod 777 -R /usr/local/nginx/html/
EXPOSE 80
EXPOSE 443
ENTRYPOINT [ "/usr/local/nginx/sbin/nginx", "-g", "daemon off;" ]
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;
#此地址是php端地址
fastcgi_pass 172.18.0.30:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
}
}
配置mysql
cd /opt/compose_nginx/mysql/
vim Dockerfile
FROM centos:7
LABEL maintainer="this is mysql image xcz"
RUN sed -i -e "s|mirrorlist=http://mirror.centos.org|mirrorlist=http://mirrors.aliyun.com|g" /etc/yum.repos.d/CentOS-*
RUN sed -i -e "s|#baseurl=http://mirror.centos.org|baseurl=http://mirrors.aliyun.com|g" /etc/yum.repos.d/CentOS-*
RUN yum clean all
RUN yum makecache
RUN yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake make
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_INNOBASE_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1 && make -j3 && make install
ADD my.cnf /etc/
ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
RUN useradd -M -s /sbin/nologin mysql && chown mysql:mysql /etc/my.cnf && chown -R mysql:mysql /usr/local/mysql/ && /usr/local/mysql/bin/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/
EXPOSE 3306
CMD ["/usr/local/mysql/bin/mysqld"]
my.cnf
[client]
port = 3306
socket=/usr/local/mysql/mysql.sock
[mysql]
port = 3306
socket = /usr/local/mysql/mysql.sock
auto-rehash
[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
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
cd /opt/compose_nginx/php/
cd /opt/compose_nginx/php/
vim Dockerfile
FROM centos:7
LABEL maintainer=' this is php image <xcz>'
RUN sed -i -e "s|mirrorlist=http://mirror.centos.org|mirrorlist=http://mirrors.aliyun.com|g" /etc/yum.repos.d/CentOS-*
RUN sed -i -e "s|#baseurl=http://mirror.centos.org|baseurl=http://mirrors.aliyun.com|g" /etc/yum.repos.d/CentOS-*
RUN yum clean all
RUN yum makecache
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 -j2 && make install
ENV PATH /usr/local/php/bin:/usr/local/php/sbin:$PATH
ADD php.ini /usr/local/php/lib/
ADD php-fpm.conf /usr/local/php/etc/
ADD www.conf /usr/local/php/etc/php-fpm.d/
EXPOSE 9000
ENTRYPOINT [ "/usr/local/php/sbin/php-fpm", "-F" ]
准备php-fpm.conf php.ini、www.conf 配置文件
vim php.ini
#该配置文件模板位于安装目录的 php-7.1.10/php.ini-development 位置
#939行,取消注释,修改
date.timezone = Asia/Shanghai
#1170行,修改
mysqli.default_socket = /usr/local/mysql/mysql.sock
vim php-fpm.conf
#该配置文件位于 /usr/local/php/etc/php-fpm.conf.default
#17行,删除注释符号“;”
pid = run/php-fpm.pid
vim www.conf
#该配置文件位于 cd /usr/local/php/etc/php-fpm.d/
user = nginx
group = nginx
#36行,修改监听IP和端口为容器IP:9000端口
listen = 172.18.0.30:9000`
编写docker-compose.yml
version: '2.1'
services:
nginx:
hostname: nginx
build:
context: ./nginx
dockerfile: Dockerfile
container_name: nginx
ports:
- 80:80
- 443:443
networks:
lnmp:
ipv4_address: 172.18.0.10
volumes:
- ./wwwroot/:/usr/local/nginx/html
mysql:
hostname: mysql
build:
context: ./mysql
dockerfile: Dockerfile
container_name: mysql
ports:
- 3306:3306
networks:
lnmp:
ipv4_address: 172.18.0.20
volumes:
- /usr/local/mysql
php:
hostname: php
build:
context: ./php
dockerfile: Dockerfile
container_name: php
ports:
- 9000:9000
networks:
lnmp:
ipv4_address: 172.18.0.30
volumes_from:
- nginx
- mysql
depends_on:
- nginx
- mysql
links:
- nginx
- mysql
networks:
lnmp:
driver: bridge
ipam:
config:
- subnet: 172.18.0.0/16
docker-compose -f docker-compose.yml up -d
配置mysql
docker exec -it mysql /bin/bash
mysql -uroot -p
create database wordpress;
grant all privileges on wordpress.* to 'wordpress'@'%' identified by '123456';
grant all privileges on *.* to 'root'@'%' identified by 'abc123';
flush privileges;
4、容器部署registry,进行容器上传下载试验
下载镜像
docker pull registry
在 daemon.json 文件中添加私有镜像仓库的地址并重启
vim /etc/docker/daemon.json
{
"insecure-registries": ["192.168.99.130:5000"], #添加此段,“,”不可少,ip是本机IP地址
"registry-mirrors": ["https://cnqz5vcz.mirror.aliyuncs.com"]
}
systemctl daemon-reload
systemctl restart docker.service
创建registry容器并开放端口
docker create -it registry /bin/bash
docker run -d -p 5000:5000 -v /data/registry:/tmp/registry registry
给镜像打标签后上传到私有仓库
#打标签
docker tag redis:latest 192.168.99.130:5000/redis
#上传
docker push 192.168.99.130:5000/redis
验证:从私有仓库下载镜像
客户端:192.168.99.132
vim /etc/docker/daemon.json
{
"insecure-registries": ["192.168.99.130:5000"], #添加此段,“,”不可少,ip是本机IP地址
}
docker pull 192.168.99.130:5000/redis
#查看镜像
docker images