一、 项目描述
使用Docker容器基于centos镜像分别制作nginx镜像,mysql镜像和php镜像使用编译安装的方式,最后通过镜像启动成容器时使用container模式网络模式并访问到php测试页面
二、 Nginx镜像制作
//拉取centos镜像
[root@Docker ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
a1d0c7532777: Pull complete
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5d0da3dc9764 2 months ago 231MB
// 运行centos镜像
[root@localhost ~]# docker run -it --name nginx 5d0da3dc9764 /bin/bash
[root@03ca6bdc0374 /]#
// 传nginx安装包到容器中
[root@localhost ~]# docker cp /usr/src/nginx-1.20.1.tar.gz 03ca6bdc0374:/usr/src/
// 创建nginx账户
[root@03ca6bdc0374 /]# useradd -r -M -s /sbin/nologin nginx
// 安装依赖包
[root@03ca6bdc0374 /]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make
// 创建nginx日志存放目录
[root@03ca6bdc0374 /]# mkdir -p /var/log/nginx
[root@03ca6bdc0374 /]# chown -R nginx.nginx /var/log/nginx/
// 解压nginx包进行编译安装
[root@03ca6bdc0374 /]# cd /usr/src/
[root@03ca6bdc0374 src]# ls
debug kernels nginx-1.20.1.tar.gz
[root@03ca6bdc0374 src]# tar xf nginx-1.20.1.tar.gz
[root@03ca6bdc0374 src]# cd nginx-1.20.1
[root@03ca6bdc0374 nginx-1.20.1]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log
[root@03ca6bdc0374 nginx-1.20.1]# make && make install
// 设置环境变量
[root@03ca6bdc0374 nginx-1.20.1]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@03ca6bdc0374 nginx-1.20.1]# source /etc/profile.d/nginx.sh
// 查看监听端口
[root@03ca6bdc0374 nginx-1.20.1]# nginx
[root@03ca6bdc0374 nginx-1.20.1]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
[root@localhost ~]# curl 172.17.0.2
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a