docker LNMP安装流程
Linux安装Docker
# 安装依赖包 yum install -y yum-utils # 添加Docker软件包源 yum-config-manager \ --add-repo \ https://download.docker.com/linux/centos/docker-ce.repo # 安装Docker CE yum install -y docker-ce # 启动Docker服务并设置开机启动 systemctl start docker systemctl enable docker
镜像从哪里来?
Docker Hub是由Docker公司负责维护的公共镜像仓库,包含大量的容器镜像,Docker工具默认从这个公共镜像库下载镜
像。
地址:https://hub.docker.com
配置镜像加速器:
vi /etc/docker/daemon.json
{
"registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"]
}
一. # 案例 容器化搭建个人博客
1.1 自定义网络 实现内部通信
docker network create lnmp
1.2构建PHP容器 PHP Dockerfile
FROM centos:7 MAINTAINER www.ctnrs.com RUN yum install epel-release -y && \ yum install -y gcc gcc-c++ make gd-devel libxml2-devel \ libcurl-devel libjpeg-devel libpng-devel openssl-devel \ libmcrypt-devel libxslt-devel libtidy-devel autoconf \ iproute net-tools telnet wget curl && \ yum clean all && \ rm -rf /var/cache/yum/* ADD php-5.6.36.tar.gz / RUN cd php-5.6.36 && \ ./configure --prefix=/usr/local/php \ --with-config-file-path=/usr/local/php/etc \ --enable-fpm --enable-opcache \ --with-mysql --with-mysqli --with-pdo-mysql \ --with-openssl --with-zlib --with-curl --with-gd \ --with-jpeg-dir --with-png-dir --with-freetype-dir \ --enable-mbstring --with-mcrypt --enable-hash && \ make -j 4 && make install && \ cp php.ini-production /usr/local/php/etc/php.ini && \ cp sapi/fpm/php-fpm.conf /usr/local/php/etc/php-fpm.conf && \ sed -i "90a \daemonize = no" /usr/local/php/etc/php-fpm.conf && \ mkdir /usr/local/php/log && \ cd / && rm -rf php* && \ ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime ENV PATH $PATH:/usr/local/php/sbin COPY php.ini /usr/local/php/etc/ COPY php-fpm.conf /usr/local/php/etc/ WORKDIR /usr/local/php EXPOSE 9000 CMD ["php-fpm"]
1.3构建nginx Dockerfile
FROM centos:7 LABEL maintainer www.ctnrs.com RUN yum install -y gcc gcc-c++ make \ openssl-devel pcre-devel gd-devel \ iproute net-tools telnet wget curl && \ yum clean all && \ rm -rf /var/cache/yum/* ADD nginx-1.15.5.tar.gz / RUN cd nginx-1.15.5 && \ ./configure --prefix=/usr/local/nginx \ --with-http_ssl_module \ --with-http_stub_status_module && \ make -j 4 && make install && \ mkdir /usr/local/nginx/conf/vhost && \ cd / && rm -rf nginx* && \ ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime ENV PATH $PATH:/usr/local/nginx/sbin COPY nginx.conf /usr/local/nginx/conf/nginx.conf WORKDIR /usr/local/nginx EXPOSE 80 CMD ["nginx", "-g", "daemon off;"]
1.4构建php基础镜像
root@localhost php]# docker build -t php:v1 . 构建php基础镜像
1.5构建Nginx基础镜像
docker build -t nginx:v1 .
1.6创建mysql容器
docker run -d --name lnmp_mysql --net lnmp --mount src=mysql-vol,dst=/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -e MYSQL_DATABASE=wordpress mysql:5.7 --character-set-server=utf8
1.7创建php容器
docker run -d --name lnmp_php --net lnmp \ --mount src=wwwroot,dst=/wwwroot php:v1
1.8创建Nginx容器
docker run -d --name lnmp_nginx --net lnmp -p 88:80 --mount src=wwwroot,dst=/wwwroot \ --mount type=bind,src=$PWD/php.conf,dst=/usr/local/nginx/conf/vhost/php.conf nginx:v1
1.9验证php是否可用
vim /var/lib/docker/volumes/wwwroot/_data/index.php <?php phpinfo()?> http://192.168.1.120:88/ 网页打开发现已经可以识别php页面
2.0博客搭建
tar -xvf wordpress-4.9.4-zh_CN.tar.gz -C /var/lib/docker/volumes/wwwroot/_data/ mv wordpress/* /var/lib/docker/volumes/wwwroot/_data http://192.168.1.120:88 页面就可以打开操作了
vi wp-config.php
二. Docker安装harbor
上传包并解压 tar -xvf harbor-offline-installer-v2.0.0.tgz [root@localhost dockerfile]# mv docker-compose-Linux-x86_64 /usr/bin/docker-compost cd harnog [root@localhost harbor]# cp harbor.yml.tmpl harbor.yml [root@localhost harbor]# chmod +x /usr/bin/docker-compost [root@localhost harbor]# ./prepare [root@localhost harbor]# ./install.sh mkdir /root/ssl/ cd /root/ssl/ [root@localhost ssl]# sh cfssl.sh [root@localhost ssl]# sh certs.sh vim harbor.yml hostname: reg.ctnrs.com http: port: 81 https: port: 443 certificate: /root/ssl/reg.ctnrs.com.pem private_key: /root/ssl/reg.ctnrs.com-key.pem scp /root/ssl/reg.ctnrs.com.pem 192.168.1.121:/etc/docker/certs.d/reg.ctnrs.comreg.ctnrs.com.crt vim /etc/hosts/ 192.168.1.120 reg.ctnrs.com [root@localhost reg.ctnrs.com]# docker login reg.ctnrs.com admin Harbor12345 登陆成功 打标签 并上传镜像 一定注意都是域名 因为是以域名签的证书 [root@localhost reg.ctnrs.com]# docker tag busybox reg.ctnrs.com/library/busybox:v1 [root@localhost reg.ctnrs.com]# docker push reg.ctnrs.com/library/busybox:v1 默认Harboy 数据目录在/data/ 日志在 cat /var/log/harbor/
docker run -d --name lnmp_mysql --net lnmp --mount src=mysql-vol,dst=/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -e MYSQL_DATABASE=wordpress mysql:5.7 --character-set-server=utf8MYSQL_DATABASE=wordpress mysql:5.7 --character-set-server=utf8