Docker安装Nginx1:不指定镜像版本,不用数据卷
0.确保有操作权限
确保有权限,自己多个账号玩没权限的话,直接su切换root去做或者sudo执行相关命令。
su
sudo docker images
1.搜索镜像
搜索nginx镜像,docker search是看不了版本号的,建议去网站上去搜索,可以看到版本号。
docker search nginx
[root@localhost /]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 15859 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 2096 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 819 [OK]
jc21/nginx-proxy-manager Docker container for managing Nginx proxy ho… 281
linuxserver/nginx An Nginx container, brought to you by LinuxS… 160
tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp… 145 [OK]
jlesage/nginx-proxy-manager Docker container for Nginx Proxy Manager 144 [OK]
alfg/nginx-rtmp NGINX, nginx-rtmp-module and FFmpeg from sou… 110 [OK]
nginxdemos/hello NGINX webserver that serves a simple page co… 78 [OK]
privatebin/nginx-fpm-alpine PrivateBin running on an Nginx, php-fpm & Al… 60 [OK]
nginx/nginx-ingress NGINX and NGINX Plus Ingress Controllers fo… 57
nginxinc/nginx-unprivileged Unprivileged NGINX Dockerfiles 54
nginxproxy/nginx-proxy Automated Nginx reverse proxy for docker con… 27
staticfloat/nginx-certbot Opinionated setup for automatic TLS certs lo… 25 [OK]
nginx/nginx-prometheus-exporter NGINX Prometheus Exporter for NGINX and NGIN… 22
schmunk42/nginx-redirect A very simple container to redirect HTTP tra… 19 [OK]
centos/nginx-112-centos7 Platform for running nginx 1.12 or building … 16
centos/nginx-18-centos7 Platform for running nginx 1.8 or building n… 13
bitwarden/nginx The Bitwarden nginx web server acting as a r… 11
flashspys/nginx-static Super Lightweight Nginx Image 11 [OK]
webdevops/nginx Nginx container 9 [OK]
mailu/nginx Mailu nginx frontend 9 [OK]
sophos/nginx-vts-exporter Simple server that scrapes Nginx vts stats a… 7 [OK]
ansibleplaybookbundle/nginx-apb An APB to deploy NGINX 3 [OK]
wodby/nginx Generic nginx 1 [OK]
[root@localhost /]#
2.拉取(下载)镜像
docker pull nginx
不指定版本号默认为latest,因此一般先去官网查看版本信息,自己指定一个版本去拉取。
[root@localhost /]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
eff15d958d66: Pull complete
1e5351450a59: Pull complete
2df63e6ce2be: Pull complete
9171c7ae368c: Pull complete
020f975acd28: Pull complete
266f639b35ad: Pull complete
Digest: sha256:097c3a0913d7e3a5b01b6c685a60c03632fc7a2b50bc8e35bcaa3691d788226e
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@localhost /]#
3.查看本地下载的镜像
docker images
[root@localhost /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ea335eea17ab 8 days ago 141MB
[root@localhost /]#
4.启动容器
docker run -d --name nginx01 -p 3301:80 nginx
- -d 表示后台启动
- –name 指定容器名字
- -p port1:port2
- port1宿主机端口,也就是你服务器对外提供(映射)的访问端口
- port2容器内部端口,即nginx容器内的端口,默认是80
docker -p端口暴露
-p的作用就是容器内部端口和容器外部端口进行映射,内外端口有映射才能访问。
如果关闭防火墙,相当于对外暴露所有端口。
5.查看运行中的容器
docker ps
[root@localhost /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
aca793e8d0cc nginx "/docker-entrypoint.…" 7 seconds ago Up 6 seconds 0.0.0.0:3301->80/tcp, :::3301->80/tcp nginx01
[root@localhost /]#
6.运行测试
curl查看访问nginx
curl nginx容器外部映射的端口号:3301
[root@localhost /]# curl localhost:3301
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
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>
[root@localhost /]#
访问对应ip+端口号
7.进入容器
docker exec -it nginx01 /bin/bash
root@aca793e8d0cc:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@aca793e8d0cc:/# cd /etc/nginx
root@aca793e8d0cc:/etc/nginx# ll
bash: ll: command not found
root@aca793e8d0cc:/etc/nginx# ls
conf.d fastcgi_params mime.types modules nginx.conf scgi_params uwsgi_params
root@aca793e8d0cc:/etc/nginx#
8.退出容器,并停止
exit退出容器
conf.d fastcgi_params mime.types modules nginx.conf scgi_params uwsgi_params
root@aca793e8d0cc:/etc/nginx# exit
exit
[root@localhost /]#
停止容器:docker stop 容器name
[root@localhost /]# docker stop nginx01
nginx01
[root@localhost /]#