Docker-学习二(容器基本操作)

内容提要

docker容器相关操作,包括容器启停,查看,删除,创建,导入导出,内存配置等。

1.创建并启动容器

1.新建容器

可以使用docker create命令新建一个容器,例如:

 docker create -it ubuntu:latest

2.启动容器

使用docker create
命令新建的容器处于停止状态需要配合使用docker start 命令启动。

docker start ssf

3.新建并启动容器

可以使用docker run 命令来直接新建并启动容器。(如果本地镜像有则启动,没有则获取最新镜像)
这里用ubuntu镜像echo一个hello word

 docker run ubuntu /bin/echo 'Hello world'

下面的命令启动一个bash终端,允许用户进行交互 并在后台运行:

 docker run -it -d ubuntu:14.04 /bin/bash

—P参数是随机指定一个32768~61000的主机端口与容器绑定,而-p则是指定一个端口与容器绑定

docker run -d -p 80:80 nginx:v1.0
命令详解
-a, --attach=[]             Attach to STDIN, STDOUT or STDERR
--add-host=[]               Add a custom host-to-IP mapping (host:ip)
--blkio-weight=0            Block IO (relative weight), between 10 and 1000
-c, --cpu-shares=0          CPU shares (relative weight)
--cap-add=[]                Add Linux capabilities
--cap-drop=[]               Drop Linux capabilities
--cgroup-parent=            Optional parent cgroup for the container
--cidfile=                  Write the container ID to the file
--cpu-period=0              Limit CPU CFS (Completely Fair Scheduler) period
--cpu-quota=0               Limit the CPU CFS quota
--cpuset-cpus=              CPUs in which to allow execution (0-3, 0,1)
--cpuset-mems=              MEMs in which to allow execution (0-3, 0,1)
-d, --detach=false          Run container in background and print container ID
--device=[]                 Add a host device to the container
--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 file of environment variables
--expose=[]                 Expose a port or a range of ports
-h, --hostname=             Container host name
--help=false                Print usage
-i, --interactive=false     Keep STDIN open even if not attached
--init=                     Run container following specified init system container method (systemd)
--ipc=                      IPC namespace to use
-l, --label=[]              Set meta data on a container
--label-file=[]             Read in a line delimited file of labels
--link=[]                   Add link to another container
--log-driver=               Logging driver for container
--log-opt=[]                Log driver options
--lxc-conf=[]               Add custom lxc options
-m, --memory=               Memory limit
--mac-address=              Container MAC address (e.g. 92:d0:c6:0a:29:33)
--memory-swap=              Total memory (memory + swap), '-1' to disable swap
--name=                     Assign a name to the container
--net=bridge                Set the Network mode for the container
--oom-kill-disable=false    Disable OOM Killer
-P, --publish-all=false     Publish all exposed ports to random ports
-p, --publish=[]            Publish a container's port(s) to the host
--pid=                      PID namespace to use
--privileged=false          Give extended privileges to this container
--read-only=false           Mount the container's root filesystem as read only
--restart=no                Restart policy to apply when a container exits
--rm=false                  Automatically remove the container when it exits
--security-opt=[]           Security Options
--sig-proxy=true            Proxy received signals to the process
-t, --tty=false             Allocate a pseudo-TTY
-u, --user=                 Username or UID (format: <name|uid>[:<group|gid>])
--ulimit=[]                 Ulimit options
--uts=                      UTS namespace to use
-v, --volume=[]             Bind mount a volume
--volumes-from=[]           Mount volumes from the specified container(s)
-w, --workdir=              Working directory inside the container

2.终止容器

1.容器终止

可以使用docker stop来终止运行中的容器,格式为:
docker stop[-t|–time[=10]][CONTAINER…]
首先会向容器发送SIGTERM信号,等待一段超时时间(默认为 10秒)后,再发送 SIGKILL 信号来终止容器。

 docker stop ce5

使用 docker kill 命令直接强行终止容器。

docker kill ce5

2.容器重启

终止状态的容器可以使用docker start 启动

docker start ce5

在运行状态的容器可以使用docker restart重启

docker restart ce5

3.进入容器

进入容器有三种方法,其中主要推荐使用第一种exec方法,其他方法了解作用即可。

1.exec命令(重点)

Docker从1.3.0版本起提供了一个更加方便的exec命令,可以在容器内直接执行任意命令。该命令的基本格式为:
docker exec [-d|–detach] [–detach-keys[=[]]] [-i|–interactive] [–privileged]
[-t|–tty] [-u|–user[=USER]] CONTAINER COMMAND
[ARG…] 。

docker exec -it 243c32535da7 /bin/bash

通过指定- it 参数来保持标准输入打开,并且分配一个伪终端。

相关参数
·-i,--interactive=true| false:打开标准输入接受用户输入命令,默认为false;
·--privileged=true| false:是否给执行命令以高权限,默认为false;
·-t,--tty=true| false:分配伪终端,默认为false;
·- u,--user="":执行命令的用户名或ID。

2.attach命令

命令格式:docker attach [–detach-keys[=[]]] [–no-stdin] [–sig-proxy[=true]] CONTAINER
支持三个主要选项:

·--detach-keys[=[]]:指定退出attach 模式的快捷键序列,默认是 CTRL-p CTRL-q;
·--no-stdin=true| false:是否关闭标准输入,默认是保持打开;
·--sig-proxy=true| false:是否代理收到的系统信号给应用进程,默认为true

使用示例:

docker attach myredis 

当多个窗口同时用attach命令连到同一个容器的时候,所有窗口都会同步显示。当某个窗口因命令阻塞时,其他窗口也无法执行操作了。

3.nsenter工具

在util-linux 软件包版本2.23+中包含nsenter工具。如果系统中的 util-linux包没有该命令需下载。(略…)

4. 容器操作

在容器中操作时,使用ps 报错,是因为ps没有安装在基础wheezy图像。

apt-get update && apt-get install procps

同样在容器中安装vim等工具时,需要先使用apt-get update 同步一下容器内和服务器的更新源,才可安装。

4.删除容器

使用docker rm 命令删除处于终止或者退出状态的容器。命令格式为:docker rm[-f|–force][-l|–link][-v|–volumes]CONTAINER[CONTAINER…]

docker rm ce554267d7a4

默认情况下rm命令只能删除停止或者退出状态的容器,但是可以添加-f参数,将容器kill后强行删除。

docker rm -f ce554267d7a4

###相关参数

·- f,--force=false:是否强行终止并删除一个运行中的容器;
·- l,--link=false:删除容器的连接,但保留容器;
·- v,--volumes=false:删除容器挂载的数据卷。

5.导入导出容器

1.导出容器

命令的格式为 docker export[-o|-- output[=""]]CONTAINER

docker export -o test_for_run.tar ce5
 或
docker export e81 >test_for_stop.tar

2.导入容器

和导入镜像一样可以使用docker import将tar导入变成镜像,命令格式为:
docker import [-c|–change[=[]]] [-m|–message[=MESSAGE]] file|URL|-[REPOSITORY [:TAG]]
导入时可以定义标签信息

 docker import test_for_run.tar - test/ubuntu:v1.0

在使用docker load 导入由docker export保存容器生成的tar时会报异常信息:

open /var/lib/docker/tmp/docker-import-283259618/repositories: no such file or directory

导入容器tar需使用docker import命令

6.容器资源配置

使用docker stats命令来看当前服务器的内存,针对各个场景对相应容器分配合理内存资源。

docker stats

使用docker的update命令来对内存大小进行管理分配

docker update -m 350m  --memory-swap -1  mysqlserver

–memory-swap -1 参数是指不让容器和宿主机进行内存交换

--automated=true| false:仅显示自动创建的镜像,默认为否;
·--no-trunc=true| false:输出信息不截断显示,默认为否;
·- s,--stars=X:指定仅显示评价为指定星级以上的镜像,默认为0,即输出所有镜像。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1TWPwciG-1574869123039)(https://i.loli.net/2019/11/25/csIVayoYXUhRrDp.png)]

参考资料:Docker技术入门与实战

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值