引言
- Docker 系列文章
- Docker官方镜像 https://hub.docker.com/
一、安装 nginx
1.安装环境
- Linux 环境:centos 7
- docker 版本:26.1.0
- nginx版本:latest
2. 离线镜像准备
(1)外网环境 拉取 nginx镜像
bash
#安装最新版本
docker pull nginx:latest
(2)导出镜像压缩文件
bash
docker save nginx:latest -o D:\developsoft\docker\DockerDesktopWSL\data\images\nginx.tar
3. 上传 镜像压缩文件到 服务器
本文将 nginx.tar 放在 /root 目录下
4. 离线镜像文件导入
bash
#导入镜像
docker load -i /root/nginx.tar
#查询镜像
docker images
5.创建并运行容器 nginx-container
bash
docker run --name nginx-container -d -p 80:80 nginx
参数说明:
docker run #运行 Docker 容器的命令。
--name nginx-container #为容器指定名称为 nginx-container。
-d #参数表示以"后台模式"运行容器。
-p 80:80#用于将容器的端口映射到宿主机,使得可以通过宿主机的80 端口访问
6. 复制容器的的配置文件到宿主机器 (配置文件映射)
- 创建nginx中数据存放文件夹
bash
mkdir -p /home/docker_data/nginx # -p 参数 自动创建不存在的目录
- 复制容器的的配置文件到宿主机
bash
docker cp nginx-container:/etc/nginx /home/docker_data/nginx/conf
docker cp nginx-container:/usr/share/nginx/html /home/docker_data/nginx/html
docker cp nginx-container:/var/log/nginx /home/docker_data/nginx/logs
7.修改配置文件 nginx.conf
注意修改 静态资源路径为 /usr/share/nginx/html
bash
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location = /50x.html {
root /usr/share/nginx/html;
}
完整 nginx 默认配置如下:
bash
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
8. 查看测试页面
在 /home/docker_data/nginx/html 查看是否存在 index.html 和 50x.html 文件,如果不存在,则添加测试页面
index.html 内容如下
html
<!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 href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
50x.html 内容如下
html
<!DOCTYPE html>
<html>
<head>
<title>Error</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>An error occurred.</h1>
<p>Sorry, the page you are looking for is currently unavailable.<br/>
Please try again later.</p>
<p>If you are the system administrator of this resource then you should check
the <a href="http://nginx.org/r/error_log">error log</a> for details.</p>
<p><em>Faithfully yours, nginx.</em></p>
</body>
</html>
9.删除nginx容器,并通过复制出的配置文件重新启动
bash
# 停止 docker 容器
docker stop nginx
# 删除 docker 容器
docker rm -f nginx
以复制出的配置文件重新启动
bash
docker run --restart=always --name=nginx-container -p 80:80\
-v /home/docker_data/nginx/conf:/etc/nginx \
-v /home/docker_data/nginx/html:/usr/share/nginx/html \
-v /home/docker_data/nginx/logs:/var/log/nginx \
-d nginx
查看 容器 运行状态
bash
docker ps
10. 进入容器查看配置命令
- 使用 docker exec 命令加上 -it 选项(-i 代表交互模式,-t 分配一个伪终端)以及你想要执行的命令(在这种情况下是 bash)来进入容器。
bash
docker exec -it nginx-container bash
- 如果你的容器没有名称,但你知道它的ID(比如 abcd12345678),你也可以使用ID来代替名称:
bash
docker exec -it abcd12345678 bash
11.访问测试
浏览器访问地址:http://127.0.0.1:80