使用Docker,拉取Nginx镜像,创建并运行Nginx容器

时间:2024-03-01 21:00:45

查看DockerHub,拉取Nginx镜像,创建并运行Nginx容器。

  • 在DockerHub中搜索Ningx镜像,查看镜像名称

  • 拉取Nginx镜像

[root@service ~]# systemctl start docker
[root@service ~]# mkdir -p /etc/docker
[root@service ~]# tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://xxxx.mirror.aliyuncs.com"]
}
EOF
{
  "registry-mirrors": ["https://xxxx.mirror.aliyuncs.com"]
}
[root@service ~]# systemctl daemon-reload
[root@service ~]# systemctl restart docker
[root@service ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
a2abf6c4d29d: Pull complete 
a9edb18cadd1: Pull complete 
589b7251471a: Pull complete 
186b1aaa4aa6: Pull complete 
b4df32aa5a72: Pull complete 
a0bcbecc962e: Pull complete 
Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
拉取成功!
  • 查看本地镜像列表

[root@service ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
mysql        latest    ba6b635d3b6b   3 weeks ago   632MB
nginx        latest    605c77e624dd   2 years ago   141MB

 

  • 保存Nginx镜像

[root@service ~]# docker save -o ngnix.tar nginx:latest
[root@service ~]# ll
总用量 142488
-rw-------. 1 root root      1293  9月  7 16:34 anaconda-ks.cfg
-rw-------. 1 root root 145902080  2月 13 21:01 ngnix.tar
  • 删除Nginx镜像

[root@service ~]# docker rmi nginx:latest
Untagged: nginx:latest
Untagged: nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Deleted: sha256:605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85
Deleted: sha256:b625d8e29573fa369e799ca7c5df8b7a902126d2b7cbeb390af59e4b9e1210c5
Deleted: sha256:7850d382fb05e393e211067c5ca0aada2111fcbe550a90fed04d1c634bd31a14
Deleted: sha256:02b80ac2055edd757a996c3d554e6a8906fd3521e14d1227440afd5163a5f1c4
Deleted: sha256:b92aa5824592ecb46e6d169f8e694a99150ccef01a2aabea7b9c02356cdabe7c
Deleted: sha256:780238f18c540007376dd5e904f583896a69fe620876cabc06977a3af4ba4fb5
Deleted: sha256:2edcec3590a4ec7f40cf0743c15d78fb39d8326bc029073b41ef9727da6c851f
  • 重新获取Nginx

[root@service ~]# docker load -i ngnix.tar
2edcec3590a4: Loading layer [==================================================>]  83.86MB/83.86MB
e379e8aedd4d: Loading layer [==================================================>]     62MB/62MB
b8d6e692a25e: Loading layer [==================================================>]  3.072kB/3.072kB
f1db227348d0: Loading layer [==================================================>]  4.096kB/4.096kB
32ce5f6a5106: Loading layer [==================================================>]  3.584kB/3.584kB
d874fd2bc83b: Loading layer [==================================================>]  7.168kB/7.168kB
Loaded image: nginx:latest
[root@service ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
mysql        latest    ba6b635d3b6b   3 weeks ago   632MB
nginx        latest    605c77e624dd   2 years ago   141MB
  • 创建并运行容器

[root@service ~]# docker run -d --name nginx -p 80:80 nginx
daae102abc11d39c7a28dfae3c9642b9ed600d515e6582398a46758b91f9d277
[root@service ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                   CREATED         STATUS         PORTS                               NAMES
daae102abc11   nginx     "/docker-entrypoint.…"   9 seconds ago   Up 8 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   nginx
  • 停止Nginx

[root@service ~]# docker stop nginx
nginx
[root@service ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
  • 再次启动Nginx

[root@service ~]# docker start nginx
nginx
[root@service ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                   CREATED         STATUS        PORTS                               NAMES
daae102abc11   nginx     "/docker-entrypoint.…"   6 minutes ago   Up 1 second   0.0.0.0:80->80/tcp, :::80->80/tcp   nginx
  • 查看Nginx运行日志

[root@service ~]# docker logs nginx
-f :持续查看Nginx运行
  • 进入容器内部

[root@service ~]# docker exec -it nginx bash 
root@daae102abc11:/# 
root@daae102abc11:/# ls
bin  boot  dev	docker-entrypoint.d  docker-entrypoint.sh  etc	home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@daae102abc11:/# exit
exit