第一部分 安裝與簡單教程
docker 安裝
// ubuntu 維護的方式
$ sudo apt install docker.io
// docker 官方維護的方式
$ curl -sSL https://get.docker.com/ | sh
docker 查詢版本
$ docker version
啓動 docker service
$ service docker start
查找 庫
$ docker search tutorial
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
learn/tutorial 40
georgeyord/reactjs-tutorial This is the backend of the React comment box… 5 [OK]
microsoft/aci-tutorial-app 2
fiware/tutorials.tourguide-app FIWARE Tour Guide App sample application 1 [OK]
mhausenblas/kairosdb-tutorial GitHub fetcher for KairosDB tutorial 1 [OK]
microsoft/aci-tutorial-sidecar 1
muli/gluon-tutorials-zh https://github.com/mli/gluon-tutorials-zh 1 [OK]
chris24walsh/flask-aws-tutorial Runs a simple flask webapp demo, with the op… 1 [OK]
starkandwayne/concourse-tutorial 0
fgraichen/concourse-tutorial-hello-world 0
activeeon/par-connector-tutorial Do the par-connector tutorial with R. The tu… 0 [OK]
camphor/python-tutorial camphor-/python-tutorial 0 [OK]
splicemachine/tutorial-spark-kafka-consumer Spark Streaming Tutorial 0
mukeshkale/concourse-tutorial-hello-world 0
krishnatest/docker-nodejs-tutorialkk docker-nodejs-tutorialkk 0
rade95/concourse-tutorial-hello-world 0
jhinds/flask-tutorial 0
helinwang/paddle-k8s-tutorial 0
annefou/esm-python-tutorials Docker image for jupyterhub on Sigma2 appsto… 0 [OK]
sahotay/concourse-tutorial-hello-world 0
kidikarus/concourse-tutorial-47-tasks 0
lukasheinrich/quickana-tutorial Image for the analysis code built from https… 0
starkandwayne/concourse-tutorial-ci 0
fiware/tutorials.context-provider Context Provider used within the FIWARE Step… 0
bluerdocker/concourse-tutorial-hello-world 0
下載 庫
$ sudo docker pull learn/tutorial
Using default tag: latest
latest: Pulling from learn/tutorial
271134aeb542: Pull complete
Digest: sha256:2933b82e7c2a72ad8ea89d58af5d1472e35dacd5b7233577483f58ff8f9338bd
Status: Downloaded newer image for learn/tutorial:latest
從下載的庫中 啓動容器, 輸出 hello world!
$ sudo docker run learn/tutorial echo 'hello world!'
hello world!
容器內 安裝 ping 工具
$ sudo docker run learn/tutorial apt-get install -y ping
Reading package lists...
Building dependency tree...
The following NEW packages will be installed:
iputils-ping
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 56.1 kB of archives.
After this operation, 143 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu/ precise/main iputils-ping amd64 3:20101006-1ubuntu1 [56.1 kB]
debconf: delaying package configuration, since apt-utils is not installed
Fetched 56.1 kB in 1s (41.6 kB/s)
Selecting previously unselected package iputils-ping.
(Reading database ... 7545 files and directories currently installed.)
Unpacking iputils-ping (from .../iputils-ping_3%3a20101006-1ubuntu1_amd64.deb) ...
Setting up iputils-ping (3:20101006-1ubuntu1) ...
查看本地的 容器
$ sudo docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0eae7d396f51 learn/tutorial "ping www.baidu.com" 35 seconds ago Created lucid_ellis
提交 改動過的容器
sudo docker commit 0eae7d396f51 learn/ping
sha256:913ffd4d3cb50b83936319f1feec2226051168fa442d636af13bc59b463c756d
容器內使用 ping 命令
$ sudo docker run learn/ping ping www.baidu.com
PING www.a.shifen.com (115.239.210.27) 56(84) bytes of data.
64 bytes from 115.239.210.27: icmp_req=1 ttl=53 time=13.5 ms
64 bytes from 115.239.210.27: icmp_req=2 ttl=53 time=13.6 ms
64 bytes from 115.239.210.27: icmp_req=3 ttl=53 time=16.5 ms
64 bytes from 115.239.210.27: icmp_req=4 ttl=53 time=17.2 ms
64 bytes from 115.239.210.27: icmp_req=5 ttl=53 time=13.2 ms
^C
--- www.a.shifen.com ping statistics ---
13 packets transmitted, 13 received, 0% packet loss, time 16254ms
rtt min/avg/max/mdev = 13.202/19.764/53.950/10.438 ms
查看鏡像
sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
learn/ping latest 82b6c745aa80 45 minutes ago 140MB
learn/tutorial latest a7876479f1aa 6 years ago 128MB
免去 sudo 執行
$ sudo groupadd docker
$ sudo gpasswd -a ${USER} docker
$ sudo service docker restart
// 重啓計算機 或者 log out 後重新登錄
第二部分 進階命令學習
交互式容器
- 單詞執行命令, 執行完成後,容器停止運行
docker run [images] .[comand]
$ docker run learn/tutorial echo 'hello world!'
- 啓動交互式命令
-i --interactive=true 打開交互
-t --tty=true 打開一個tty終端
$ docker run -i -t learn/tutorial /bin/bash
root@021445a7d77c:/#
root@021445a7d77c:/# exit // 停止
- 查看容器
正在運行的容器
-l 最新的容器
-a 全部的容器
docker ps -a -l
- 自定義 container 的名字
$ docker run --name=ping01 -i -t learn/ping /bin/bash
- 根據定義的名字, 查看容器的配置
$ docker inspect ping01
之前的幾種方法,每次都重新創建了一個容器, 我們可以重新啓動已經停止的容器
6. 重新啓動停止的容器
-i 交互的方式啓動
$ docker start -i ping01
root@cce87c7af35f:/#
- 刪除已經停止的容器
docker rm learn/ping
守護式容器
交互式容器運行完就停止了,守護容器可以一直運行, 非常適合運行應用和服務
啓動方法一 ctrl p, ctrl q
$ docker run -i -t learn/ping /bin/bash
$ Ctrl + P Ctrl + Q // 方式 退出會讓容器後臺運行
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cce87c7af35f learn/ping "/bin/bash" 12 minutes ago Up 2 minutes ping01
再次進入運行中的容器
$ docker attach ping01
root@cce87c7af35f:/#
啓動方法二 run -d 命令
- 創建一個 後臺運行的容器 命名爲dc1
$ docker run --name dc1 -d learn/tutorial /bin/sh -c " while true; do echo hello world; sleep 1; done"
c88c6f6d3196ddd2a375ce0323c52cdb43fe31c7d8cf6a0b3c01cef12f251fa8
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c88c6f6d3196 learn/tutorial "/bin/sh -c ' while …" 32 seconds ago Up 30 seconds dc1
- 通過 logs 查詢 容器運行狀態
docker logs -f -t --tail 容器名
-f 不斷刷新 -t 加入時間戳 --tail 顯示末尾的
$ docker logs -f -t --tail 5 dc1
2019-04-14T07:45:43.958480341Z hello world
2019-04-14T07:45:44.962045188Z hello world
2019-04-14T07:45:45.963005862Z hello world
2019-04-14T07:45:46.965690641Z hello world
2019-04-14T07:45:47.968577102Z hello world
2019-04-14T07:45:48.970776116Z hello world
- 查看容器內運行的進程 top
$ docker top dc1
UID PID PPID C STIME TTY TIME CMD
root 7120 7088 0 15:42 ? 00:00:00 /bin/sh -c while true; do echo hello world; sleep 1; done
root 8230 7120 0 15:54 ? 00:00:00 sleep 1
- 在容器內啓動新進程 exec
docker exec -i -t dc1 /bin/bash
root@c88c6f6d3196:/#
$ docker top dc1
UID PID PPID C STIME TTY TIME CMD
root 7120 7088 0 15:42 ? 00:00:00 /bin/sh -c while true; do echo hello world; sleep 1; done
root 8499 7088 0 15:55 pts/0 00:00:00 /bin/bash
root 8586 7120 0 15:56 ? 00:00:00 sleep 1
- 停止運行中的容器 stop kill
docker stop dc1 // docker kill dc1