栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

docker容器命令(一)

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

docker容器命令(一)

容器命令

创建容器:docker run

参数:

  • -it 交互
  • -d 后台
  • –name 容器名
  • -p 主机端口:容器端口 (主机端口映射到docker端口)
docker run --name centos1 -it centos /bin/bash

docker ps 显示容器信息

参数:

  • -a
  • -q 只显示容器id
[root@tanqi ~]# docker ps -a
ConTAINER ID   IMAGE          COMMAND           CREATED              STATUS                      PORTS     NAMES
8ad5b2b6854f   centos         "/bin/bash"       about a minute ago   Up about a minute                     centos1
3c45403d051a   centos         "/bin/bash"       14 minutes ago       Exited (0) 14 minutes ago             frosty_cannon

[root@tanqi ~]# docker ps 
ConTAINER ID   IMAGE     COMMAND       CREATED              STATUS              PORTS     NAMES
8ad5b2b6854f   centos    "/bin/bash"   about a minute ago   Up about a minute             centos1
[root@tanqi ~]# docker ps -q
8ad5b2b6854f
[root@tanqi ~]# docker ps -aq
8ad5b2b6854f

  • 启动容器

`docker start -i 容器id/容器名

Options:
-a, --attach Attach STDOUT/STDERR and forward signals
–detach-keys string Override the key sequence for detaching a container
-i, --interactive Attach container’s STDIN

  • 进入容器

docker exec -it 容器id/容器名 /bin/bash #启动新终端

[root@tanqi ~]# docker exec -it centos1 /bin/bash

docker attach 容器id/容器名 //进入容器直接进入正在执行

[root@tanqi ~]# docker attach centos1
[root@8ad5b2b6854f /]# 
  • 退出容器

exit退出并停止

crtl+P+Q 退出 不停止

  • 从容器拷贝到主机

docker cp 容器id:容器路径 主机路径

[root@tanqi ~]# docker cp centos1:/home/test /root
[root@tanqi ~]# ls
dump.rdb  f1.txt  install.sh  study  test

-v 卷的技术

其他命令
  • 后台创建启动容器:

docker run -d centos# 可能会停止 因为没有前台进程

  • 日志

docker logs --tail 10 容器id/容器名 //看后10条

Options:
–details Show extra details provided to logs
-f, --follow Follow log output 新log会加
–since string Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for
42 minutes)
-n, --tail string Number of lines to show from the end of the logs (default “all”)
-t, --timestamps Show timestamps
–until string Show logs before a timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m
for 42 minutes)

[root@tanqi ~]# docker logs --tail 2 centos1
[root@8ad5b2b6854f home]# ls 
test

[root@tanqi ~]# docker logs -f --tail 2 centos1
[root@8ad5b2b6854f home]# ls 
test
...等待刷新
  • 运行脚本

docker run -d 容器di /bin/sh -c "脚本语言"

  • 进程信息

docker top 容器id/容器名

[root@tanqi ~]# docker top centos1
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                15054               15033               0                   15:30               pts/0               00:00:00            /bin/bash
  • 元数据

docker inspect 容器id/容器名

小结 容器命令

创建容器:docker run

参数

  • -it 交互

  • -d 后台

  • –name 容器名

docker run --name centos1 -it centos /bin/bash

docker ps 显示容器信息

参数:

  • -a
  • -q 只显示容器id
[root@tanqi ~]# docker ps -a
ConTAINER ID   IMAGE          COMMAND           CREATED              STATUS                      PORTS     NAMES
8ad5b2b6854f   centos         "/bin/bash"       about a minute ago   Up about a minute                     centos1
3c45403d051a   centos         "/bin/bash"       14 minutes ago       Exited (0) 14 minutes ago             frosty_cannon

[root@tanqi ~]# docker ps 
ConTAINER ID   IMAGE     COMMAND       CREATED              STATUS              PORTS     NAMES
8ad5b2b6854f   centos    "/bin/bash"   about a minute ago   Up about a minute             centos1
[root@tanqi ~]# docker ps -q
8ad5b2b6854f
[root@tanqi ~]# docker ps -aq
8ad5b2b6854f

  • 启动容器

`docker start -i 容器id/容器名

Options:
-a, --attach Attach STDOUT/STDERR and forward signals
–detach-keys string Override the key sequence for detaching a container
-i, --interactive Attach container’s STDIN

  • 进入容器

docker exec -it 容器id/容器名 /bin/bash #启动新终端

[root@tanqi ~]# docker exec -it centos1 /bin/bash

docker attach 容器id/容器名 //进入容器直接进入正在执行

[root@tanqi ~]# docker attach centos1
[root@8ad5b2b6854f /]# 
  • 退出容器

exit退出并停止

crtl+P+Q 退出 不停止

  • 从容器拷贝到主机

docker cp 容器id:容器路径 主机路径

[root@tanqi ~]# docker cp centos1:/home/test /root
[root@tanqi ~]# ls
dump.rdb  f1.txt  install.sh  study  test

-v 卷的技术

其他命令
  • 后台创建启动容器:

docker run -d centos# 可能会停止 因为没有前台进程

  • 日志

docker logs --tail 10 容器id/容器名 //看后10条

Options:
–details Show extra details provided to logs
-f, --follow Follow log output 新log会加
–since string Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for
42 minutes)
-n, --tail string Number of lines to show from the end of the logs (default “all”)
-t, --timestamps Show timestamps
–until string Show logs before a timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m
for 42 minutes)

[root@tanqi ~]# docker logs --tail 2 centos1
[root@8ad5b2b6854f home]# ls 
test

[root@tanqi ~]# docker logs -f --tail 2 centos1
[root@8ad5b2b6854f home]# ls 
test
...等待刷新
  • 运行脚本

docker run -d 容器di /bin/sh -c "脚本语言"

  • 进程信息

docker top 容器id/容器名

[root@tanqi ~]# docker top centos1
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                15054               15033               0                   15:30               pts/0               00:00:00            /bin/bash
  • 元数据

docker inspect 容器id/容器名

小结

官网的图

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/321730.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号