Article directory
1. Docker-compose overview
Dockerfile allows users to manage a single application container; compose allows users to define a set of associated application containers (called projects) in a template (YAML format), such as a web service container plus The database service container on the back-end, etc.
The Docker-Compose project is an official open source project of Docker, responsible for the rapid orchestration of Docker container clusters.
Docker-Compose divides the managed containers into three layers, namely project, service and container. All files (docker-compose.yml, extends file or environment variable file, etc.) under the Docker-Compose running directory form a project. If there is no special specified project name, it 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. Therefore, it is necessary to use other tools to realize service discovery and load balancing, such as Consul.
The project configuration file of Docker-Compose is docker-compose.yml by default. You can customize the configuration file through the environment variable COMPOSE_FTILE or the -f parameter, which defines multiple dependent services and the containers that each service runs.
Using a Dockerfile template file allows users to easily define a single application container. At work, there are often situations where multiple containers are required 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 a back-end database service container, and even a load balancing container.
Compose allows users to define a set of associated application containers as a project through a single docker-compose .yml template file (YAML format).
The Docker-Compose project is written in Python and calls the API provided by the Docker service to manage containers. Therefore, as long as the operating platform supports the Docker API, you can use compose on it for orchestration management.
2. YAML file format and preparation considerations
YAML is a markup language that can display data serialization format intuitively and is highly readable. 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 in square brackets [], and hashes are enclosed in brackets {}
Three, Docker Compose configuration common fields
field | illustrate |
---|---|
build | Specify the Dockerfile file name. To specify the Dockerfile file, you need to use the dockerfile tag to specify the child tag of the build tag. |
dockerfile | Build image context path |
context | Can be the path of the dockerfile, or the url address pointing to the git repository |
image | specified mirror |
command | Execute the command, overriding the default command |
container name | Specify the container name, because the container name is unique, if you specify a custom name, you cannot scale |
deploy | Specify the configuration related to deploying and running the service, which can only be used in Swarm mode |
environment | add environment variable |
networks | Join the network |
ports | Expose the container port, same as -p, but the port cannot be lower than 60 |
volumes | Mount the host path or command volume |
hostname | container hostname |
restart | Restart strategy, default no, always, no-failure, unless-stoped |
1.no, the default policy, the container is not restarted when the container exits 2.on -
failure, when the container exits abnormally (exit status is not 0), the container will restart 4. always restart the container when the container exits
5. unless-stopped, always restart the container when the container exits, but not consider containers that were stopped when the Docker daemon started
Fourth, Docker Compose common commands
build:重新构建服务
ps:列出容器
up:创建和启动容器
exec:在容器里面执行命令
scale:指定一个服务容器启动数量
top:显示容器进程
logs:查看容器输出
down:删除容器、网络、数据卷和镜像
stop/start/restart:停止/启动/重启服务
Five, compose deployment lnmp
(1) Required software packages
(2) Configure nginx
vim Dockerfile
FROM centos:7
MAINTAINER this is nginx image <jzm 2022-03-10>
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
CMD [ "/usr/local/nginx/sbin/nginx", "-g", "daemon off;" ]
(3) Configure mysql
vim Dockerfile
FROM centos:7
MAINTAINER this is mysql image <jzm 2022-03-10>
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/local/mysql/bin/mysqld
~
vim my.cnf
[client]
port = 3306
socket = /usr/local/mysql/mysql.sock
[mysql]
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
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
configure php
vim Dockerfile
FROM centos:7
MAINTAINER this is php image <zww>
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 -j4 && 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" ]
Configure the Docker-compose.yml file
version: '2'
services:
nginx:
build:
context: ./nginx
dockerfile: Dockerfile
container_name: nginx
ports:
- 1314:80
- 1315:443
networks:
lnmp:
ipv4_address: 172.20.0.10
volumes:
- ./nginx/html/:/usr/local/nginx/html
mysql:
build:
context: ./mysql
dockerfile: Dockerfile
container_name: mysql
ports:
- 3306:3306
networks:
lnmp:
ipv4_address: 172.20.0.20
volumes:
- /usr/local/mysql
php:
build:
context: ./php
dockerfile: Dockerfile
container_name: php
ports:
- 9000:9000
networks:
lnmp:
ipv4_address: 172.20.0.30
volumes_from:
- nginx
- mysql
depends_on:
- nginx
- mysql
networks:
lnmp:
driver: bridge
ipam:
config:
- subnet: 172.20.0.0/16
run docker-compose
docker-compose -f docker-compose.yml up -d
Go into the MySQL container and grant permissions
docker exec -it mysql bash
mysql
mysql> create database wordpress;
mysql> grant all privileges on wordpress.* to 'wordpress'@'%' identified by 'abc123';
mysql> grant all privileges on *.* to 'root'@'%' identified by 'abc123';
mysql> flush privileges;
browser access test
http://192.168.100.135/wordpress/index.php