操作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容器操作的更多相关文章

  1. Docker:镜像操作和容器操作

    镜像操作 列出镜像: $ sudo docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE hello-world latest 0a6b ...

  2. 容器大小的改变以及容器操作可能使迭代器失效、vector对象的容量变化

    1 改变容器的大小 我们可以使用resize来增加或缩小容器,与往常一样,array不支持resize.如果当前大小大于所要求的大小,容器后面的元素会被删除:如果当前大小小于新大小,会将新元素添加到容 ...

  3. C++ 容器操作

    typedef struct point { int x; int y; }Point; 在声明变量的时候就可以:Point p1; 如果没有typedef, 如: struct point { in ...

  4. JAVA中的集合容器操作类

    目录 JAVA中的集合容器操作类 List集合 ArrayList的操作方法说明 LinkedList Stack Set Map Queue 总结 JAVA中的集合容器操作类 Java容器类库总共分 ...

  5. 004-docker命令-容器生命周期管理、容器操作

    1.容器生命周期管理 docker run :创建一个新的容器并运行一个命令 语法:docker run [OPTIONS] IMAGE [COMMAND] [ARG...] OPTIONS说明: - ...

  6. 顺序容器----顺序容器操作,vector对象如何增长,额外的string操作,容器适配器

    一.顺序容器操作 1.向顺序容器添加元素 向顺序容器(array除外)添加元素的操作: 操作 说明 c.push_back(t) 在c的尾部创建一个值为t的元素.返回void c.emplace_ba ...

  7. STL容器能力一览表和各个容器操作函数异常保证

    STL容器能力一览表 Vector Deque List Set Multiset map Multimap 典型内部 结构 dynamic array Array of arrays Doubly ...

  8. 【足迹C++primer】38、关联容器操作(2)

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/cutter_point/article/details/35244805 关联容器操作(2) map ...

  9. docker 容器操作( 以 tomcat 为例 )

    一.容器操作 一个镜像可以启动多个容器.比如一个 tomcat 镜像,可以启动多个tomcat 容器,启动后的这些 tomcat 都是各自独立的 二.步骤 1.搜索镜像 [root@localhost ...

随机推荐

  1. SQL和HQL 区别浅析!!!

    hql是面向对象查询,格式:from + 类名 + 类对象 + where + 对象的属性 sql是面向数据库表查询,格式:from + 表名 + where + 表中字段 1.查询 一般在hiber ...

  2. nginx 反向代理实现负载均衡*配置实战

    重要点: 1配置反向代理多虚拟主机节点服务器 2经过反向代理后的节点服务器记录用户IP 3与反向代理配置相关的更多参数说明 4根据URL目录地址转发 (1)根据URL中的目录地址实现代理转发(动静分离 ...

  3. [转]Nginx配置信息详解

    序言 Nginx是lgor Sysoev为俄罗斯访问量第二的rambler.ru站点设计开发的.从2004年发布至今,凭借开源的力量,已经接近成熟与完善. Nginx功能丰富,可作为HTTP服务器,也 ...

  4. leetcode 146. LRU Cache 、460. LFU Cache

    LRU算法是首先淘汰最长时间未被使用的页面,而LFU是先淘汰一定时间内被访问次数最少的页面,如果存在使用频度相同的多个项目,则移除最近最少使用(Least Recently Used)的项目. LFU ...

  5. ubuntu更换源的方法

    1.查看ubuntu版本的方法: 使用命令 sudo lsb_release -a 输出如下: root@localhost:/etc/apt# sudo lsb_release -aNo LSB m ...

  6. [Python]正则匹配字符串 | 蒲公英二维码图片url

    代码示例: import re def Find(string): # findall() 查找匹配正则表达式的字符串 url = re.findall('http[s]?://(?:[a-zA-Z] ...

  7. pip安装django出错 Could not install packages due to an EnvironmentError: [Errno 13]

    pip install django 下载安装Django报错, 按照提示的建议改为 pip install --user django 安装完成

  8. 从源码看 Vue 中的 Mixin

    最近在做项目的时候碰到了一个奇怪的问题,通过 Vue.mixin 方法注入到 Vue 实例的一个方法不起作用了,后来经过仔细排查发现这个实例自己实现了一个同名方法,导致了 Vue.mixin 注入方法 ...

  9. Linux新增开放端口

    CentOS系统 开放端口的方法: 方法一:命令行方式               1. 开放端口命令: /sbin/iptables -I INPUT -p tcp --dport 8080 -j ...

  10. MATLAB学习(六)绘图图形功能

    >> x=0:.1:2*pi;plot(x,sin(x),x,cos(x))                               >> plot(x,sin(x),'p ...