1. 下载nginx
[[email protected] my.Shells]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/redis latest 1e70071f4af4 6 weeks ago 106.7 MB [[email protected] my.Shells]# docker pull nginx //下载nginx Using default tag: latest Trying to pull repository docker.io/library/nginx ... latest: Pulling from docker.io/library/nginx e7bb522d92ff: Pull complete 6edc05228666: Pull complete cd866a17e81f: Pull complete Digest: sha256:285b49d42c703fdf257d1e2422765c4ba9d3e37768d6ea83d7fe2043dad6e63d [[email protected] my.Shells]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/nginx latest 3f8a4339aadd 3 weeks ago 108.5 MB docker.io/redis latest 1e70071f4af4 6 weeks ago 106.7 MB
2. 运行
[[email protected] my.Shells]# docker run -p 8080:80 -d docker.io/nginx //将80端口映射为8080,或者80:80还是原先的80端口,不可以不写。 c0462d5e18783e20f9515108fa62ab0f2ac808ea85370a7c82aee9407abf4672 [[email protected] my.Shells]# netstat -anp | grep 8080 //端口已经开启了 tcp6 0 0 :::8080 :::* LISTEN 2529/docker-proxy-c [[email protected] my.Shells]# docker ps //nginx已经在运行了 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c0462d5e1878 docker.io/nginx "nginx -g ‘daemon off" 4 minutes ago Up 4 minutes 0.0.0.0:8080->80/tcp angry_mccarthy
3. 运行结果
[[email protected] my.Shells]# ./openFirewallPort.sh //先在防火墙上开一个端口 enter the port: 8080 success
---openFirewallPort.sh-------
echo "enter the port: "
read port
firewall-cmd --add-port=$port/tcp
下图已经成功访问到了
注意: 当docker运行nginx时,外界访问还是docker所在的那个IP地址,就相当于nginx在那台机器上运行一样。 但对于docker所在的那台机器来说,nginx就是附属于docker的一个镜像。若操作nginx还是由docker登录nginx容器,进行操作。 登录的nginx容器就是一个linux系统,只不过只有nginx而已,nginx按照linux默认路径安装。比如 [email protected]:/usr/share/nginx/html# ls 这个路径就是默认的静态页面存放路径 50x.html index.html bash命令都一样,但是vi在我机器上是不能用的,但可以使用cp、mv 等命令,因为nginx都是配置好的,不能乱改。
1)可以通过在还未登录nignx容器前,把需要的文件写好,然后复制到指定目录下: [[email protected] my.Shells]# docker cp hello.html c0462d5e1878://usr/share/nginx/html [[email protected] my.Shells]# docker exec -it c0462d5e1878 bash [email protected]:/usr/share/nginx/html# ls 50x.html hello.html index.html
2)
2)通过主机目录映射到容器
docker run -p 80:80 -d -v $PWD/html:usr/share/nginx/html docker.io/nginx
-v $PWD/html:usr/share/nginx/html 表示把当前路径下html目录映射为usr/share/nginx/html
也就是说主机下的html就是容器下的usr/share/nginx/html
html内的文件修改和添加就等同于容器usr/share/nginx/html文件操作
外网访问就可以访问得到,就不用再登录容器操作文件了
4. 停止服务
[[email protected] my.Shells]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c0462d5e1878 docker.io/nginx "nginx -g ‘daemon off" 56 minutes ago Up 56 minutes 0.0.0.0:8080->80/tcp angry_mccarthy [[email protected] my.Shells]# docker stop c0462d5e1878 c0462d5e1878 [[email protected] my.Shells]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5. 重启服务
[[email protected] my.Shells]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [[email protected] my.Shells]# docker start c0462d5e1878 c0462d5e1878 [[email protected] my.Shells]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c0462d5e1878 docker.io/nginx "nginx -g ‘daemon off" 59 minutes ago Up 12 seconds 0.0.0.0:8080->80/tcp angry_mccarthy
6. 再开启一个相同的服务
[[email protected] my.Shells]# docker run -p 8081:80 -d docker.io/nginx //再开启一个服务,端口为8081 1fd8a0b5d138203150f1cdbfb9690235159159881785a4654abb04c7c96c5b18 [[email protected] my.Shells]# docker ps //会有两个进程,一个8080,一个8081 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1fd8a0b5d138 docker.io/nginx "nginx -g ‘daemon off" 4 seconds ago Up 3 seconds 0.0.0.0:8081->80/tcp suspicious_hypatia c0462d5e1878 docker.io/nginx "nginx -g ‘daemon off" About an hour ago Up 4 minutes 0.0.0.0:8080->80/tcp angry_mccarthy
上图访问的是新开启的8081服务
注意:新启动的服务和原先的服务是两个容器,原先的hello.html在新服务中是没有的
7. 卸载服务
[[email protected] my.Shells]# docker ps //此时8080和8081都在运行 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1fd8a0b5d138 docker.io/nginx "nginx -g ‘daemon off" 4 minutes ago Up 4 minutes 0.0.0.0:8081->80/tcp suspicious_hypatia c0462d5e1878 docker.io/nginx "nginx -g ‘daemon off" About an hour ago Up 8 minutes 0.0.0.0:8080->80/tcp angry_mccarthy [[email protected] my.Shells]# docker stop 1fd8a0b5d138 //停下8081 1fd8a0b5d138 [[email protected] my.Shells]# docker ps //就剩8080还在运行 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c0462d5e1878 docker.io/nginx "nginx -g ‘daemon off" About an hour ago Up 9 minutes 0.0.0.0:8080->80/tcp angry_mccarthy [[email protected] my.Shells]# docker ps -a //可以看到8080在运行,8081已经Exited CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1fd8a0b5d138 docker.io/nginx "nginx -g ‘daemon off" 5 minutes ago Exited (0) 7 seconds ago suspicious_hypatia c0462d5e1878 docker.io/nginx "nginx -g ‘daemon off" About an hour ago Up 9 minutes 0.0.0.0:8080->80/tcp angry_mccarthy [[email protected] my.Shells]# [[email protected] my.Shells]# docker rm 1fd8a0b5d138 //移除这个进程进行了,注意运作着的进程是无法rm的,要先stop 1fd8a0b5d138