1. Overview of Docker-compose
The Docker-Compose project is an official open source project of Docker, which is responsible for the rapid arrangement of Docker container clusters.
Docker-Compose divides the managed containers into three layers, namely project (project), service (service) and container (container). All the files in the Docker-Compose running directory (docker-compose.yml, extends file or environment variable file, etc.) form a project. If there is no special designation, the project name is the current directory name. A project can contain multiple services, and each service defines the image, parameters, and dependencies of the container running. A service can include multiple container instances. Docker-Compose does not solve the problem of load balancing, so other tools are needed to realize service discovery and load balancing. For example, the default project configuration file of Consul Docker-Compose is docker-compose.yml
. The configuration file can be customized through the environment variable COMPOSE_FILE or the -f parameter, which defines multiple dependent services and the container running each service
uses a Dockerfile template file, which allows users to easily define a separate application container. In work, we often encounter situations that require multiple containers to cooperate with each other to complete a certain task. For example, to implement a web project, in addition to the web service container itself, it is often necessary to add back-end database service containers, and even load balancing containers, etc. Compose allows users to use
a separate docker-compose.yml template file (YAML format) To define a set of associated application containers as a project.
The Docker-Compose project is written in Python and calls the API provided by the Docker service to manage the container. Therefore, as long as the operating platform supports the Docker API, Compose can be used to arrange and manage the
YAML file format and writing precautions
YAML is a markup language, which can display data serialization format very intuitively, with high readability. Similar to XML data description language, the syntax is much simpler than XML.
YAML data structures are represented by indentation, consecutive items are represented by minus signs, key-value pairs are separated by colons, arrays are enclosed by brackets [], and hashes are enclosed by curly braces {}.
2. YAML file format and writing precautions
YAML is a markup language, which can display data serialization format very intuitively, with high readability. Similar to XML data description language, the syntax is much simpler than XML.
YAML data structures are represented by indentation, consecutive items are represented by minus signs, key-value pairs are separated by colons, arrays are enclosed by brackets [], and hashes are enclosed by curly braces {}.
3. Common fields for Docker Compose configuration
1.no, the default policy, does not restart the container when the container exits
2.on-failure, when the container exits abnormally (exit status is not 0), the container will be restarted
3.on-failure:3, when the container exits abnormally Restart the container at any time, up to 3 times
4.always, always restart the container when the container exits
5.unless-stopped, always restart the container when the container exits, but do not consider containers that have stopped when the Docker daemon starts
Docker Compose common commands
字段 描述
build 重新构建服务
ps 列出容器
up 创建和启动容器
exec 在容器里面执行命令
scale 指定一个服务容器启动数量
top 显示容器进程
logs 查看容器输出
down 删除容器、网络、数据卷和镜像
stop/start/restart 停止/启动/重启服务
Docker Compose file structure
yum ``install` `-y tree
tree ``/opt/compose_nginx
/opt/compose_nginx/
├── docker-compose.yml ``#创建模板脚本
├── nginx
│?? ├── Dockerfile ``#创建容器脚本
│?? ├── nginx-1.12.0.``tar``.gz ``#复制源码包
│?? └── run.sh ``#启动服务脚本
└── wwwroot
``└── index.html ``#站点网页
compose deploy lnmp
1. Docker Compose environment installation
Docker Compose 是 Docker 的独立产品,因此需要安装 Docker 之后在单独安装 Docker Compose
#下载 在Linux上我们可以从GitHub上下载它的二进制包来使用,此命令是下载Docker Compose的当前稳定版本'
curl -L https://github.com/docker/compose/releases/download/1.21.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
#安装
chmod +x /usr/local/bin/docker-compose
#查看版本
docker-compose --version
2. Create a working directory and move the corresponding installation package and configuration file
----------------------nginx-----------------------------
1.mkdir -p /opt/compose-lnmp/nginx
2.cd /opt/compose-lnmp/nginx
3.把nginx-1.12.0.tar.gz和wordpress-4.9.4-zh_CN.tar.gz和nginx的配置文件nginx.conf移进来(配置文件都已经修改好)
----------------------mysql-----------------------------
1.mkdir -p /opt/compose-lnmp/mysql
2.cd /opt/compose-lnmp/mysql
3.把boost_1_59_0.tar.gz和mysql-5.7.17.tar.gz和mysql配置文件my.cnf移动到本目录(配置文件都已经修改好)
----------------------php--------------------------------
1.mkdir -p /opt/compose-lnmp/php
2.cd /opt/compose-lnmp/php
3.把php-7.1.10.tar.bz2、php-fpm.conf、php.ini、www.conf移动到本目录(配置文件都已经修改好)
-----------------------------------
3.dockerfile
#nginx
vim Dockerfile
FROM centos:7
MAINTAINER this is nginx image <lzc>
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make
RUN 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 && 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;" ]
#mysql
vim Dockerfile
FROM centos:7
MAINTAINER this is mysql image <lzc>
RUN yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake make
RUN 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_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 -j4 && make install
RUN chown -R mysql:mysql /usr/local/mysql/
ADD my.cnf /etc/my.cnf
RUN chown mysql:mysql /etc/my.cnf
ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
WORKDIR /usr/local/mysql/
RUN bin/mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
RUN cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
EXPOSE 3306
RUN systemctl enable mysqld
VOLUME [ "/usr/local/mysql" ]
CMD /usr/sbin/init
#php
vim Dockerfile
FROM centos:7
MAINTAINER this is php image <lzc>
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
RUN 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 && 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
CMD /usr/local/php/sbin/php-fpm -F
4. Build the compose file of lnmp
cd /opt/compose-lnmp #compose的工作目录
vim docker-compose.yml #构建compose的yml文件
version: '2' #版本2能使用volumes_from
services:
nginx:
hostname: nginx
build:
context: ./nginx
dockerfile: Dockerfile
container_name: nginx
ports:
- 1111:80
- 2222:443
volumes:
- ./nginx/html/:/usr/local/nginx/html
networks:
lnmp:
ipv4_address: 172.18.0.10
mysql:
hostname: mysql
build:
context: ./mysql
dockerfile: Dockerfile
container_name: mysql
networks:
lnmp:
ipv4_address: 172.18.0.20
ports:
- 3306:3306
php:
hostname: php
build:
context: ./php
dockerfile: Dockerfile
container_name: php
networks:
lnmp:
ipv4_address: 172.18.0.30
ports:
- 9000:9000
volumes_from:
- nginx
- mysql
networks:
lnmp:
driver: bridge
ipam:
config:
- subnet: 172.18.0.0/16
5. Start the lnmp built by compose
cd /opt/compose-lnmp/
docker-compose -f docker-compose.yml up -d
----------------------------------------------------------
-f, --file FILE :使用特定的 compose 模板文件,默认为 docker-compose.yml
-p, --project-name NAME :指定项目名称,默认使用目录名称
-d :在后台运行
----------------------------------------------------------
docker ps -a