设为首页 收藏本站
查看: 593|回复: 0

[经验分享] docker learn

[复制链接]

尚未签到

发表于 2018-5-27 08:17:30 | 显示全部楼层 |阅读模式
  docker 常用命令
[root@localhost ~]# docker version
Client:
Version:      17.05.0-ce
API version:  1.29
Go version:   go1.7.5
Git commit:   89658be
Built:        Thu May  4 22:06:25 2017
OS/Arch:      linux/amd64
Server:
Version:      17.05.0-ce
API version:  1.29 (minimum version 1.12)
Go version:   go1.7.5
Git commit:   89658be
Built:        Thu May  4 22:06:25 2017
OS/Arch:      linux/amd64
Experimental: false
[root@localhost ~]# docker info
Containers: 3
Running: 1
Paused: 0
Stopped: 2
Images: 29
Server Version: 17.05.0-ce
Storage Driver: overlay
Backing Filesystem: xfs
Supports d_type: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host macvlan null overlay
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 9048e5e50717ea4497b757314bad98ea3763c145
runc version: 9c2d8d184e5da67c95d601382adf14862e4f2228
init version: 949e6fa
Security Options:
seccomp
  Profile: default
Kernel Version: 3.10.0-693.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 1.781GiB
Name: localhost.localdomain
ID: QHTO:DMIZ:PMAS:SRXP:KTR4:CH7T:3IKV:6IWK:TRKT:I6VP:CVES:PNSP
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false

[root@localhost ~]# docker logs -f 2cff  //查看容器日志
WARNING: 'UsePAM no' is not supported in Red Hat Enterprise Linux and may cause several problems.
Could not load host key: /etc/ssh/ssh_host_ecdsa_key
Could not load host key: /etc/ssh/ssh_host_ed25519_key
^C
[root@localhost ~]# docker ps //查看正在运行的容器
CONTAINER ID        IMAGE               COMMAND               CREATED             STATUS              PORTS                   NAMES
2cff51b765c2        sshdocker           "/usr/sbin/sshd -D"   22 minutes ago      Up 22 minutes       0.0.0.0:32769->22/tcp   mysshdocker
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS                   NAMES
2cff51b765c2        sshdocker           "/usr/sbin/sshd -D"      22 minutes ago      Up 22 minutes               0.0.0.0:32769->22/tcp   mysshdocker
2b3b55ec6693        sshdocker           "/usr/sbin/sshd -D"      4 hours ago         Exited (0) 35 minutes ago                           mysshdcontainer
2323f1ff98d7        bd0c225f3c21        "/bin/sh -c 'yum i..."   4 hours ago         Exited (1) 4 hours ago                              eager_meitner
[root@localhost ~]# docker ps -a -q  //为查看所有的容器,包括已经停止的。
2cff51b765c2
2b3b55ec6693
2323f1ff98d7删除所有容器
docker rm $(docker ps -a -q)
删除单个容器
docker rm <容器名orID>
停止、启动、杀死一个容器
docker stop <容器名orID>
docker start <容器名orID>
docker kill <容器名orID>
查看所有镜像
docker images
删除所有镜像
docker rmi $(docker images | grep none | awk '{print $3}' | sort -r)
运行一个新容器,同时为它命名、端口映射、文件夹映射。以redmine镜像为例
docker run --name redmine -p 9003:80 -p 9023:22 -d -v /var/redmine/files:/redmine/files -v /var/redmine/mysql:/var/lib/mysql sameersbn/redmine
一个容器连接到另一个容器
docker run -i -t --name sonar -d -link mmysql:db   tpires/sonar-server
sonar
容器连接到mmysql容器,并将mmysql容器重命名为db。这样,sonar容器就可以使用db的相关的环境变量了。
拉取镜像
docker pull <镜像名:tag>

docker pull sameersbn/redmine:latest
# 保存镜像到一个tar包; -o, --output="" Write to an file  
$docker save image_name -o file_path  
# 加载一个tar包格式的镜像; -i, --input="" Read from a tar archive file  
$docker load -i file_path  
# 机器a  
$docker save image_name > /home/save.tar  
# 使用scp将save.tar拷到机器b上,然后:  
$docker load < /home/save.tar  
eg:
[root@localhost ~]# docker save centos7/ssh:version1  -o /home/save.tar
[root@localhost ~]# ls /home/save.tar
/home/save.tar
[root@localhost ~]#
构建自己的镜像
docker build -t <镜像名> <Dockerfile路径>
如Dockerfile在当前路径:
docker build -t xx/gitlab .
eg:
docker build -t centos7:tag1 /root/dockerfile/centos7/  

  Docker容器进入的方式。转载自 //http://www.cnblogs.com/xhyan/p/6593075.html
  docker   attach   ssh    nsenter    exec
[root@localhost ~]# docker exec -it 2cff /bin/bash
[root@2cff51b765c2 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.2  netmask 255.255.0.0  broadcast 0.0.0.0
        ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)
        RX packets 5255  bytes 15870393 (15.1 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4615  bytes 271329 (264.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[root@2cff51b765c2 /]# [root@localhost ~]#
[root@localhost ~]# ssh admin@172.17.0.2
admin@172.17.0.2's password:
[admin@2cff51b765c2 ~]$ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.2  netmask 255.255.0.0  broadcast 0.0.0.0
        ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)
        RX packets 5289  bytes 15875283 (15.1 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4639  bytes 275951 (269.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[admin@2cff51b765c2 ~]$ exit
logout
Connection to 172.17.0.2 closed.
[root@localhost ~]# docke ps
-bash: docke: command not found
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND               CREATED             STATUS              PORTS                   NAMES
2cff51b765c2        sshdocker           "/usr/sbin/sshd -D"   12 minutes ago      Up 12 minutes       0.0.0.0:32769->22/tcp   mysshdocker重新查看container的stdout
# 启动top命令,后台运行$ ID=$(sudo docker run -d ubuntu /usr/bin/top -b)# 获取正在running的container的输出$ sudo docker attach $ID
top - 02:05:52 up  3:05,  0 users,  load average: 0.01, 0.02, 0.05Tasks:   1 total,   1 running,   0 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.1%us,  0.2%sy,  0.0%ni, 99.7%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:    373572k total,   355560k used,    18012k free,    27872k buffers
Swap:   786428k total,        0k used,   786428k free,   221740k cached
^C$
$ sudo docker stop $ID
eg
[root@localhost ~]# docker run -d centos7/ssh /usr/bin/top -b
Unable to find image 'centos7/ssh:latest' locally
^C
[root@localhost ~]# docker run -d centos7/ssh:version1 /usr/bin/top -b
a17fac4b304526346ff0b07b209e1859259914c0b73bd538be11f946b456b101
[root@localhost ~]# docker attach
"docker attach" requires exactly 1 argument(s).
See 'docker attach --help'.
Usage:  docker attach [OPTIONS] CONTAINER
Attach local standard input, output, and error streams to a running container
[root@localhost ~]# docker attach a17fac4b304526346ff0b07b209e1859259914c0b73bd538be11f946b456b101

top - 16:08:55 up 2 days,  1:23,  0 users,  load average: 0.00, 0.07, 0.09
Tasks:   1 total,   1 running,   0 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.1 us,  1.8 sy,  0.0 ni, 98.1 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  1867048 total,   473488 free,   225960 used,  1167600 buff/cache
KiB Swap:  2097148 total,  2095864 free,     1284 used.  1329652 avail Mem
   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
     1 root      20   0   40188   1676   1296 R   0.0  0.1   0:00.05 top
top - 16:08:58 up 2 days,  1:23,  0 users,  load average: 0.00, 0.07, 0.09
Tasks:   1 total,   1 running,   0 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  1867048 total,   473360 free,   226088 used,  1167600 buff/cache
KiB Swap:  2097148 total,  2095864 free,     1284 used.  1329528 avail Mem
   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
     1 root      20   0   40188   1676   1296 R   0.0  0.1   0:00.05 top
top - 16:09:01 up 2 days,  1:23,  0 users,  load average: 0.00, 0.07, 0.09
Tasks:   1 total,   1 running,   0 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.1 sy,  0.0 ni, 99.9 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  1867048 total,   473360 free,   226088 used,  1167600 buff/cache
KiB Swap:  2097148 total,  2095864 free,     1284 used.  1329528 avail Mem
   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
     1 root      20   0   40188   1676   1296 R   0.0  0.1   0:00.05 top
top - 16:09:04 up 2 days,  1:23,  0 users,  load average: 0.00, 0.07, 0.09
Tasks:   1 total,   1 running,   0 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.1 sy,  0.0 ni, 99.9 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  1867048 total,   473268 free,   226176 used,  1167604 buff/cache
KiB Swap:  2097148 total,  2095864 free,     1284 used.  1329440 avail Mem
   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
     1 root      20   0   40188   1676   1296 R   0.0  0.1   0:00.05 top^C
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# docker stop a17fac4b304526346ff0b07b209e1859259914c0b73bd538be11f946b456b101
a17fac4b304526346ff0b07b209e1859259914c0b73bd538be11f946b456b101  docker commit

  docker commit命令-提交一个新的image
[root@localhost ~]# docker ps -l
CONTAINER ID        IMAGE               COMMAND               CREATED             STATUS              PORTS                   NAMES
2cff51b765c2        sshdocker           "/usr/sbin/sshd -D"   6 minutes ago       Up 6 minutes        0.0.0.0:32769->22/tcp   mysshdocker
[root@localhost ~]# docker commit 2cff centos7/ssh:version1
sha256:7369170d76efb411ea023bc795aa2b2e403077c9d324f9334429cad35df7a005
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos7/ssh         version1            7369170d76ef        9 seconds ago       422MB
sshdocker           latest              3f5fd510439f        4 hours ago         323MB
<none>              <none>              bd0c225f3c21        4 hours ago         281MB
centos7             tag1                b7350ef2bb8d        5 hours ago         621MB
centos              centos7.1.1503      c288e5945774        5 weeks ago         212MB
[root@localhost ~]#  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-481598-1-1.html 上篇帖子: Docker拥抱k8s早有预兆,Docker现何去何从? 下篇帖子: docker 关于管理数据
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表