操作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. 8 HashMap

    1.Map接口 public interface Map<K, V> 将键映射到值的对象,一个映射不能包含重复的键,每个键只能映射到一个值. 具体的实现:HashMap,TreeMap, ...

  2. JAVA类定义的修饰

    命名类的访问权限public.protected.friendly,private 这四个的权限, 作用域: 当前(父,超)类: 同一Package: 派生(子)类: 其他Package: publi ...

  3. 【log4j】log4j.properties 文件示例

    # 下面的文件内容是写程序长期要用的,放在这里留个底#Output information(higher than INFO) to stdout and file.info/debug/error ...

  4. 为macos开启外接显示器hdpi分辨率

    安装了Switch RES,但是缺少当前显示器合适的HDPI分辨率的时候,可以参考这里,自动生成合适的配置文件. https://comsysto.github.io/Display-Override ...

  5. Tomcat安装应用部署及配置文件解读

    Tomcat服务器是一个免费的开放源代码的Web应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP程序的首选. Tomcat和Nginx,APa ...

  6. easyUI之window窗口

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...

  7. 指定JSON.toJSONString中实体类属性的输出顺序

    最近在使用JSON.toJSONString过程中出现实体类的属性与转换之前的顺序不一致 public static void main(String[] args) { Person person ...

  8. centos7.6安装docker

    先运行 yum update 然后卸载旧版本 yum remove docker \ docker-client \ docker-client-latest \ docker-common \ do ...

  9. DevOps - 微服务与Serverless

    微服务 简介 "微服务"强调的是服务的大小,它关注的是某一个点. "微服务架构"则是一种架构思想,需要从整体上对软件系统进行通盘的考虑. 通俗来说,微服务架构就 ...

  10. 20190603 - CentOS 7 提示 Failed to load SELinux policy. Freezing 导致卡住不启动的解决办法

    现象 最近 Windows 和两台 Mac 混用,将 Windows VirtualBox 中安装的 CentOS 7 拷贝到 Mac 上. 启动 CentOS 7 时,图形界面进度卡在最后,按 Es ...