docker镜像保存、导入和docker的操作(运行,停止,删除,进入和查看日志等操作)

#第一种方法

#导出
docker save -o centos.tar centos
#导入
docker load --input centos.tar

#第二种方法

######################################################################################

#-d后台启动,如果没有这个镜像,会自动帮你pull

#-t分配个伪终端,可以登录进去;-i 伪终端可以保持打开状态

 

 

 

[root@centos7-5-01 ~]# docker run --name mydocker -i -t centos /bin/bash

 

#-d启动容器,让他在后台运行,如果你本机没有nginx镜像,他会自动去获取nginx镜像

 [root@centos7-5-01 ~]# docker run -d --name mynginx nginx

Unable to find image 'nginx:latest' locally

latest: Pulling from nginx

1cb018da208f: Pull complete

c1a0d2b79b3f: Pull complete

0aec54b378f5: Pull complete

b599d18a520b: Pull complete

a1cfc1b806a4: Pull complete

c2504687d157: Pull complete

a00416541f84: Pull complete

6adf11c406b6: Pull complete

9daddd1b8b0f: Pull complete

036477bc0d5a: Pull complete

Digest: sha256:248f3c1a01b35a098c85b31c356787068b1c1adbbd2f902fb2d6f49b95fd380f

Status: Downloaded newer image for nginx:latest

0c0c2a5e9f665e0a662177ce3d52a7907346ea66ab40a53d66e067511f7ca5a9

 

#查看运行的容器

[root@centos7-5-01 ~]# docker ps -a

CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS                       PORTS               NAMES

0c0c2a5e9f66        nginx               "nginx -g 'daemon of   7 seconds ago       Up 7 seconds                 80/tcp              mynginx            

481b2fa0cf72        centos              "/bin/bash"            2 minutes ago       Exited (0) 2 minutes ago                         mydocker2          

0fd35d91742b        centos              "/bin/bash"            7 minutes ago       Exited (127) 6 minutes ago                       mydocker 

#删除运行的容器

[root@centos7-5-01 ~]# docker rm mydocker

mydocker    

[root@centos7-5-01 ~]# docker ps -a

CONTAINER ID        IMAGE               COMMAND                CREATED              STATUS                     PORTS               NAMES

0c0c2a5e9f66        nginx               "nginx -g 'daemon of   About a minute ago   Up About a minute          80/tcp              mynginx            

481b2fa0cf72        centos              "/bin/bash"            3 minutes ago        Exited (0) 3 minutes ago                       mydocker2       

#停止运行的容器  

[root@centos7-5-01 ~]# docker stop 0c0c2a5e9f66

0c0c2a5e9f66

[root@m68en-s000 game]# docker ps -a

CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS                      PORTS               NAMES

0c0c2a5e9f66        nginx               "nginx -g 'daemon of   2 minutes ago       Exited (0) 29 seconds ago                       mynginx            

481b2fa0cf72        centos              "/bin/bash"            4 minutes ago       Exited (0) 4 minutes ago                        mydocker2          

 

######################进入运行中的容器############################# 

#这个进去容器的命令不好用,有的时候进不去,只能Ctrl+C强制终止,这样容器的状态会自动退出了

[root@centos7-5-01 ~]# docker attach mynginx

  

######exec也可以进去,但有时会出现问题

docker exec -it mydocker  /bin/bash

 

#最后容器的状态

[root@centos7-5-01 ~]# docker ps -l

CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS                          PORTS               NAMES

cd15d1249f8f        nginx               "nginx -g 'daemon of   3 minutes ago       Exited (0) About a minute ago                       mynginx   

 

 

[root@centos7-5-01 ~]# docker ps -l

CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS                          PORTS               NAMES

cd15d1249f8f        nginx               "nginx -g 'daemon of   3 minutes ago       Exited (0) About a minute ago                       mynginx         

 

#启动容器  ,推荐使用下面的命令进入容器

[root@centos7-5-01 ~]# docker start cd15d1249f8f

cd15d1249f8f

[root@centos7-5-01 ~]# docker ps -l

CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS               NAMES

cd15d1249f8f        nginx               "nginx -g 'daemon of   5 minutes ago       Up 1 seconds        80/tcp              mynginx            

 

#util-linux包有这个命令nsenter

#获取容器的pid,使用nsenter命令进入容器

 

[root@centos7-5-01 ~]# docker ps -l

CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS               NAMES

cd15d1249f8f        nginx               "nginx -g 'daemon of   5 minutes ago       Up 1 seconds        80/tcp              mynginx        

 

[root@centos7-5-01 ~]# docker inspect --format "{{.State.Pid}}" cd15d1249f8f

3451

 

[root@centos7-5-01 ~]# nsenter --target 3451 --mount --uts --ipc --net --pid

 

#脚本进入,脚本如下

#!/bin/bash

docker ps -l

read -p "请输入你要进去的容器:" CNAME

#CNAME=$1

CPID=$(docker inspect --format "{{.State.Pid}}" $CNAME)

nsenter --target "$CPID" --mount --uts --ipc --net --pid

 

 

###全部杀死容器

docker kill $(docker ps -a -q)

 

 

 

猜你喜欢

转载自www.cnblogs.com/zwlsky13/p/10432425.html