2-docker简单使用(运行nginx容器)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ywmack/article/details/78191488
1:看Docker版本信息
docker version

2:搜索可用的Nginx镜像
官方有一个网址专门存放镜像
也可以用命令来搜索可用的镜像
docker search nginx

3:docker命令来下载Nginx镜像
docker pull (类似git命令)
在docker镜像索引网站上,镜像名称都是按照 用户名/镜像名的方式来存储的
有一组比较特殊的镜像 比如nginx的基础镜像,经过官方确认可以直接镜像名来查询到
我们来下载nginx镜像
docker pull nginx
如果是其它用户底下的镜像一定写全称
如:docker pull mack/nginx

4:查看docker里面有多少镜像
docker images
[root@10-1-4-88 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest da5939581ac8 3 weeks ago 108MB
hello-world latest 05a3bd381fc2 3 weeks ago 1.84kB
[root@10-1-4-88 ~]#

5:docker运行nginx镜像
docker run -p 8080:80 --name nginx_web -it nginx /bin/bash
-p 8080:80 是指主机的8080指向的镜像的80端口
--name 容器名称
-it 哪个镜像进入bash环境
[root@10-1-4-88 ~]# docker run -p 8080:80 --name nginx_web -it nginx /bin/bash
启动nginx
root@4b5c52c8adca:/# nginx-------------------(该环境就是容器的bash环境)

6:退出容器
退出容器 Ctrl+p+q

7:访问测试

8:查看运行中的镜像 docker ps
[root@10-1-4-88 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4b5c52c8adca nginx "/bin/bash" 5 minutes ago Up 4 minutes 0.0.0.0:8080->80/tcp nginx_web
[root@10-1-4-88 ~]#
9:查看所有镜像的信息 docker ps -a
-a 参数是运行或未运行的容器都显示出来,
下面的images hello-world 是我测试的时候运行的, 未运行 -a 参数就可以显示
[root@10-1-4-88 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4b5c52c8adca nginx "/bin/bash" 8 minutes ago Up 8 minutes 0.0.0.0:8080->80/tcp nginx_web
b6aa600a318b hello-world "/hello" About an hour ago Exited (0) About an hour ago condescending_snyder

10:查看容器更详细的信息 docker inspect ID (ID可以头几位就可以)
如:
docker inspect 4b5c52c8adca
docker inspect 4b5c
两条命令结果 是一样的

11:终止容器
docker stop [NAME]/[CONTAINER ID]:将容器退出。
docker kill [NAME]/[CONTAINER ID]:强制停止一个容器。

docker stop nginx_web

12:docker stop nginx_web 在次访问Nginx

13:在次docker ps (-a) 看nginx_web是否还在运行
[root@10-1-4-88 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@10-1-4-88 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4b5c52c8adca nginx "/bin/bash" 12 minutes ago Exited (0) 54 seconds ago nginx_web
b6aa600a318b hello-world "/hello" About an hour ago Exited (0) About an hour ago condescending_snyder

14:我们在次启动容器 nginx_web
docker start 4b5c52
进入容器启动nginx
docker exec -it 4b5c52c8adca /bin/bash
[root@10-1-4-88 ~]# docker exec -it 4b5c /bin/bash
root@4b5c52c8adca:/# nginx

15:启动nginx_web 再次访问Nginx


16:Docker rm ID 删除容器 hello-world (nginx 后面还要使用 先删除测试的hello-world容器)
查看所有容器
[root@10-1-4-88 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4b5c52c8adca nginx "/bin/bash" 14 minutes ago Exited (0) 3 minutes ago nginx_web
b6aa600a318b hello-world "/hello" About an hour ago Exited (0) About an hour ago condescending_snyder
删除其中一个
[root@10-1-4-88 ~]# docker rm b6aa600
b6aa600
在次查看所有容器
[root@10-1-4-88 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4b5c52c8adca nginx "/bin/bash" 14 minutes ago Exited (0) 3 minutes ago nginx_web
[root@10-1-4-88 ~]#

一次性全部删除所有容器 (不推荐使用)
docker本身没有提供一次性删除操作,但是可以使用如下命令实现:
docker rm 'docker ps -a -q':-a标志列出所有容器,-q标志只列出容器的ID,然后传递给rm命令,依次删除容器


17:删除镜像 docker rmi imagesid
查询所有镜像
[root@10-1-4-88 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest da5939581ac8 3 weeks ago 108MB
hello-world latest 05a3bd381fc2 3 weeks ago 1.84kB
删除其中一个
[root@10-1-4-88 ~]# docker rmi 05a3
Untagged: hello-world:latest
Untagged: hello-world@sha256:b2ba691d8aac9e5ac3644c0788e3d3823f9e97f757f01d2ddc6eb5458df9d801
Deleted: sha256:05a3bd381fc2470695a35f230afefd7bf978b566253199c4ae5cc96fafa29b37
Deleted: sha256:3a36971a9f14df69f90891bf24dc2b9ed9c2d20959b624eab41bbf126272a023
在次查询所有镜像
[root@10-1-4-88 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest da5939581ac8 3 weeks ago 108MB
[root@10-1-4-88 ~]#

18:修改容器保存 docker commit id
比如我们在容器里面安装了一些软件 。想保存该容器 用该命令docker commit id
查看容器
[root@10-1-4-88 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4b5c52c8adca da5939581ac8 "/bin/bash" 30 minutes ago Up 10 minutes 0.0.0.0:8080->80/tcp nginx_web
提交修改容器
[root@10-1-4-88 ~]# docker commit 4b5c nginx
sha256:1628545933abb78ef12f5e0566395e79173444eeb7cd4e072805e93af88f7a84
查看镜像 latest就是最后提交的怎么本 image id 也会不一样 created 创建时间 在6S以前
[root@10-1-4-88 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 1628545933ab 6 seconds ago 108MB
<none> <none> 1717b3a9873b 53 seconds ago 108MB
nginx <none> da5939581ac8 3 weeks ago 108MB

猜你喜欢

转载自blog.csdn.net/ywmack/article/details/78191488