字段 | 说明 |
---|---|
build | 指定 Dockerfile 文件名,要指定Dockerfile文件需要在build标签的子级标签中使用dockerfile标签指定 |
dockerfile | 构建镜像上下文路径 |
context | 可以是 dockerfile 的路径,或者是指向 git 仓库的 url 地址 |
image | 指定镜像 |
command | 执行命令,覆盖默认命令 |
container name | 指定容器名称,由于容器名称是唯一的,如果指定自定义名称,则无法 scale |
deploy | 指定部署和运行服务相关配置,只能在 Swarm 模式使用 |
environment | 添加环境变量 |
networks | 加入网络 |
ports | 暴露容器端口,与 -p 相同,但端口不能低于 60 |
volumes | 挂载宿主机路径或命令卷 |
hostname | 容器主机名 |
restart | 重启策略,默认 no,always,no-failure,unless-stoped |
字段 | 描述 |
---|---|
build | 重新构建服务 |
ps | 列出容器 |
up | 创建和启动容器 |
exec | 在容器里面执行命令 |
scale | 指定一个服务容器启动数量 |
top | 显示容器进程 |
logs | 查看容器输出 |
down | 删除容器、网络、数据卷和镜像 |
stop/start/restart | 停止/启动/重启服务 |
Docker Compose 文件结构
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 ``#站点网页
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. 创建工作目录并移动相应安装包、配置文件
----------------------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移动到本目录(配置文件都已经修改好)
mkdir -p /opt/compose_nginx/nginx /opt/compose_nginx/wwwroot
cd /opt/compose_nginx/nginx
cp nginx-1.12.0.tar.gz ./
vim run.sh
#!/bin/bash
/usr/local/nginx/sbin/nginx
vim Dockerfile
#基于基础镜像
FROM centos:7
#用户信息
MAINTAINER this is nginx image
#添加环境包
RUN yum -y update
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make
RUN useradd -M -s /sbin/nologin nginx
#上传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
#指定http和https端口
EXPOSE 80
EXPOSE 443
方法一:
RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf #关闭 nginx 在后台运行
#添加宿主机中run.sh到容器中
ADD run.sh /run.sh
RUN chmod 755 /run.sh
CMD ["/run.sh"]
方法二:
ENTRYPOINT [ "/usr/local/nginx/sbin/nginx", "-g", "daemon off;" ]
echo "this is test web" > /opt/compose_nginx/wwwroot/index.html
配置MySQL
[root@localhost compose]# cd mysql/
[root@localhost mysql]# ls
Dockerfile my.cnf mysql-boost-5.7.20.tar.gz
[root@localhost mysql]# cat 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
[root@localhost mysql]# cat Dockerfile
FROM centos:7
MAINTAINER this is mysql image
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
配置PHP
[root@localhost compose]# cd php/
[root@localhost php]# ls
Dockerfile php-7.1.10.tar.bz2 php-fpm.conf php.ini www.conf
[root@localhost php]# cat Dockerfile
FROM centos:7
MAINTAINER this is php image
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
ENTRYPOINT [ "/usr/local/php/sbin/php-fpm", "-F" ]
编写配置文件docker-compose.yml
vim /opt/compose_nginx/docker-compose.yml
version: '2'
services:
nginx:
hostname: nginx
build:
context: ./nginx
dockerfile: Dockerfile
ports:
- 1314:80
- 1315:443
container_name: nginx
networks:
lnmp:
ipv4_address: 172.20.0.10
volumes:
- ./nginx/html/:/usr/local/nginx/html
- ./nginx/nginx.conf:/usr/local/nginx/conf/nginx.conf
networks:
lnmp:
driver: bridge
ipam:
config:
- subnet: 172.20.0.0/16
----------------------------------------------------------------------------------------------------------
-f, --file FILE :使用特定的 compose 模板文件,默认为 docker-compose.yml
-p, --project-name NAME :指定项目名称,默认使用目录名称
-d :在后台运行
----------------------------------------------------------------------------------------------------------
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b48dceee248f compose_nginx_nginx "/run.sh" About a minute ago Up About a minute 0.0.0.0:1216->80/tcp, 0.0.0.0:1217->443/tcp compose_nginx_nginx_1
cd /opt/compose_nginx/
docker-compose ps #必须在docker-compose.yml所在目录执行此命令
运行docker compose
[root@localhost compose]# ls
docker-compose.yml mysql nginx php
[root@localhost compose]# docker-compose -f docker-compose.yml up -d
进入MySQL容器并授予权限
docker-compose ps #运行结果
docker exec -it mysql bash
mysql
create database wordpress;
grant all privileges on wordpress.* to 'wordpress'@'%' identified by 'abc123';
grant all privileges on *.* to 'root'@'%' identified by 'abc123';
flush privileges;
浏览器访问测试http://192.168.161.154:1314/wordpress/index.php