1. 程式人生 > >Docker 容器基本操作

Docker 容器基本操作

ron ... sin exit 跟蹤 tor ins running detach

啟動容器

  • 運行一次命令並結束進程

    $ 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]?

重啟容器

$ 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 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
  • 客戶端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

使用Docker幫助

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

Docker 容器基本操作