Docker container 容器介绍

容器操作

使用 docker 命令行操作 docker 容器

启动容器

[email protected] ~ $ 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

容器启动可以使用上述参数,下面是一些常见的参数的使用示例:

[email protected] ~ $ docker run -ti ubuntu:14.04  /bin/bash
[email protected]:/#
# -t 表示返回一个 tty 终端,-i 表示打开容器的标准输入,使用这个命令可以得到一个容器的 shell 终端,如果不使用 dockerfile 的话,可以使用这种方式来对容器做一些更改,然后使用「docker commit」提交到新的 image
#让我们打开另外一个终端,查看下当前运行的容器
[email protected] ~ $ 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 正处于运行中
[email protected]:/# exit
exit
#当我们 「exit」 退出的时候,再查看下「docker ps」
[email protected] ~ $ 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
#我们发现容器已经退出了,所以这种启动的方法不是我们使用容器来运行服务的好办法
[email protected] ~ $ 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
[email protected] ~ $ docker run -p 100:22 -d  base/163
6d9542bdc544e20b2f9718633a479e09047abef675ea24377d4daf4f04f20237
#这里我们使用了前面创建的 base/163 的容器,并使用了 -p 和 -d 2个参数,他们分别表示映射宿主主机端口和后台运行容器的意思
[email protected] ~ $ 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 服务差不多,我们会在后面的章节详细介绍
[email protected] ~ $ ssh [email protected] -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.

[email protected]:~#exit
logout
Connection to 127.0.0.1 closed.
[email protected] ~ $ 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 等与容器数据存储和网络相关的参数将相关章节再详细介绍

停止容器

[email protected] ~ $ 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.
#还可以设定在强制杀死容器进程之前等待多少秒来等待容器停止,一般使用默认值即可
[email protected] ~ $ docker stop 6d9
#也可以使用容器的名称来停止,停止多个容器在每个容器之间加空格即可
6d9
[email protected] ~ $ 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

删除容器

[email protected] ~ $ 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
  #删除容器相关的数据卷
[email protected] ~ $ docker start 6d9
6d9
[email protected] ~ $ 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」来启动它
[email protected] ~ $ docker start 6d9
6d9
[email protected] ~ $ 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
[email protected] ~ $ docker rm -f 6d9
#使用 -f 强制删除容器
6d9
[email protected] ~ $ 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
[email protected] ~ $ docker rm -l   tomcat001/tomysql
tomcat001/tomysql
#使用 -l 删除了之前建立的到 mysql 的连接
[email protected] ~ $ 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
[email protected] ~ $

导出容器

[email protected] ~ $ docker export

Usage: docker export CONTAINER

Export the contents of a filesystem as a tar archive to STDOUT
[email protected] ~ $ docker run -d base/163
5883b400a03ed81b76a22badeb8948642023dd4b64c083cc74e787a2e3d7f7a4
[email protected] ~ $ 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
[email protected] ~ $ docker export 588 >>base/163.tar
[email protected] ~/base $ ls -lh
total 227M
-rw-r--r-- 1 core core 227M Oct 16 03:00 163.tar
#导出了一个227M的文件,我们又可以将他导入成为 images

容器原理

更多内容请关注 http://www.dockerpool.com

时间: 2024-10-29 19:11:52

Docker container 容器介绍的相关文章

Docker 容器介绍

Docker 容器介绍 Docker 是一个基于 Go 语言的开源应用容器引擎,它既能实现虚拟化,又可用于将应用服务打包成轻量.可移植的容器,从而可以发布到任何 Linux 平台.除了优秀了沙箱机制外,Docker 容器的开销也极低. 正如其名,Docker 所做的事情正是以一个集装箱的身份承载应用服务的运行,它与传统的 KVM.Xen 等基于硬件虚拟化的云计算系统不同,而是采用了 LXC (Linux Container) 内核虚拟化的模式,使得应用服务不再需要独立的货轮(OS)为其提供运作环

进入正在运行的 docker 容器(docker container)

在使用 docker 容器的时候,我们总会想看看容器内部长什么样子:我们使用 docker exec 命令可以满足我们的期望: ? compose docker exec --help Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...] Run a command in a running container Options: -d, --detach Detached mode: run command in the backgrou

Hadoop 2.6.0 重要特性之 Docker Container Executor (DCE)

Docker Container Executor (DCE) 是 Hadoop 2.6.0 包含的一个重要特性:至此,大数据的巨人 Hadoop 也终于可以利用当前虚拟化/云计算领域的宠儿 Docker 的强大能力了. 对于 Docker 概念介绍的文章已经非常多了,因此本文就不再赘述,仅仅直接引用 Hadoop 社区里对其的描述:"Docker (https://www.docker.io/) combines an easy-to-use interface to Linux contai

Docker container 集装箱说明

容器操作 使用 docker 命令行操作 docker 容器 启动容器 [email protected] ~ $ 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 weig

Docker源码分析(八):Docker Container网络(下)

1.Docker Client配置容器网络模式 Docker目前支持4种网络模式,分别是bridge.host.container.none,Docker开发者可以根据自己的需求来确定最适合自己应用场景的网络模式. 从Docker Container网络创建流程图中可以看到,创建流程第一个涉及的Docker模块即为Docker Client.当然,这也十分好理解,毕竟Docker Container网络环境的创建需要由用户发起,用户根据自身对容器的需求,选择网络模式,并将其通过Docker Cl

Docker源码分析(七):Docker Container网络 (上)

1.前言(什么是Docker Container) 如今,Docker技术大行其道,大家在尝试以及玩转Docker的同时,肯定离不开一个概念,那就是“容器”或者“Docker Container”.那么我们首先从实现的角度来看看“容器”或者“Docker Container”到底为何物. 逐渐熟悉Docker之后,大家肯定会深深得感受到:应用程序在Docker Container内部的部署与运行非常便捷,只要有Dockerfile,应用一键式的部署运行绝对不是天方夜谭: Docker Conta

Docker实用命令介绍

Docker实用命令介绍 1. docker启动.关闭.停止 ╭─wil-xz in ~ 12:15:44 ╰─?(?﹏?.)? service docker restart Redirecting to /bin/systemctl restart docker.service ╭─wil-xz in ~ 12:15:44 ╰─?(?﹏?.)? service docker stop Redirecting to /bin/systemctl stop docker.service ╭─wil

Docker: 基础介绍及配置安装 [一]

一.Docker介绍 Docker是Docker.lnc公司开源的一个基于LXC技术之上构建的Container容器引擎,源代码托管在Github上,基于Go语言并遵从Apache2.0协议开源 Docker是通过内核虚拟化技术(namespaces及cgroups等)来提供容器的资源隔离与安全保障等.由于Docker通过操作系统层的虚拟化实现隔离,所以Docker容器在运行时,不需要类似虚拟机(VM)额外的操作系统开销,提高资源利用率 二.Docker的三大理念 构建 运输 运行 Docker

Docker: 限制容器可用的 CPU

原文:Docker: 限制容器可用的 CPU 默认情况下容器可以使用的主机 CPU 资源是不受限制的.和内存资源的使用一样,如果不对容器可以使用的 CPU 资源进行限制,一旦发生容器内程序异常使用 CPU 的情况,很可能把整个主机的 CPU 资源耗尽,从而导致更大的灾难.本文将介绍如何限制容器可以使用的 CPU 资源.本文的 demo 中会继续使用<Docker: 限制容器可用的内存>一文中创建的 docker 镜像 u-stress 进行压力测试,文中就不再过多的解释了. 限制可用的 CPU