码迷,mamicode.com
首页 > 其他好文 > 详细

Docker 容器基本操作

时间:2018-02-16 22:33:00      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:host   done   exe   post   port   detach   int   err   gid   

启动容器

  • 运行一次命令并结束进程

    $ docker run [--name=cname] IMAGE [COMMAND][ARG...] 
    # run在新容器中执行命令
    # --name=cname 自定义容器名字

    由于第一使用ubuntu,而本地并不存在ubuntu的image,所以会自动执行pull操作拉取image

    $ docker run ubuntu echo 'hello the cruel world!'
    Unable to find image 'ubuntu:latest' locally
    latest: Pulling from library/ubuntu

    客户端CentOS执行命令docker run ubuntu echo ‘hello the cruel world!‘

    $ docker run ubuntu echo 'hello the cruel world!'
    hello the cruel world!
  • 启动交互式容器:

    $ docker run -i -t IMAGE /bin/bash
    # -i --interactive=ture | fasle 默认是 false 
    # -t --tty=true | false 默认是 false

    客户端CentOS执行命令docker run -i -t ubuntu /bin/bash

技术分享图片

  • 启动守护式容器:

    • 什么是守护式容器:
      • 能够长期运行
      • 没有交互式会话
      • 适合运行应用程序和服务
  • 第一种,从交互式变为守护式

    $ docker run-i-t IMAGE/bin/bash 
      Ctrl+P Ctrl+Q

    客户端CentOS执行命令docker start -i I2D

技术分享图片

  • 第二种,从运行起便是守护式

    $ docker run -d container_name [COMMAND] [ARG]
    # -d

    客户端CentOS执行命令docker run -d --name=Dtest ubuntu

    $ docker run -d --name=Dtest ubuntu
    0cdfa354a681f134421af606242716a80d1373089b909a9a937080c7c1027cce

    返回了container_id_

查看容器

  • 查看多个容器一般信息

    $ docker ps [-a] [-1]
    # 无参数时列出正在运行的容器
    # -a 列出所有容器
    # -l 列出新建容器
  • 详细查看单个容器信息

    $ docker inspect [container id] [container name]

    也可以用-f或者--format标志来选定查看结果

    $ sudo docker inspect --format='{{ .State.Running }}' daemon_dave 
    false

上面这条命令会返回容器的运行状态,示例中该状态为false。

查看容器的IP地址

$ sudo docker inspect --format '{{ .NetworkSettings.IPAddress }}? daemon一dave
172.17.0.2

除了查看容器,你还可以通过浏览/var/lib/docker目录来深入了解Docker的工作原 理。该目录存放着Docker镜像、容器以及容器的配置。所有的容器都保存在/var/lib/ docker/containers 目录下

重启容器

$ docker start [-i] container_name
# -i --interactive=ture | fasle 默认是 false 

客户端CentOS执行命令docker start -i ubuntu serene_torvalds

技术分享图片

停止容器

  • 停止守护式

    $ docker stop [-t] container_name
    -t, --time int   Seconds to wait for stop before killing it (default 10)
  • 停止交互式

    exit

删除容器

$ docker rm [-f] container_name
-f, --force     Force the removal of a running container (uses SIGKILL)
-v, --volumes   Remove the volumes associated with the container

目前,还没有办法一次删除所有容器,不过有个小技巧来删除全部容器。

$ docker stop `docker ps -a -q`
1b9f83b5bdb2

$ docker rm `docker ps -a -q`  
1b9f83b5bdb2

上面是反引号。

附加容器

从守护式变为交互式

$ docker attach [container_name] [container id]

客户端CentOS执行命令docker attach I2D

$ docker attach I2D
root@a48898b48251:/#

容器日志

$ docker logs --help
Usage:  docker logs [OPTIONS] CONTAINER
Fetch the logs of a container
Options:
      --details        Show extra details provided to logs
  -f, --follow         Follow log output
      --since string   Show logs since timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)
      --tail string    Number of lines to show from the end of the logs (default "all")
  -t, --timestamps     Show timestamps
      --until string   Show logs before a timestamp (e.g. 2013-01-02T13:23:37) or relative 

客户端CentOS执行命令让容器每秒打印hello world

$ docker run -d --name=Dtest ubuntu /bin/sh -c "while true; do echo hello world; sleep 1;done" 
f6ea2c0c091cb8e3d8cb94f8bf7284b2b1d7e271e4725545c9118178c433cadb
  • 默认查看所有日志:

    $ docker logs Dtest
    hello world
    hello world
    hello world
    hello world
    hello world
    hello world
    hello world。。。。。。

    然而这样并不能看出日志的价值所在

  • -t可以看到时间

    $ docker logs -t Dtest
    2018-01-27T14:18:11.908687857Z hello world
    2018-01-27T14:18:12.912134261Z hello world
    2018-01-27T14:18:13.919331171Z hello world
    2018-01-27T14:18:14.921899475Z hello world
    2018-01-27T14:18:15.930985832Z hello world
    2018-01-27T14:18:16.934391247Z hello world
    2018-01-27T14:18:17.939755671Z hello world
    2018-01-27T14:18:18.940269958Z hello world
    。。。。。。
  • -f一起跟踪日志

技术分享图片

  • --tail 0只查看最新日志

查看进程

docker top CONTAINER [ps OPTIONS]

启动新进程

虽然Docker的理念是一个容器运行一种服务,但是仍然需要在容器运行多个进程心满足需求

docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

Run a command in a running container

Options:
  -d, --detach               Detached mode: run command in the background
      --detach-keys string   Override the key sequence for detaching a container
  -e, --env list             Set environment variables
  -i, --interactive          Keep STDIN open even if not attached
      --privileged           Give extended privileges to the command
  -t, --tty                  Allocate a pseudo-TTY
  -u, --user string          Username or UID (format: <name|uid>[:<group|gid>])
  -w, --workdir string       Working directory inside the container

docker exec命令是Docker 1.3引入的,早期版本并不支持该命令。

  • 客户端CentOS执行命令docker exec -i -t Dtest /bin/bash

    $ docker exec -i -t Dtest /bin/bash
    root@f6ea2c0c091c:/# 

    CTRL+P,CTRL+Q退出交互式模式到守护模式,再次使用docker top,可以看到容器中多了一个进程

技术分享图片

停止守护式

$ docker stop 容器名 # 默认等等10秒
$ docker kill 容器名 # 粗暴的方式

端口映射

  • 映射所有端口
docker run -P -i-t ubuntu/bin/bash
# -P, --publish-all=true I false 默认为false 
# 为容器暴露的所有端口进行映射
  • 指定端口映射列表
-p,一publish=[]

containerPort
docker run -p 80 -i -t ubuntu /bin/bash 

hostPortcontainerPort
docker run -p 8080:80 -i -t ubuntu /bin/bash 

ip::containerPort
docker run -p 0.0.0.0:80 -i -t ubuntu /bin/bash 

ip : hostPort: containerPort
docker run -p 0.0.0.0:8080:80 -i -t ubuntu /bin/bash

自动重启容器

如果由于某种错误而导致容器停止运行,我们还可以通过--restart标志,让Docker 自动重新启动该容器。--restart标志会检查容器的退出代码,并据此来决定是否要重启容器。默认的行为是Docker不会重启容器。

$ sudo docker run —restart=always --name daemon_dave -d ubuntu / bin/sh -c "while true; do echo hello world; sleep 1; done"

在本例中,--restart标志被设置为always。无论容器的退出代码是什么,Docker 都会自动重启该容器。除了 always,我们还可以将这个标志设为on-failure,这样,只有当容器的退出代码为非0值的时候,才会自动重启。另外,on-failure还接受一个可 选的重启次数参数

--restart=on-failure:5

这样,当容器退出代码为非0时,Docker会尝试自动重启该容器,最多重启5次。

--restart标志是Dockerl.2.0引入的选项。

使用Docker帮助

  • man docker-run
  • man docker-logs
  • man docker-top
  • man docker-exec

Docker 容器基本操作

标签:host   done   exe   post   port   detach   int   err   gid   

原文地址:https://www.cnblogs.com/oneTOinf/p/8450663.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!