1.以centos7作为基础镜像
2.安装好ip,vim,ping命令
3.编译安装nginx(编写脚本)
4.使用指定的配置文件nginx.conf
5.启动容器的时候启动nginx
6.网站的网页上传到容器里:
1.直接做到镜像里
2.使用数据卷挂载使用
/usr/local/nginx1/conf
需要哪些东西?
1.nginx的安装脚本 install_nginx.sh -->源码
https://nginx.org/download/nginx-1.23.1.tar.gz
2.Dockerfile3.编译安装nginx的配置文件
第一步:新建文件夹nginx
[root@manger mynginx]# mkdir nginx
[root@manger mynginx]# cd nginx/
第二步:下载nginx源码包
[root@manger nginx]# wget https://nginx.org/download/nginx-1.23.1.tar.gz
--2023-07-11 16:04:19-- https://nginx.org/download/nginx-1.23.1.tar.gz
正在解析主机 nginx.org (nginx.org)... 3.125.197.172, 52.58.199.22, 2a05:d014:edb:5702::6, ...
正在连接 nginx.org (nginx.org)|3.125.197.172|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:1104352 (1.1M) [application/octet-stream]
正在保存至: “nginx-1.23.1.tar.gz”
100%[===============================================================>] 1,104,352 4.67KB/s 用时 2m 23s
2023-07-11 16:06:45 (7.52 KB/s) - 已保存 “nginx-1.23.1.tar.gz” [1104352/1104352])
[root@manger nginx]# ls
nginx-1.23.1.tar.gz
第三步:编写一键安装nginx的脚本
[root@manger nginx]# cat install_nginx.sh
#!/bin/bash
#解决依赖关系
yum install -y zlib zlib-devel openssl openssl-devel pcre pcre-devel gcc gcc-c++ autoconf automake make
#新建/nginx目录
mkdir -p /nginx
#进入/nginx
cd /nginx
#解压下载的nginx源码包
tar xf nginx-1.23.1.tar.gz
cd nginx-1.23.1
#编译前配置
./configure --prefix=/usr/local/nginx1 --with-threads --with-http_ssl_module --with-http_stub_status_module --with-stream
#编译
make
#编译安装
make install
第四步:编写Dockerfile
[root@manger nginx]# cat Dockerfile
FROM centos:7
ENV NGINX_VERSION 1.23.1
ENV AUTHOR jack
ENV PATH=/usr/local/nginx1/sbin:$PATH
LABEL maintainer="jack<[email protected]>"
RUN mkdir /nginx
COPY . /nginx
WORKDIR /nginx
RUN set -ex; \
bash install_nginx.sh; \
yum install vim iputils net-tools iproute -y
EXPOSE 80
STOPSIGNAL SIGQUIT
CMD ["nginx","-g","daemon off;"]
[root@manger nginx]# ls
Dockerfile install_nginx.sh nginx-1.23.1.tar.gz
第五步:制作镜像
[root@manger nginx]# docker build -t sancnginx:1.0 .
[+] Building 136.0s (10/10) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 376B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/centos:7 4.6s
=> [1/5] FROM docker.io/library/centos:7@sha256:be65f488b7764ad3638f236b7b515b3678369a5124c47b8d3 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 580B 0.0s
=> CACHED [2/5] RUN mkdir /nginx 0.0s
=> [3/5] COPY . /nginx 0.0s
=> [4/5] WORKDIR /nginx 0.0s
=> [5/5] RUN set -ex; bash /nginx/install_nginx.sh; yum install vim iputils net-tools iproute 129.2s
=> exporting to image 2.1s
=> => exporting layers 2.1s
=> => writing image sha256:cdd45d93df0329061058c3ad763e0067c61b65d6641c2cbfcd11c4a1ae67cda7 0.0s
=> => naming to docker.io/library/sancnginx:1.0 0.0s
[root@manger nginx]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
sancnginx 1.0 cdd45d93df03 27 seconds ago 627MB
第六步:使用制作的镜像启动一个容器
[root@manger nginx]# docker run -d -p 4477:80 --name sanc-nginx-1 sancnginx:1.0
d7cb44d1db8c8245fdfc14d9d060ca274527b28ebe5bc63d2231e5a05f199eae
[root@manger nginx]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d7cb44d1db8c sancnginx:1.0 "nginx -g 'daemon of…" 2 seconds ago Up 1 second 0.0.0.0:4477->80/tcp, :::4477->80/tcp sanc-nginx-1
第七步:测试访问
第八步:把编译安装nginx的配置文件从容器里拷贝出来
[root@manger nginx]# docker cp sanc-nginx-1:/usr/local/nginx1/conf/nginx.conf .
Successfully copied 4.61kB to /mynginx/nignx/.
[root@manger nginx]# ls
Dockerfile install_nginx.sh nginx-1.23.1.tar.gz nginx.conf
第九步:修改nginx.conf配置文件;修改Dockerfile
[root@manger nginx]# vim nginx.conf
worker_processes 2;
events {
worker_connections 4096;
}
server {
listen 8080;
server_name localhost;
[root@manger nginx]# cat Dockerfile
FROM centos:7
ENV NGINX_VERSION 1.23.1
ENV AUTHOR jack
ENV PATH=/usr/local/nginx1/sbin:$PATH
LABEL maintainer="jack<[email protected]>"
RUN mkdir /nginx
COPY . /nginx
WORKDIR /nginx
RUN set -ex; \
bash install_nginx.sh; \
yum install vim iputils net-tools iproute -y
EXPOSE 8080
COPY ./nginx.conf /usr/local/nginx1/conf
STOPSIGNAL SIGQUIT
CMD ["nginx","-g","daemon off;"]
第十步:重新制作镜像
[root@manger nginx]# docker build -t sancnginx:1.1 .
[+] Building 246.0s (10/10) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 378B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/centos:7 1.8s
=> [1/5] FROM docker.io/library/centos:7@sha256:be65f488b7764ad3638f236b7b515b3678369a5124c47b8d3 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 15.49kB 0.0s
=> CACHED [2/5] RUN mkdir /nginx 0.0s
=> [3/5] COPY . /nginx 0.0s
=> [4/5] WORKDIR /nginx 0.0s
=> [5/5] RUN set -ex; bash /nginx/install_nginx.sh; yum install vim iputils net-tools iproute 241.9s
=> exporting to image 2.3s
=> => exporting layers 2.3s
=> => writing image sha256:099da6fe30d76ec400ebe70e1d38479ea4ed52757d4f8b89ed99dfe24705ea17 0.0s
=> => naming to docker.io/library/sancnginx:1.1 0.0s
[root@manger nginx]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
sancnginx 1.1 099da6fe30d7 About a minute ago 627MB
第十一步:创建卷
[root@manger nginx]# docker volume create sc
sc
[root@manger nginx]# docker volume ls
DRIVER VOLUME NAME
local sc
local test-vol
[root@manger nginx]# docker volume inspect sc
[
{
"CreatedAt": "2023-07-11T17:32:36+08:00",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/sc/_data",
"Name": "sc",
"Options": null,
"Scope": "local"
}
]
[root@manger nginx]# cd /var/lib/docker/volumes/sc/_data/
[root@manger _data]# echo "hello world,welcome to changshasanchuang" >>index.html
[root@manger _data]# ls
index.html
[root@manger _data]# cat index.html
hello world,welcome to changshasanchuang
第十二步:使用新制作的镜像启动容器并且使用卷挂载
[root@manger nginx]# docker run -d --name sanc-nginx-2 -p 3388:8080 --mount source=sc,target=/usr/local/nginx1/html sancnginx:1.1
0796781cfcd6bc20e548b3c79cf1d61bec1f4b83f678738c4a7480fd19c3b146
[root@manger nginx]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0796781cfcd6 sancnginx:1.2 "nginx -g 'daemon of…" 6 seconds ago Up 5 seconds 0.0.0.0:3388->8080/tcp, :::3388->8080/tcp sanc-nginx-2
第十三步:测试访问
版权归原作者 韩未零 所有, 如有侵权,请联系我们删除。