简单几步即可实现nginx的部署,并在此过程中熟悉docker的一些常用操作~
1 搜索并拉取nginx镜像
# 搜寻nginx镜像
root@master:/home/hqc# docker search nginx
# 拉取镜像
root@master:/home/hqc# docker pull nginx
2 常用到的指令
# 查看镜像
root@master:/home/hqc# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ea335eea17ab 46 hours ago 141MB
hello-world latest feb5d9fea6a5 8 weeks ago 13.3kB
# 此时显示有两个镜像
# 尝试删除不需要的镜像
root@master:/home/hqc# docker image rm hello-world
Error response from daemon: conflict: unable to remove repository reference "hello-world" (must force) - container 0f44d1956fa6 is using its referenced image feb5d9fea6a5
# 删除hello-world镜像时会报错,因为该容器正在运行,须停掉后再删除
# 查看正在运行的所有容器
root@master:/home/hqc# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0f44d1956fa6 hello-world "/hello" 3 hours ago Exited (0) 3 hours ago suspicious_pare
# 进入指定容器
root@master:/home/hqc# docker exec it 0f44d1956fa6 bash
# 或者
root@master:/home/hqc# docker exec it suspicious_pare bash
# 退出该容器
root@0f44d1956fa6:~# exit
# 停止运行的容器
root@master:/home/hqc# docker stop 0f44d1956fa6
0f44d1956fa6
# 删除该容器
root@master:/home/hqc# docker rm 0f44d1956fa6
0f44d1956fa6
# 再次查看容器已删除
root@master:/home/hqc# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# 但查看镜像时hello-world镜像仍然存在
root@master:/home/hqc# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ea335eea17ab 46 hours ago 141MB
hello-world latest feb5d9fea6a5 8 weeks ago 13.3kB
# 删除镜像
root@master:/home/hqc# docker image rm hello-world
Untagged: hello-world:latest
Untagged: hello-world@sha256:cc15c5b292d8525effc0f89cb299f1804f3a725c8d05e158653a563f15e4f685
Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412
Deleted: sha256:e07ee1baac5fae6a26f30cabfe54a36d3402f96afda318fe0a96cec4ca393359
# 此时再次查看hello-world已经没有了
root@master:/home/hqc# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ea335eea17ab 46 hours ago 141MB
3 运行nginx镜像
nginx默认端口为80
# 运行nginx并映射端口为8888 (端口号可以随便设置)
root@master:/home/hqc# docker run -d -p 8888:80 nginx
e2f50bf67d1fcb9d9cd141f601bb423b0f20c3c42415fa097ff3b48d30d08178
# 查看正在运行的容器信息
root@master:/home/hqc# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e2f50bf67d1f nginx "/docker-entrypoint.…" 7 seconds ago Up 5 seconds 0.0.0.0:8888->80/tcp, :::8888->80/tcp competent_sutherland
root@master:/home/hqc# docker start competent_sutherland
# 关闭电脑(服务器)后,容器也将不再运行,可使用此命令直接重启(competent_sutherland为容器名)
root@master:/home/hqc# ifconfig
wlp3s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.27.120.238 netmask 255.255.128.0 broadcast 172.27.127.255
# 查看该网络环境下本机IP为172.27.120.238
4 查看是否部署成功:本机+端口号访问
访问成功!