docker的常用操作

#########################################docker event state#########################################

 

 

 

 

 

 

 

[root@centos7-5-01 ~]# docker version

Client:
Version: 18.09.2
API version: 1.39
Go version: go1.10.6
Git commit: 6247962
Built: Sun Feb 10 04:13:27 2019
OS/Arch: linux/amd64
Experimental: false

Server: Docker Engine - Community
Engine:
Version: 18.09.2
API version: 1.39 (minimum version 1.12)
Go version: go1.10.6
Git commit: 6247962
Built: Sun Feb 10 03:47:25 2019
OS/Arch: linux/amd64
Experimental: false
[root@centos7-5-01 ~]# docker info
Containers: 1
Running: 1
Paused: 0
Stopped: 0
Images: 1
Server Version: 18.09.2
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 9754871865f7fe2f4e74d43e2fc7ccd237edcbce
runc version: 09c8266bf2fcf9519a651b04ae54c967b9ab86ec
init version: fec3683
Security Options:
seccomp
Profile: default
Kernel Version: 3.10.0-862.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 1.779GiB
Name: centos7-5-01
ID: 2OY6:GLOK:FYFW:4VP2:H5H5:JDHQ:DEGU:C7FB:7QY7:4MJH:E2Z6:KQKI
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
Product License: Community Engine

WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled

 

 

 ###############镜像的导入和导出############### 

#第一种方法

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

#第二种方法

 

#################################docker常见操作命令######################################

#-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/sh

 

#最后容器的状态

[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)

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/zwlsky13/p/10432425.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值