-----------------安装 Docker---------------------------
目前 Docker 只能支持 64 位系统。
systemctl stop firewalld.service
setenforce 0
#安装依赖包
yum install -y yum-utils device-mapper-persistent-data lvm2
--------------------------------------------------------------------------------------------
yum-utils:提供了 yum-config-manager 工具。
device mapper: 是Linux内核中支持逻辑卷管理的通用设备映射机制,它为实现用于存储资源管理的块设备驱动提供了一个高度模块化的内核架构。
device mapper存储驱动程序需要 device-mapper-persistent-data 和 lvm2。
--------------------------------------------------------------------------------------------
#设置阿里云镜像源
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
#安装 Docker-CE并设置为开机自动启动
yum install -y docker-ce docker-ce-cli containerd.io
systemctl start docker.service
systemctl enable docker.service
--------------------------------------------------------------------------------------------
安装好的Docker系统有两个程序,Docker服务端和Docker客户端。其中Docker服务端是一个服务进程,负责管理所有容器。 Docker客户端则扮演着Docker服务端的远程控制器,可以用来控制Docker的服务端进程。大部分情况下Docker服务端和客户端运行在一台机器上。
--------------------------------------------------------------------------------------------
#查看 docker 版本信息
docker version
docker info
浏览器访问 https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors 获取镜像加速器配置
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://xxxxxx.aliyuncs.com"]
}
EOF
systemctl daemon-reload
systemctl restart docker
#获取镜像
格式:docker pull 仓库名称[:标签]
#如果下载镜像时不指定标签,则默认会下载仓库中最新版本的镜像,即选择标签为 latest 标签。
docker pull centos:7
#创建Docker网络:mynetwork 子网:172.18.0.0/16 选项:com.docker.network.bridge.name=docker1
docker network create --subnet=172.18.0.0/16 --opt "com.docker.network.bridge.name"="docker1" mynetwork
mkdir /opt/nginx
cd /opt/nginx/
上传 nginx-1.12.0.tar.gz、wordpress-4.9.4-zh_CN.tar.gz 、 nginx.conf 到 /opt/nginx/ 目录中
mkdir /opt/nginx/html
tar zxvf wordpress-4.9.4-zh_CN.tar.gz -C /opt/nginx/html
vim Dockerfile
FROM centos:7
MAINTAINER this is nginx image <wl>
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;" ]
docker build -t nginx:lnmp .
启动镜像:
docker run -d --name nginx -p 80:80 -v /opt/nginx/html:/usr/local/nginx/html --net mynetwork --ip 172.18.0.10 nginx:lnmp
#-d在后台启动一个名为"nginx"的Docker容器。它将主机的80端口映射到容器的80端口(-p 80:80)。它还将主机上的目录/opt/nginx/html挂载到容器中的/usr/local/nginx/html目录(-v /opt/nginx/html:/usr/local/nginx/html)。容器连接到名为"mynetwork"的网络(--net mynetwork),并分配IP地址172.18.0.10(--ip 172.18.0.10)。容器基于镜像"nginx:lnmp"。
mkdir /opt/mysqld
cd /opt/mysqld
上传 mysql-boost-5.7.20.tar.gz 到 /opt/mysqld 目录中
vim Dockerfile
FROM centos:7
MAINTAINER this is mysql image <MY>
RUN yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake make
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 && make install
ADD my.cnf /etc/
ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
RUN useradd -M -s /sbin/nologin mysql
RUN chown mysql:mysql /etc/my.cnf
RUN chown -R mysql:mysql /usr/local/mysql/
RUN /usr/local/mysql/bin/mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
EXPOSE 3306
CMD ["/usr/local/mysql/bin/mysqld"]
[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
构建mysql:lnmp镜像
docker build -t mysql:lnmp .
docker run --name=mysql -d --privileged -v /usr/local/mysql --net mynetwork --ip 172.18.0.20 mysql:lnmp
mkdir /opt/php
cd /opt/php
上传 php-7.1.10.tar.bz2 、www.conf、php-fpm.conf、php.ini到 /opt/php 目录中
vim Dockerfile
FROM centos:7
MAINTAINER this is php image <wl>
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 build -t php:lnmp .
docker run --name=php -d -p 9000:9000 --volumes-from mysql --volumes-from nginx --net mynetwork --ip 172.18.0.30 php:lnmp
docker exec -it mysql /bin/bash
mysql
create database wordpress;
grant all privileges on wordpress.* to 'wordpress'@'%' identified by '123456';
grant all privileges on *.* to 'root'@'%' identified by 'abc123';
flush privileges;
http://20.0.0.106/wordpress/index.php
注意如果登不上,讲wp-config.php 配置文件写入用户名 及其密码,随后重启nginx容器
二、使用docker-compose 搭建lnmp
Docker Compose 是 Docker 的独立产品,因此需要安装 Docker 之后在单独安装 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
或者直接将compose的包放入 /usr/local/bin/下
#安装
chmod +x /usr/local/bin/docker-compose
#查看版本
docker-compose --version
准备依赖文件:
mkdir /opt/compose_lnmp/
cd /compose_lnmp
nginx mysql php
同上:在相应的目录内编写Dockerfile文件!!
写完相对应的Dockerfile文件后编写docker-compose.yml文件
#定义docker-compose版本,可以是2 或 3
version: '3'
#使用services定义服务
services:
#第一个service
nginx:
#设置容器名
container_name: nginx
#使用dockerfile来构建镜像
build:
#指定Dockerfile文件所在位置
context: ./nginx
#指定文件名
dockerfile: Dockerfile
#映射端口
ports:
- 80:80
- 443:443
#设置数据卷挂载
volumes:
- ./nginx/html:/usr/local/nginx/html
#加入网络
networks:
lnmp:
ipv4_address: 172.18.0.10
mysql:
container_name: mysql
build:
context: ./mysql
dockerfile: Dockerfile
ports:
- 3306:3306
networks:
lnmp:
ipv4_address: 172.18.0.20
volumes:
- db-data:/usr/local/mysql
privileged: true
php:
container_name: php
build:
context: ./php
dockerfile: Dockerfile
ports:
- 9000:9000
networks:
lnmp:
ipv4_address: 172.18.0.30
volumes:
- db-data:/usr/local/mysql
- ./nginx/html:/usr/local/nginx/html
depends_on:
- nginx
- mysql
#设置网络为自定义网络
networks:
lnmp:
driver: bridge
ipam:
config:
- subnet: 172.18.0.0/16
volumes:
db-data:
最后在后台启动 Docker Compose 项目:
docker-compose -f docker-compose.yml up -d
随后进入mysql 容器:
docker exec -it mysql /bin/bash
mysql
create database wordpress;
grant all privileges on wordpress.* to 'wordpress'@'%' identified by '123456';
grant all privileges on *.* to 'root'@'%' identified by 'abc123';
flush privileges;
浏览器访问:
http://20.0.0.106/wordpress/index.php
cd /opt/compose_nginx/nginx/html/wordpress
vim wp-config.php