使用docker-compose单机编排lnmp环境,并实现iWebShop商城发布。且镜像推送到私有仓库,后期编排更加方便。最后部署Weave Scope监控容器。
harbor安装需要使用docker-compose
安装docker-compose
[root@harbor ~]# yum install epel-release -y
[root@harbor ~]# yum install python-pip -y
[root@harbor ~]# pip install docker-compose
[root@harbor ~]# docker-compose --version
docker-compose version 1.24.0, build 0aa5906
部署Harbor
[root@harbor ~]# tar xf harbor-offline-installer-v1.8.0.tgz.tar -C /usr/local
修改配置文件
[root@harbor ~]# vim /usr/local/harbor/harbor.yml
hostname = 192.168.231.32 #设置主机名
刷新配置文件
[root@harbor ~]# /usr/local/harbor/prepare
进行安装
[root@harbor ~]# /usr/local/harbor/install.sh
✔ ----Harbor has been installed and started successfully.----
配置docker
添加不安全的注册,否则不能推送进行,需要配置https
[root@harbor ~]# vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://xxxx.mirror.aliyuncs.com"],
"insecure-registries": ["192.168.231.32"]
}
重启docker
[root@harbor ~]# systemctl restart docker
浏览器访问
输入主机ip,用户名和密码默认为:admin,Harbor12345
新建仓库
lnmp服务器推送镜像进行测试
配置docker
[root@lnmp ~]# cat /etc/docker/daemon.json
{
"registry-mirrors": ["https://xxxx.mirror.aliyuncs.com"],
"insecure-registries": ["192.168.231.32:80"]
}
重启docker
[root@lnmp ~]# systemctl restart docker
给镜像打标签
[root@lnmp ~]# docker tag centos:7 192.168.231.32/real/centos:7
登录私有镜像库
[root@lnmp ~]# docker login 192.168.231.32
Username: admin
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
推送镜像
[root@lnmp ~]# docker push 192.168.231.32/real/centos:7
[root@lnmp ~]# mkdir -p lnmp/{nginx,mysql,php}
[root@lnmp ~]# mkdir -p lnmp/nginx/{html,conf.d,log}
docker-compose编写
[root@lnmp lnmp]# cat docker-compose.yml
version: '2.2'
services:
nginx:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./nginx/html:/usr/share/nginx/html #网页文件
- ./nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf #nginx配置文件
- ./nginx/log:/var/log/nginx/ #nginx日志
environment:
- TZ=Asia/Shanghai
container_name: "nginx"
networks:
- front
networks:
front:
nginx配置文件
[root@lnmp ~]# cat lnmp/nginx/conf.d/default.conf
server {
index index.php index.html;
server_name php-docker.local;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /usr/share/nginx/html;
}
创建index.html
[root@lnmp ~]# cat lnmp/nginx/html/index.html
启动容器
[root@lnmp lnmp]# docker-compose up -d
访问测试
编写php的Dockerfile文件
[root@lnmp ~]# cat lnmp/php/Dockerfile
FROM php:7-fpm
COPY zhongkeda.list /etc/apt/sources.list
RUN apt-get update && apt-get install -y libfreetype6-dev libjpeg-dev libpng-dev zlib1g-dev libzip-dev\
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install gd \
&& docker-php-ext-install mysqli \
&& docker-php-ext-install pdo_mysql \
&& docker-php-ext-install zip \
&& docker-php-ext-install sockets \
&& rm -r /var/lib/apt/lists/*
上边安装相关扩展,与mysql进行连接
创建debian镜像源
否则下载会很慢
[root@lnmp ~]# cat lnmp/php/zhongkeda.list
deb http://mirrors.ustc.edu.cn/debian stable main contrib non-free
deb-src http://mirrors.ustc.edu.cn/debian stable main contrib non-free
deb http://mirrors.ustc.edu.cn/debian stable-proposed-updates main contrib non-free
deb-src http://mirrors.ustc.edu.cn/debian stable-proposed-updates main contrib non-free
创建php镜像
[root@lnmp php]# pwd
/root/lnmp/php
[root@lnmp php]# docker build -t php:test .
给镜像打标签
[root@lnmp ~]# docker tag php:test 192.168.231.32/real/php:test
编写docker-compose.yml
[root@lnmp lnmp]# cat docker-compose.yml
version: '2.2'
services:
nginx:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./nginx/html:/usr/share/nginx/html
- ./nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf
- ./nginx/log:/var/log/nginx/
depends_on: #解决容器的依赖、启动先后的问题
- php
environment:
- TZ=Asia/Shanghai
container_name: "nginx"
networks:
- front
php:
image: 192.168.231.32/real/php:test
volumes:
- ./nginx/html:/usr/share/nginx/html
container_name: "php"
networks:
- front
networks:
front:
修改nginx的配置文件
使nginx和php连接
[root@lnmp lnmp]# cat nginx/conf.d/default.conf
server {
index index.php index.html;
server_name php-docker.local;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /usr/share/nginx/html;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
创建php文件
[root@lnmp lnmp]# cat nginx/html/index.php
重启项目中的服务
[root@lnmp lnmp]# docker-compose up -d
访问测试
编写docker-compose文件
添加
mysql:
image: mysql:5.6
restart: always
ports:
- "3306:3306"
environment:
- TZ=Asia/Shanghai
- MYSQL_ROOT_PASSWORD=123456
- MYSQL_DATABASE=iwebshop
volumes:
- ./mysql/mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf
- ./mysql/data:/var/lib/mysql
container_name: "mysql"
networks:
- front
创建相关文件
[root@lnmp ~]# mkdir lnmp/mysql/data/
[root@lnmp ~]# egrep -v "^$|#" lnmp/mysql/mysqld.cnf
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
log_bin = mysql-bin
binlog_format = mixed
log-error = /var/log/mysql/error.log
symbolic-links=0
创建php文件进行测试
[root@lnmp lnmp]# cat nginx/html/index.php
setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
编排
[root@lnmp lnmp]# docker-compose up -d
查看
[root@lnmp lnmp]# docker-compose ps
Name Command State Ports
----------------------------------------------------------------------
mysql docker-entrypoint.sh mysqld Up 0.0.0.0:3306->3306/tcp
nginx nginx -g daemon off; Up 0.0.0.0:80->80/tcp
php docker-php-entrypoint php-fpm Up 9000/tcp
访问测试
iWebShop包解压
[root@lnmp ~]# unzip iWebShop5.5临时试用版本.zip
[root@lnmp ~]# mv iWebShop5.5/* lnmp/nginx/html/
[root@lnmp ~]# mv upload.zip lnmp/nginx/html/upload
[root@lnmp ~]# unzip lnmp/nginx/html/upload/upload.zip
给其他用户添加写权限
[root@lnmp ~]# chmod o+w lnmp/nginx/html/*
[root@lnmp ~]# chmod o+w lnmp/nginx/html/config/*
浏览器进行安装
Weave Cloud是一种可视化,管理和监控Docker容器的快速而简单的方法,使开发人员能够专注于编写代码。
编辑docker-compose.yml
添加
scope:
image: weaveworks/scope:1.11.2
network_mode: "host" #此时容器使用本地主机的网络,它拥有完全的本地主机接口访问权限。
pid: "host" #跟主机系统共享进程命名空间。
privileged: true #允许容器中运行一些特权命令
labels:
- "works.weave.role=system" #为容器添加 Docker 元数据(metadata)信息
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:rw"
command:
- "--probe.docker=true"
构建镜像并启动容器
[root@lnmp lnmp]# docker-compose up -d
查看
浏览器访问:
ip:4040
监控本机的3个容器,包括其内存、CPU等
总:
version: '2.2'
services:
nginx:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./nginx/html:/usr/share/nginx/html
- ./nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf
- ./nginx/log:/var/log/nginx/
depends_on:
- php
environment:
- TZ=Asia/Shanghai
container_name: "nginx"
networks:
- front
restart: always
php:
image: 192.168.231.32/real/php:test
volumes:
- ./nginx/html:/usr/share/nginx/html
environment:
- TZ=Asia/Shanghai
container_name: "php"
networks:
- front
- back
mysql:
image: mysql:5.6
restart: always
ports:
- "3306:3306"
environment:
- TZ=Asia/Shanghai
- MYSQL_ROOT_PASSWORD=123456
- MYSQL_DATABASE=iwebshop
volumes:
- ./mysql/mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf
- ./mysql/data:/var/lib/mysql
container_name: "mysql"
networks:
- back
scope:
image: weaveworks/scope:1.11.2
network_mode: "host"
pid: "host"
privileged: true
labels:
- "works.weave.role=system"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:rw"
command:
- "--probe.docker=true"
networks:
front:
back: