安裝命令:
yum -y install docker
啓動 Docker 後臺服務:
<span style="color:#000000">service docker start</span>
運行hello-world命令:
[root@localhost ~]# docker run ansible/centos7-ansible:latest /bin/echo 'hello'
hello
[root@localhost ~]#
各個參數解析:
-
docker: Docker 的二進制執行文件。
-
run:與前面的 docker 組合來運行一個容器。
-
ansible/centos7-ansible:latest指定要運行的鏡像,Docker首先從本地主機上查找鏡像是否存在,如果不存在,Docker 就會從鏡像倉庫 Docker Hub 下載公共鏡像。
-
/bin/echo "Hello world": 在啓動的容器裏執行的命令
以上命令完整的意思可以解釋爲:Docker 以ansible/centos7-ansible:latest鏡像創建一個新容器,然後在容器裏執行 bin/echo "Hello world",然後輸出結果。
交互式啓動方式:
[root@localhost ~]# docker run -i -t ansible/centos7-ansible:latest /bin/bash
[root@367b51f5b233 ansible]#
[root@367b51f5b233 ansible]#
各個參數解析:
-
-t:在新容器內指定一個僞終端或終端。
-
-i:允許你對容器內的標準輸入 (STDIN) 進行交互。
我們可以通過運行exit命令或者使用CTRL+D來退出容器。
後臺啓動模式:
[root@localhost ~]# docker run -d ansible/centos7-ansible:latest /bin/bash -c "while true; do echo hello world; sleep 1; done"
20abcde0437d6d27d3536cd4e3de3054be71f6d3d18380218fe5fe6167a70911
20abcde0437d6d27d3536cd4e3de3054be71f6d3d18380218fe5fe6167a70911
<span style="color:#333333"><span style="color:#333333">[root@localhost ~]# </span>
</span>
查看後臺命令:
docker ps
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
20abcde0437d ansible/centos7-ansible:latest "/bin/bash -c 'whi..." 2 minutes ago Up 2 minutes amazing_sammet
8c17fc4c6d6f training/webapp "python app.py" 4 hours ago Up 4 hours 0.0.0.0:32768->5000/tcp determined_nightingale
[root@localhost ~]#
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
20abcde0437d ansible/centos7-ansible:latest "/bin/bash -c 'whi..." 2 minutes ago Up 2 minutes amazing_sammet
8c17fc4c6d6f training/webapp "python app.py" 4 hours ago Up 4 hours 0.0.0.0:32768->5000/tcp determined_nightingale
[root@localhost ~]#
查看日誌:docker logs id/name
[root@localhost ~]# docker logs 20abcde0437d
hello world
hello world
hello world
hello world
hello world
hello world
hello world
停止命令:docker stop
[root@localhost ~]# docker stop 20abcde0437d
20abcde0437d
[root@localhost ~]#
查看鏡像:
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/hello-world latest f2a91732366c 3 months ago 1.85 kB
docker.io/ansible/centos7-ansible latest 688353a31fde 15 months ago 447 MB
docker.io/training/webapp latest 6fae60ef3446 2 years ago 349 MB
[root@localhost ~]#
各個選項說明:
-
REPOSITORY:表示鏡像的倉庫源
-
TAG:鏡像的標籤
-
IMAGE ID:鏡像ID
-
CREATED:鏡像創建時間
-
SIZE:鏡像大小
查找鏡像命令:docker search name
[root@localhost ~]# docker search http
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/httpd The Apache HTTP Server Project 1569 [OK]
docker.io docker.io/haproxy HAProxy - The Reliable, High Performance T... 913 [OK]
docker.io docker.io/jruby JRuby (http://www.jruby.org) is an impleme... 62 [OK]
docker.io docker.io/hypriot/rpi-busybox-httpd Raspberry Pi compatible Docker Image with ... 40
[root@localhost ~]#
獲取docker鏡像:docker pull name
[root@localhost ~]# docker pull httpd
Using default tag: latest
Trying to pull repository docker.io/library/httpd ...
latest: Pulling from docker.io/library/httpd
f2b6b4884fc8: Pull complete
b58fe2a5c9f1: Pull complete
e797fea70c45: Pull complete
6c7b4723e810: Pull complete
02074013c987: Pull complete
a4a11b801d86: Pull complete
70d17a98bee0: Pull complete
Digest: sha256:8359424a58cf59f1ea6a1a55e3974d5d569a510ebec0004357cf200adce5f27a
Status: Downloaded newer image for docker.io/httpd:latest
[root@localhost ~]#