容器操作

使用 docker 命令行操作 docker 容器

启动容器

core@localhost ~ $ docker run

Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Run a command in a new container

  -a, --attach=[]            Attach to STDIN, STDOUT or STDERR.
-c, --cpu-shares=0 CPU shares (relative weight)
--cap-add=[] Add Linux capabilities
--cap-drop=[] Drop Linux capabilities
--cidfile="" Write the container ID to the file
--cpuset="" CPUs in which to allow execution (0-3, 0,1)
-d, --detach=false Detached mode: run container in the background and print new container ID
--device=[] Add a host device to the container (e.g. --device=/dev/sdc:/dev/xvdc)
--dns=[] Set custom DNS servers
--dns-search=[] Set custom DNS search domains
-e, --env=[] Set environment variables
--entrypoint="" Overwrite the default ENTRYPOINT of the image
--env-file=[] Read in a line delimited file of environment variables
--expose=[] Expose a port from the container without publishing it to your host
-h, --hostname="" Container host name
-i, --interactive=false Keep STDIN open even if not attached
--link=[] Add link to another container in the form of name:alias
--lxc-conf=[] (lxc exec-driver only) Add custom lxc options --lxc-conf="lxc.cgroup.cpuset.cpus = 0,1"
-m, --memory="" Memory limit (format: <number><optional unit>, where unit = b, k, m or g)
--name="" Assign a name to the container
--net="bridge" Set the Network mode for the container
'bridge': creates a new network stack for the container on the docker bridge
'none': no networking for this container
'container:<name|id>': reuses another container network stack
'host': use the host network stack inside the container. Note: the host mode gives the container full access to local system services such as D-bus and is therefore considered insecure.
-P, --publish-all=false Publish all exposed ports to the host interfaces
-p, --publish=[] Publish a container's port to the host
format: ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort
(use 'docker port' to see the actual mapping)
--privileged=false Give extended privileges to this container
--restart="" Restart policy to apply when a container exits (no, on-failure, always)
--rm=false Automatically remove the container when it exits (incompatible with -d)
--sig-proxy=true Proxy received signals to the process (even in non-TTY mode). SIGCHLD, SIGSTOP, and SIGKILL are not proxied.
-t, --tty=false Allocate a pseudo-TTY
-u, --user="" Username or UID
-v, --volume=[] Bind mount a volume (e.g., from the host: -v /host:/container, from Docker: -v /container)
--volumes-from=[] Mount volumes from the specified container(s)
-w, --workdir="" Working directory inside the container

容器启动能够使用上述參数,以下是一些常见的參数的使用演示样例:

core@localhost ~ $ docker run -ti ubuntu:14.04  /bin/bash
root@fd2db7b20f6b:/#
# -t 表示返回一个 tty 终端。-i 表示打开容器的标准输入。使用这个命令能够得到一个容器的 shell 终端。假设不使用 dockerfile 的话,能够使用这样的方式来对容器做一些更改,然后使用「docker commit」提交到新的 image
#让我们打开另外一个终端,查看下当前执行的容器
core@localhost ~ $ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fd2db7b20f6b dl.dockerpool.com:5000/ubuntu:14.04 "/bin/bash" 23 seconds ago Up 22 seconds thirsty_hawking
9cb2e45814e0 4b32789c7d66 "/run.sh" 21 hours ago Up 21 hours 0.0.0.0:3306->3306/tcp, 0.0.0.0:8080->80/tcp loving_feynman
e3c136d76b44 tutum/tomcat:8.0 "/run.sh" 23 hours ago Up 23 hours 0.0.0.0:80->8080/tcp tomcat001
fe9e65aaf58c dl.dockerpool.com:5000/mysql:5.7 "/entrypoint.sh mysq 23 hours ago Up 23 hours 3306/tcp db001,tomcat001/tomysql
#能够看到 fd2db7b20f6b 正处于执行中
root@fd2db7b20f6b:/# exit
exit
#当我们 「exit」 退出的时候,再查看下「docker ps」
core@localhost ~ $ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9cb2e45814e0 4b32789c7d66 "/run.sh" 21 hours ago Up 21 hours 0.0.0.0:3306->3306/tcp, 0.0.0.0:8080->80/tcp loving_feynman
e3c136d76b44 tutum/tomcat:8.0 "/run.sh" 23 hours ago Up 23 hours 0.0.0.0:80->8080/tcp tomcat001
fe9e65aaf58c dl.dockerpool.com:5000/mysql:5.7 "/entrypoint.sh mysq 23 hours ago Up 23 hours 3306/tcp db001,tomcat001/tomysql
#我们发现容器已经退出了,所以这样的启动的方法不是我们使用容器来执行服务的好办法
core@localhost ~ $ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
base/163 latest 468d347c06bc 46 hours ago 249.1 MB
test/supervisord latest 468d347c06bc 46 hours ago 249.1 MB
ubuntu 14.04 1357f421be38 5 days ago 192.7 MB
dl.dockerpool.com:5000/ubuntu 14.04 1357f421be38 5 days ago 192.7 MB
mysql 5.7 e95cbb9f48ea 7 days ago 258.6 MB
dl.dockerpool.com:5000/mysql 5.7 e95cbb9f48ea 7 days ago 258.6 MB
<none> <none> 4b32789c7d66 6 weeks ago 469.8 MB
tutum/tomcat 8.0 866eb07a675e 6 weeks ago 539.4 MB
tutum/tomcat latest 02e84f04100e 6 weeks ago 539.4 MB
dl.dockerpool.com:5000/alexeiled/docker-oracle-xe-11g latest ba16d5d5e1aa 7 months ago 2.388 GB
core@localhost ~ $ docker run -p 100:22 -d base/163
6d9542bdc544e20b2f9718633a479e09047abef675ea24377d4daf4f04f20237
#这里我们使用了前面创建的 base/163 的容器。并使用了 -p 和 -d 2个參数。他们分别表示映射宿主主机端口和后台执行容器的意思
core@localhost ~ $ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6d9542bdc544 base/163:latest "/usr/bin/supervisor 5 minutes ago Up 5 minutes 0.0.0.0:100->22/tcp desperate_franklin
9cb2e45814e0 4b32789c7d66 "/run.sh" 21 hours ago Up 21 hours 0.0.0.0:3306->3306/tcp, 0.0.0.0:8080->80/tcp loving_feynman
e3c136d76b44 tutum/tomcat:8.0 "/run.sh" 23 hours ago Up 23 hours 0.0.0.0:80->8080/tcp tomcat001
fe9e65aaf58c dl.dockerpool.com:5000/mysql:5.7 "/entrypoint.sh mysq 23 hours ago Up 23 hours 3306/tcp
#从输出的 port 列。我们能够看到 0.0.0.0:100->22/tcp ,它表示该容器映射了宿主主机的 100 端口到容器的 22 端口,即 ssh 服务端口
#这样我们就创建了一个具有 ssh 服务的容器,假设须要创建其它类型服务的容器。步骤跟创建 ssh 服务差点儿相同,我们会在后面的章节具体介绍
core@localhost ~ $ ssh root@127.0.0.1 -p 100
Welcome to Ubuntu 14.04 LTS (GNU/Linux 3.2.0-58-generic x86_64) * Documentation: https://help.ubuntu.com/ The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright. Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law. root@6d9542bdc544:~#exit
logout
Connection to 127.0.0.1 closed.
core@localhost ~ $ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6d9542bdc544 base/163:latest "/usr/bin/supervisor 11 minutes ago Up 11 minutes 0.0.0.0:100->22/tcp desperate_franklin
9cb2e45814e0 4b32789c7d66 "/run.sh" 21 hours ago Up 21 hours 0.0.0.0:3306->3306/tcp, 0.0.0.0:8080->80/tcp loving_feynman
e3c136d76b44 tutum/tomcat:8.0 "/run.sh" 23 hours ago Up 23 hours 0.0.0.0:80->8080/tcp tomcat001
fe9e65aaf58c dl.dockerpool.com:5000/mysql:5.7 "/entrypoint.sh mysq 23 hours ago Up 23 hours 3306/tcp db001,tomcat001/tomysql
#我们从 ssh 终端退出后,容器也不会停止

注意:假设读者没有跟着我们的教程来创建 base/163 的镜像,这里是看不到的这个 images 的,点击这里查看教程 另外。另一些如 -v --link 等与容器数据存储和网络相关的參数将相关章节再具体介绍

停止容器

core@localhost ~ $ docker stop

Usage: docker stop [OPTIONS] CONTAINER [CONTAINER...]

Stop a running container by sending SIGTERM and then SIGKILL after a grace period

  -t, --time=10      Number of seconds to wait for the container to stop before killing it.
Default is 10 seconds.
#还能够设定在强制杀死容器进程之前等待多少秒来等待容器停止,一般使用默认值就可以
core@localhost ~ $ docker stop 6d9
#也能够使用容器的名称来停止,停止多个容器在每一个容器之间加空格就可以
6d9
core@localhost ~ $ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9cb2e45814e0 4b32789c7d66 "/run.sh" 21 hours ago Up 21 hours 0.0.0.0:3306->3306/tcp, 0.0.0.0:8080->80/tcp loving_feynman
e3c136d76b44 tutum/tomcat:8.0 "/run.sh" 23 hours ago Up 23 hours 0.0.0.0:80->8080/tcp tomcat001
fe9e65aaf58c dl.dockerpool.com:5000/mysql:5.7 "/entrypoint.sh mysq 23 hours ago Up 23 hours 3306/tcp db001,tomcat001/tomysql

删除容器

core@localhost ~ $ docker rm

Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...]

Remove one or more containers

  -f, --force=false      Force the removal of a running container (uses SIGKILL)
#使用 SIGKILL 强制删除一个正在执行的容器
-l, --link=false Remove the specified link and not the underlying container
#删除容器的连接。而非容器
-v, --volumes=false Remove the volumes associated with the container
#删除容器相关的数据卷
core@localhost ~ $ docker start 6d9
6d9
core@localhost ~ $ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6d9542bdc544 base/163:latest "/usr/bin/supervisor 25 minutes ago Up 2 seconds 0.0.0.0:100->22/tcp desperate_franklin
9cb2e45814e0 4b32789c7d66 "/run.sh" 21 hours ago Up 21 hours 0.0.0.0:3306->3306/tcp, 0.0.0.0:8080->80/tcp loving_feynman
e3c136d76b44 tutum/tomcat:8.0 "/run.sh" 23 hours ago Up 23 hours 0.0.0.0:80->8080/tcp tomcat001
fe9e65aaf58c dl.dockerpool.com:5000/mysql:5.7 "/entrypoint.sh mysq 23 hours ago Up 23 hours 3306/tcp db001,tomcat001/tomysql
#当我们使用「docker stop」停止一个容器之后,容器并没有被删除。我们还能够使用「docker start」来启动它
core@localhost ~ $ docker start 6d9
6d9
core@localhost ~ $ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6d9542bdc544 base/163:latest "/usr/bin/supervisor 25 minutes ago Up 2 seconds 0.0.0.0:100->22/tcp desperate_franklin
9cb2e45814e0 4b32789c7d66 "/run.sh" 21 hours ago Up 21 hours 0.0.0.0:3306->3306/tcp, 0.0.0.0:8080->80/tcp loving_feynman
e3c136d76b44 tutum/tomcat:8.0 "/run.sh" 23 hours ago Up 23 hours 0.0.0.0:80->8080/tcp tomcat001
fe9e65aaf58c dl.dockerpool.com:5000/mysql:5.7 "/entrypoint.sh mysq 23 hours ago Up 23 hours 3306/tcp db001,tomcat001/tomysql
core@localhost ~ $ docker rm -f 6d9
#使用 -f 强制删除容器
6d9
core@localhost ~ $ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9cb2e45814e0 4b32789c7d66 "/run.sh" 22 hours ago Up 22 hours 0.0.0.0:3306->3306/tcp, 0.0.0.0:8080->80/tcp loving_feynman
e3c136d76b44 tutum/tomcat:8.0 "/run.sh" 24 hours ago Up 24 hours 0.0.0.0:80->8080/tcp tomcat001
fe9e65aaf58c dl.dockerpool.com:5000/mysql:5.7 "/entrypoint.sh mysq 24 hours ago Up 24 hours 3306/tcp db001,tomcat001/tomysql
core@localhost ~ $ docker rm -l tomcat001/tomysql
tomcat001/tomysql
#使用 -l 删除了之前建立的到 mysql 的连接
core@localhost ~ $ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9cb2e45814e0 4b32789c7d66 "/run.sh" 22 hours ago Up 22 hours 0.0.0.0:3306->3306/tcp, 0.0.0.0:8080->80/tcp loving_feynman
e3c136d76b44 tutum/tomcat:8.0 "/run.sh" 24 hours ago Up 24 hours 0.0.0.0:80->8080/tcp tomcat001
fe9e65aaf58c dl.dockerpool.com:5000/mysql:5.7 "/entrypoint.sh mysq 24 hours ago Up 24 hours 3306/tcp db001
core@localhost ~ $

导出容器

core@localhost ~ $ docker export

Usage: docker export CONTAINER

Export the contents of a filesystem as a tar archive to STDOUT
core@localhost ~ $ docker run -d base/163
5883b400a03ed81b76a22badeb8948642023dd4b64c083cc74e787a2e3d7f7a4
core@localhost ~ $ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5883b400a03e base/163:latest "/usr/bin/supervisor 3 seconds ago Up 2 seconds 22/tcp agitated_heisenberg
9cb2e45814e0 4b32789c7d66 "/run.sh" 23 hours ago Up 23 hours 0.0.0.0:3306->3306/tcp, 0.0.0.0:8080->80/tcp loving_feynman
e3c136d76b44 tutum/tomcat:8.0 "/run.sh" 24 hours ago Up 24 hours 0.0.0.0:80->8080/tcp tomcat001
fe9e65aaf58c dl.dockerpool.com:5000/mysql:5.7 "/entrypoint.sh mysq 24 hours ago Up 24 hours 3306/tcp db001
core@localhost ~ $ docker export 588 >>base/163.tar
core@localhost ~/base $ ls -lh
total 227M
-rw-r--r-- 1 core core 227M Oct 16 03:00 163.tar
#导出了一个227M的文件,我们又能够将他导入成为 images

容器原理

很多其它内容请关注 http://www.dockerpool.com

版权声明:本文博主原创文章,博客,未经同意不得转载。

Docker container 集装箱说明的更多相关文章

  1. 使Docker Container支持运行SWT程序

    1, 下载安装JDK的docker container 我是从这个源下载已经做好的JDK8的container: https://registry.hub.docker.com/u/dockerfil ...

  2. Docker container network configuration

    http://xmodulo.com/networking-between-docker-containers.html How to set up networking between Docker ...

  3. 自动启动docker container

    当系统启动的时候要启动docker container, 可以利用systemctl来实现 比如拿mongodb为例 创建 /usr/lib/systemd/system/docker_mongodb ...

  4. 怎样从host之外连接到docker container

    启动docker的时候的指令使用 sudo docker -H tcp://0.0.0.0:4243 -H unix:///var/run/docker.sock -d & 这样就能使dock ...

  5. Docker4Windows -- 从外部(非本机host)访问 由docker container运行的程序

    背景 当我们在windows 上面运行docker container的时候,我们需要借助于模拟器(例如,Virtual box/Hyper V),她的目的主要是在我们的windows系统上面模拟出一 ...

  6. docker container(容器)

    docker 容器 Docker容器类似于一个轻量级的沙箱,Docker利用容器来运行和隔离应用 容器是从镜像创建的应用运行实例.它可以启动,开始,停止,删除,而这些容器都是彼此相互隔离,互不可见的. ...

  7. 如何在查看docker container内进程信息,与宿主机上进程信息的映射关系

    docker container内运行的进程,在宿主机上,通过ps也是能够查到的,但是在不熟悉命令的时候,无法快速找到他们的关系. 这里科普一个基础命令 docker top 1. 找到容器的id d ...

  8. Docker: docker container常用命令实战(2)-数据持久化

    应用服务是在容器中运行的,容器随时会被删除,如果是个mysql容器呢?数据存储在容器里,容器删除了,数据也没了,那就是个噩梦. 所以一些数据是需要存储在容器之外的,可以是宿主机,可以是网络存储位置上, ...

  9. Docker: docker container常用命令实战

    容器管理,容器常用选项 选项 描述 -i, –interactive 交互式 -t, –tty 分配一个伪终端 -d, –detach 运行容器到后台 -e, –env 设置环境变量 -p, –pub ...

随机推荐

  1. amazeui页面分析4

    amazeui页面分析4 一.总结 1.直接照着作者的设计思路用:例如 pet_hd_con_time pet_hd_con_map ,这是time 和 map,那我别的说不定也可以直接用,比如aut ...

  2. 表单提交数据格式form data

    前言: 最近遇到的最多的问题就是表单提交数据格式问题了. 常见的三种表单提交数据格式,分别举例说明:(项目是vue的框架) 1.application/x-www-form-urlencoded 提交 ...

  3. C++学习笔记(达内视频版)

    达内C++(陈宗权主讲) 第一天: 课程分为Core C++(标准C++.不依赖操作系统)和Unix C++. 1.配置bash,运行.sh文件. vi bash_profile 在"pat ...

  4. Eclipse查看某个方法被哪些类调用

    方法一:打开该类,在类的定义上即类名上,右键-->References--->Project ,就可以查看该类是否被工程中的其他Java文件引用过:但是如果在JSP页面,这个方法查不出来 ...

  5. UE4制作插件的插件神器pluginCreator

    本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/45644007 作者:car ...

  6. [Recompose] Transform Props using Recompose --mapProps

    Learn how to use the 'mapProps' higher-order component to modify an existing component’s API (its pr ...

  7. JS学习十四天----server端运行JS代码

    server端运行JS代码 话说,当今不在client使用JS代码才是稀罕事.因为web应用的体验越来越丰富,client用JS实现的逻辑也越来越多,这造成的结果就是某些差点儿一致的逻辑须要在clie ...

  8. xv6进程切换-swtch函数

    https://blog.csdn.net/Swartz2015/article/details/61615603 xv6进程切换-swtch函数 进程切换中由于需要保存当前进程的寄存器状态信息,又要 ...

  9. 【心情】bjdldrz

    如果我对勇士的记忆停留在73胜10负,

  10. oracle listener.ora的host不能使localhost,而应该是该机器名,否则不能用ip地址进行连接

    # listener.ora Network Configuration File: /u01/app/oracle/product/11.2.0/dbhome_1/network/admin/lis ...