Posted on

docker初探

  1. 安裝docker(於centos7)
  1. 測試用指令
  • 顯示 docker 的資訊
    $ docker info

  • 顯示 docker 的版本
    $ docker version

  • 取得一個指定版本的 image

  • 如果不指定 image 版本該版本則為 latest
    $ docker pull [Image 名稱]:[Image 版本]

  • ex:
    $ docker pull hello-world
    $ docker pull ubuntu
    $ docker pull ubuntu:14.04

  • 顯示 docker 的 images 清單
    $ docker images

  • 透過 iamge 執行並產生一個新的 container
    $ docker run [Image 名稱]:[Image 版本] [執行指令]

  • ex:
    $ docker run hello-world
    $ docker run ubuntu /bin/echo “example 1 – ubuntu”
    $ docker run ubuntu:14.04 /bin/echo “example 2 – ubuntu:14.04”

*********** 我是分隔線 ***********

  • [OPTIONS]:
  • -i, –interactive (互動模式)
  • -t, –tty (配置一個終端機)
  • -d, –detach (在背景執行)

$ docker run [OPTIONS] [Image 名稱]:[Image 版本] [執行指令]

  • ex:
    $ docker run -i -t ubuntu:14.04 bash
    $ exit

  • 查看正在執行的 containers
    $ docker ps

  • 查看所有的 containers
    $ docker ps -a

  • 對正在執行的 container 下執指令

  • [OPTIONS]:

  • -i, –interactive (互動模式)

  • -t, –tty (配置一個終端機)

  • docker exec [OPTIONS] [Container ID] [執行指令]

  • ex:

  • 產生一個 container 在背景執行
    $ docker run -i -t -d ubuntu:14.04 bash

  • 查詢正在執行的 container
    $ docker ps
    $ docker exec -i -t [Container ID] bash
    $ exit

  • 總結

  • 啟動 docker container
    $ docker start [Container ID]

  • 停止 docker container
    $ docker stop [Container ID]

  • 重新啟動 docker container
    $ docker restart [Container ID]

  • ex:

  • 產生一個 container 在背景執行
    $ docker run -i -t -d ubuntu bash

$ docker ps
$ docker stop [Container ID]

$ docker ps
$ docker start [Container ID]

$ docker ps
$ docker restart [Container ID]

  • 刪除 container
    $ docker rm [Container ID]

  • ex:
    $ docker ps
    $ docker stop [Container ID]

$ docker rm [Container ID]
$ docker ps -a

  • 刪除 image

  • 刪除 image 前必需將透過該 image 所產生的 container 移除
    $ docker rmi [Image ID]

  • ex:

  • 移除 hello-world image 所產生的 container
    $ docker ps -a
    $ docker rm [Container ID]
    $ docker ps -a

  • 移除 hello-world image
    $ docker images
    $ docker rmi [Image ID]
    $ docker images


** 小技巧 **************************


  • 停止所有的 containers
    $ docker stop $(docker ps -a -q)

  • 刪除所有的 containers
    $ docker rm $(docker ps -a -q)

  • 刪除所有的 images
    $ docker rmi $(docker images -a -q)

  • 運行容器並掛載主機捲
    Docker容器不會保存它們產生的數據。該過程完成後,容器將停止運行,並且容器中的所有物品都將被移走。

如果要在容器停止後仍要存儲持久性數據,則需要啟用共享存儲卷。

對於安裝卷,請使用-v屬性以及要在其中保存數據的目錄的指定位置,然後是該數據在容器內的位置。

-v [/host/volume/location]:[/container/storage]
整個docker container run命令是:

docker container run -v [/host/volume/location]:[/container/storage] [docker_image]

docker build -t fmsdocker . –no-cache

  • docker file 寫法 / run寫法
    https://docs.docker.com/engine/reference/run/

  • entrypoint
    docker run –entrypoint=/bin/hostname test

  • ENV
    ENV HOSTNAME testhost
    export today=Wednesday
    docker run -e “deep=purple” -e today –rm alpine env

  • HEALTHCHECK
    docker run –name=test -d
    –health-cmd=‘stat /etc/passwd || exit 1’
    –health-interval=2s
    busybox sleep 1d

HEALTHCHECK –interval=5s –timeout=3s
CMD curl ${HOSTNAME}:1111/admin/getServerStats?auser=admin&apswd=pass.123 || exit 1