04docker容器操作
操作Docker container
容器是镜像的一个运行实例,镜像是静态的只读文件,容器带有运行时需要的可写文件层,同时,容器中的应用进程处于运行状态
1:新建一个容器
ubuntu@ubuntu:~$ docker create -it ubuntu:18.04
05ebf18db758ddf8ff238cbc85e108235e5742587eefddb4272e980e1f504854
ubuntu@ubuntu:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
05ebf18db758 ubuntu:18.04 "/bin/bash" 14 seconds ago Created frosty_northcutt
#创建的容器处于停止状态
2:启动一个容器
ubuntu@ubuntu:~$ docker start 05ebf18db758
05ebf18db758
ubuntu@ubuntu:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
05ebf18db758 ubuntu:18.04 "/bin/bash" 3 minutes ago Up 7 seconds frosty_northcutt
3:新建并启动容器
ubuntu@ubuntu:~$ docker run -it ubuntu:18.04 /bin/bash
root@292c73812a92:/#
#-t选项让Docker分配一个伪终端(pseudo-tty)并绑定到容器的标准输入上,-i则让容器的标准输入保持打开。
#man docker-run
-d:让容器以守护态运行
ubuntu@ubuntu:~$ docker run -d ubuntu:18.04 /bin/bash -c "while true;do echo hello world;sleep 1;done"
f37eba8f478a0150a6b6f0e09148d847af479c9cc5f797e4bde2c3b44c81a3da
ubuntu@ubuntu:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f37eba8f478a ubuntu:18.04 "/bin/bash -c 'while…" 11 seconds ago Up 11 seconds youthful_boyd
查看某容器的输出
ubuntu@ubuntu:~$ docker logs f37eba8f478a
hello world
hello world
hello world
...
4:停止容器
ubuntu@ubuntu:~$ docker run --name test -d --rm -it ubuntu:18.04 bash #后台运行一个容器
9834e3c598f85531902cee9734af1d847e09fa82ef63e4235358e86a69dca1fc
# --name :Assign a name to the container
# --rm:可以与-d一起工作,自动删除将在守护进程端完成
ubuntu@ubuntu:~$ docker pause test #停止容器
test
ubuntu@ubuntu:~$ docker ps #查看容器状态,处于(Paused)
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9834e3c598f8 ubuntu:18.04 "bash" About a minute ago Up About a minute (Paused) test
ubuntu@ubuntu:~$ docker unpause test #恢复容器
test
ubuntu@ubuntu:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9834e3c598f8 ubuntu:18.04 "bash" 2 minutes ago Up 2 minutes test
5:终止容器|重新启动容器
#终止容器
ubuntu@ubuntu:~$ docker stop test
test
#:查看容器
ubuntu@ubuntu:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f37eba8f478a ubuntu:18.04 "/bin/bash -c 'while…" 16 minutes ago Exited (137) 10 minutes ago youthful_boyd
#启动容器
ubuntu@ubuntu:~$ docker start f37eba8f478a
f37eba8f478a
#重启容器
ubuntu@ubuntu:~$ docker restart f37eba8f478a
f37eba8f478a
6:进入容器---当使用-d参数的容器就会进入后台,用户无法看到容器中的信息,也无法进行操作
ubuntu@ubuntu:~$ docker run -itd ubuntu:18.04 #后台运行容器
e3b8bf83a13e3a79cf63ee1e7cc6ead2bb5ad30de5d820e89b63a0a2be38a005
ubuntu@ubuntu:~$
#进入刚创立的容器中,并且打开一个新的bash终端
ubuntu@ubuntu:~$ docker exec -it e3b8bf83a13e3a79cf /bin/bash
root@e3b8bf83a13e:/#
7:删除容器-----删除处于终止或者退出状态的容器
ubuntu@ubuntu:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e3b8bf83a13e ubuntu:18.04 "/bin/bash" 4 minutes ago Up 3 minutes modest_rosalind
ubuntu@ubuntu:~$ docker rm -f e3b8bf83a13e # -f: 强制删除正在运行的容器
e3b8bf83a13e
ubuntu@ubuntu:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f37eba8f478a ubuntu:18.04 "/bin/bash -c 'while…" 39 minutes ago Exited (137) 5 minutes ago youthful_boyd
fc37c81884cf ubuntu:18.04 "-c 'while true;do e…" 39 minutes ago Created peaceful_sammet
292c73812a92 ubuntu:18.04 "/bin/bash" 43 minutes ago Exited (0) 41 minutes ago happy_nobel
#一次性删除多个容器
ubuntu@ubuntu:~$ docker rm $(docker ps -aq)
ubuntu@ubuntu:~$ docker ps -a -q | xargs docker rm
8:导入和导出容器---将容器从一个系统导入到另一个系统
创建一个正在运行和退出的容器
ubuntu@ubuntu:~$ docker run -d -it ubuntu:18.04 /bin/bash
c3637c3828b88995f9db358fb08dbc8d66bacb5605f764a75a2343340ec2bebd
ubuntu@ubuntu:~$ docker run -it ubuntu:18.04 /bin/bash
root@4f595f3e726d:/# exit
exit
ubuntu@ubuntu:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4f595f3e726d ubuntu:18.04 "/bin/bash" 9 seconds ago Exited (0) 5 seconds ago hungry_leavitt
c3637c3828b8 ubuntu:18.04 "/bin/bash" 29 seconds ago Up 29 seconds nifty_ritchie
ubuntu@ubuntu:~$ docker export -o test_stop.tar 4f5 #导出一个退出的容器
ubuntu@ubuntu:~$ docker export -o test_run.tar c36 #导出一个正在运行的容器
ubuntu@ubuntu:~$ ls -lt
total 195112
-rw------- 1 ubuntu ubuntu 66570752 Nov 14 21:30 test_run.tar
-rw------- 1 ubuntu ubuntu 66571776 Nov 14 21:29 test_stop.tar
导出的文件可以通过docker import 命令导入为镜像.
scp到另一台服务器上:
ubuntu@ubuntu:~$ scp -r test_run.tar root@192.168.1.10:/home
#格式:docker import [-c|--change[=[]]] [-m|--message[=MESSAGE]] file|URL|[REPOSITORY[:TAG]]
[root@localhost home]# docker import test_run.tar test/ubuntu:v1.0
sha256:7d1a701d2885277b0cbb5160a9947eec965b612d4674e872a62047ef0d3f1b39
[root@localhost home]# docker images #新服务器上查看镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
test/ubuntu v1.0 7d1a701d2885 3 minutes ago 64.2MB
9:查看容器
#inspect:查看镜像的具体信息
[root@localhost home]# docker image inspect test/ubuntu:v1.0
[
{
"Id": "sha256:7d1a701d2885277b0cbb5160a9947eec965b612d4674e872a62047ef0d3f1b39",
"RepoTags": [
"test/ubuntu:v1.0"
...
#查看容器的具体信息
ubuntu@ubuntu:~$ docker container inspect c3637c3828b8
[
{
"Id": "c3637c3828b88995f9db358fb08dbc8d66bacb5605f764a75a2343340ec2bebd",
"Created": "2019-11-14T13:20:17.367777873Z",
"Path": "/bin/bash",
...
#查看容器内进程可以使用docker [container] top [OPTIONS] CONTAINER [CONTAINER...]子命令
ubuntu@ubuntu:~$ docker top c3637c3828b8
#查看统计信息可以使用docker [container] stats [OPTIONS] [CONTAINER...]子命令,会显示CPU、内存、存储、网络等使用情况的统计信息
-a :输出所有统计信息,默认仅输出运行中的
ubuntu@ubuntu:~$ docker stats -a
10:复制文件:可以在容器和主机之间复制文件
命令格式为docker [container] cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|- 。
#将本地路径的test.txt 复制到 容器的/tmp下
ubuntu@ubuntu:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4f595f3e726d ubuntu:18.04 "/bin/bash" About an hour ago Exited (0) About an hour ago hungry_leavitt
c3637c3828b8 ubuntu:18.04 "/bin/bash" About an hour ago Up About an hour nifty_ritchie
ubuntu@ubuntu:~$ docker cp test.txt c3637c3828b8:/tmp
ubuntu@ubuntu:~$ docker exec -it c3637c3828b8 /bin/bash
root@c3637c3828b8:/# cd /tmp/
root@c3637c3828b8:/tmp# ls
test.txt
root@c3637c3828b8:/tmp#
11:查看容器变更
命令格式为docker [container] diff CONTAINER
ubuntu@ubuntu:~$ docker diff c3637c3828b8
C /root
A /root/.bash_history
C /tmp
A /tmp/test.txt
12:查看端口映射
命令格式为docker container port CONTAINER [PRIVATE_PORT[/PROTO]]
ubuntu@ubuntu:~$ docker port c3637c3828b8 #没有映射端口
小结:
docker container help命令查看Docker支持的容器操作子命令
ubuntu@ubuntu:~$ docker container help
Usage: docker container COMMAND
Manage containers
Options:
Commands:
attach Attach local standard input, output, and error streams to a running container
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
exec Run a command in a running container
export Export a container's filesystem as a tar archive
inspect Display detailed information on one or more containers
kill Kill one or more running containers
logs Fetch the logs of a container
ls List containers
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
prune Remove all stopped containers
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
run Run a command in a new container
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
wait Block until one or more containers stop, then print their exit codes
Run 'docker container COMMAND --help' for more information on a command.
04docker容器操作的更多相关文章
- Docker:镜像操作和容器操作
镜像操作 列出镜像: $ sudo docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE hello-world latest 0a6b ...
- 容器大小的改变以及容器操作可能使迭代器失效、vector对象的容量变化
1 改变容器的大小 我们可以使用resize来增加或缩小容器,与往常一样,array不支持resize.如果当前大小大于所要求的大小,容器后面的元素会被删除:如果当前大小小于新大小,会将新元素添加到容 ...
- C++ 容器操作
typedef struct point { int x; int y; }Point; 在声明变量的时候就可以:Point p1; 如果没有typedef, 如: struct point { in ...
- JAVA中的集合容器操作类
目录 JAVA中的集合容器操作类 List集合 ArrayList的操作方法说明 LinkedList Stack Set Map Queue 总结 JAVA中的集合容器操作类 Java容器类库总共分 ...
- 004-docker命令-容器生命周期管理、容器操作
1.容器生命周期管理 docker run :创建一个新的容器并运行一个命令 语法:docker run [OPTIONS] IMAGE [COMMAND] [ARG...] OPTIONS说明: - ...
- 顺序容器----顺序容器操作,vector对象如何增长,额外的string操作,容器适配器
一.顺序容器操作 1.向顺序容器添加元素 向顺序容器(array除外)添加元素的操作: 操作 说明 c.push_back(t) 在c的尾部创建一个值为t的元素.返回void c.emplace_ba ...
- STL容器能力一览表和各个容器操作函数异常保证
STL容器能力一览表 Vector Deque List Set Multiset map Multimap 典型内部 结构 dynamic array Array of arrays Doubly ...
- 【足迹C++primer】38、关联容器操作(2)
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/cutter_point/article/details/35244805 关联容器操作(2) map ...
- docker 容器操作( 以 tomcat 为例 )
一.容器操作 一个镜像可以启动多个容器.比如一个 tomcat 镜像,可以启动多个tomcat 容器,启动后的这些 tomcat 都是各自独立的 二.步骤 1.搜索镜像 [root@localhost ...
随机推荐
- linux 部署jar
Linux 运行jar包命令如下: 方式一: java -jar xxx.jar 这种方式特点是ssh窗口关闭时,程序中止运行.或者是运行时没法切出去执行其他任务,有没有办法让Jar在后台运行呢: 方 ...
- RocketMQ使用记录
---恢复内容开始--- he following softwares are assumed installed: 64bit OS, Linux/Unix/Mac is recommended; ...
- RHEL 7.6系统安装配置图解教程
- LightGBM新特性总结
LightGBM提出两种新方法:Gradient-based One-Side Sampling (GOSS) 和Exclusive Feature Bundling (EFB)(基于梯度的one-s ...
- 【MyBatis】【SQL】没有最快,只有更快,从一千万条记录中删除八百万条仅用1分9秒
这次直接使用delete from emp where cdate<'2018-02-02',看看究竟会发生什么. Mapper里写好SQL: <?xml version="1. ...
- 在业务控制方法中写入模型变量收集参数,且使用@InitBind来解决字符串转日期类型
1) 在默认情况下,springmvc不能将String类型转成java.util.Date类型,所有我们只能在Action 中自定义类型转换器 <form action="${pa ...
- Ruby on Rails 的模型 validates 验证
validate(), 这个方法在每次保存数据时都会被调用.如:def validate if name.blank? && email.blank? errors.add_to_b ...
- php上传文件夹
用过浏览器的开发人员都对大文件上传与下载比较困扰,之前遇到了一个php文件夹上传下载的问题,无奈之下自己开发了一套文件上传控件,在这里分享一下.希望能对你有所帮助. 功能介绍: 树形目录导航.您可以通 ...
- CF792E Colored Balls【思维】
题目传送门 考试的时候又想到了小凯的疑惑,真是中毒不浅... 设每一个数都可以被分成若干个$k$和$k+1$的和.数$x$能够被分成若干个$k$和$k+1$的和的充要条件是:$x%k<=floo ...
- 【并行计算-CUDA开发】CUDA存储器模型
CUDA存储器模型 除了执行模型以外,CUDA也规定了存储器模型(如图2所示)和一系列用于主控CPU与GPU间通信的不同地址空间.图中红色的区域表示GPU片内的高速存储器,橙色区域表示DRAM中的的地 ...