1、查询nginx镜像选择拉取哪个镜像
[root@wby ~]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 11731 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1630 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 729 [OK]
linuxserver/nginx An Nginx container, brought to you by LinuxS… 70
bitnami/nginx Bitnami nginx Docker Image 69 [OK]
tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp… 49 [OK]
nginx/nginx-ingress NGINX Ingress Controller for Kubernetes 20
nginxdemos/hello NGINX webserver that serves a simple page co… 18 [OK]
jlesage/nginx-proxy-manager Docker container for Nginx Proxy Manager 17 [OK]
schmunk42/nginx-redirect A very simple container to redirect HTTP tra… 17 [OK]
crunchgeek/nginx-pagespeed Nginx with PageSpeed + GEO IP + VTS + more_s… 13
blacklabelops/nginx Dockerized Nginx Reverse Proxy Server. 12 [OK]
centos/nginx-18-centos7 Platform for running nginx 1.8 or building n… 11
centos/nginx-112-centos7 Platform for running nginx 1.12 or building … 9
nginxinc/nginx-unprivileged Unprivileged NGINX Dockerfiles 8
webdevops/nginx Nginx container 8 [OK]
nginx/nginx-prometheus-exporter NGINX Prometheus Exporter 5
sophos/nginx-vts-exporter Simple server that scrapes Nginx vts stats a… 5 [OK]
1science/nginx Nginx Docker images that include Consul Temp… 4 [OK]
mailu/nginx Mailu nginx frontend 3 [OK]
pebbletech/nginx-proxy nginx-proxy sets up a container running ngin… 2 [OK]
travix/nginx NGinx reverse proxy 2 [OK]
wodby/nginx Generic nginx 0 [OK]
ansibleplaybookbundle/nginx-apb An APB to deploy NGINX 0 [OK]
centos/nginx-110-centos7 Platform for running nginx 1.10 or building … 0
[root@wby ~]#
2、下载官方的nginx镜像
[root@wby ~ ]#docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
f7e2b70d04ae: Pull complete
08dd01e3f3ac: Pull complete
d9ef3a1eb792: Pull complete
Digest: sha256:98efe605f61725fd817ea69521b0eeb32bef007af0e3d0aeb6258c6e6fe7fc1a
Status: Downloaded newer image for nginx:latest
[root@wby ~ ]#
3、查看本地镜像列表
[root@wby ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 24c9c444666b 27 hours ago 660MB
<none> <none> 513e9ff8637a 27 hours ago 660MB
docker-demo latest cc4586fa82b4 27 hours ago 660MB
postgres 9.6 ac400042d32f 5 days ago 230MB
nginx latest 98ebf73aba75 5 days ago 109MB
pgbi/kong-dashboard latest f9e2977207e3 4 months ago 96.4MB
kong 0.13.1 e23f12af394a 4 months ago 91.7MB
java 8 d23bdf5b1b1b 2 years ago 643MB
[root@wby ~]#
4、生成容器并运行容器
现在先不挂载本地目录到nginx镜像运行后的容器中,而是先简单运行nginx镜像
下面执行的命令 -p 8081:80 表示将宿主机的8081端口映射到nginx镜像运行后的80端口(nginx配置文件默认是80端口)
-d 表示以后台进程的方式启动。
–name 表示 容器的名称
docker ps 命令展示正在运行中的容器
[root@wby ~]# docker run -d --name mynginx -p 8081:80 nginx
af7636e620549e4a337f83a0393eff281037a0af4145dfe8ffb6e1433cfabb58
[root@wby ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
af7636e62054 nginx "nginx -g 'daemon of…" 20 seconds ago Up 19 seconds 0.0.0.0:8080->80/tcp mynginx
[root@wby ~]#
5、访问nginx首页
6、想要挂载就需要知道挂载所需文件的位置,即进入到nginx容器内部进行查找
1)、进入到容器内部
docker exec -it container_name bash
2)、查看容器的结构目录:其实每一个容器就相当于一个独立的系统。
[root@wby ~]# docker exec -it docker_nginx bash
root@097d59294211:/# ls
bin dev home lib64 mnt proc run srv tmp var
boot etc lib media opt root sbin sys usr
root@097d59294211:/#
3)、nginx的结构目录在容器中如下:
日志位置:/var/log/nginx/
配置文件位置:/etc/nginx/
项目位置:/usr/share/nginx/html
4)、在本地创建所需文件夹以及文件并添加内容
所需文件夹:
/home/wby/nginx/conf
/home/wby/nginx/conf.d
/home/wby/nginx/html
/home/wby/nginx/log
所需文件:
/home/wby/nginx/conf/nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
/home/wby/nginx/conf.d/default.conf
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/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;
}
}
/home/wby/nginx/html/index.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>hello docker!</h1>
</body>
</html>
5)、关闭在运行的nginx容器并重新运行
关闭nginx容器:
[root@wby ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
af7636e62054 nginx "nginx -g 'daemon of…" 3 hours ago Up 3 hours 0.0.0.0:8080->80/tcp mynginx
[root@wby ~]# docker stop af7636e62054
[root@wby ~]#
重新运行nginx容器:
docker run --name mynginx -d -p 8081:80 \
-v /home/wenyugang/nginx/log:/var/log/nginx \
-v /home/wenyugang/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /home/wenyugang/nginx/conf.d:/etc/nginx/conf.d \
-v /home/wenyugang/nginx/html:/usr/share/nginx/html \
nginx
6)、访问nginx首页
可以发现,nginx首页展示的内容已经被改变了,展示的就是前面在宿主机上创建的index.html内容。而且修改宿主机的index.html或者nginx.conf、default.conf,就相当于你直接修改docker中的nginx容器中的文件一样。
验证方法很简单,直接进入nginx容器中查看index.html的文件是否和宿主机的内容一样。
[root@wby ~]# docker exec -it 28e29 bash
root@28e295bef826:/# cat /usr/share/nginx/html/index.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>hello docker!</h1>
</body>
</html>
root@28e295bef826:/#
参考文档
[1]https://blog.csdn.net/wuzhangweiss/article/details/88932691
[2]https://blog.csdn.net/qq_25406669/article/details/88339513