docker入門教程-安裝及使用

安裝

官方文檔

Docker提供了以下Linux發(fā)行版和體系結(jié)構(gòu)中的.deb和.rpm軟件包:

docker linux

這里我們以centos為例(新版Docker已不支持centos 7以下的版本,所以centos 6.x 慎用):

  • 卸載舊版

    sudo yum remove docker \
                      docker-client \
                      docker-client-latest \
                      docker-common \
                      docker-latest \
                      docker-latest-logrotate \
                      docker-logrotate \
                      docker-engine
    
  • 設(shè)置倉庫

    # 安裝需要的工具
    sudo yum install -y yum-utils \
      device-mapper-persistent-data \
      lvm2
    # 設(shè)置docker倉庫
    sudo yum-config-manager \
        --add-repo \
        https://download.docker.com/linux/centos/docker-ce.repo
    
  • 安裝docker

    sudo yum install docker-ce docker-ce-cli containerd.io
    

Docker的使用

Docker技術(shù)的核心是image、container、networkvolume

舊版的docker客戶端的命令一般是 docker command的方式的,新版的Docker為了更好的區(qū)分不同模塊的命令,可以支持類似docker image ls這樣的命令(docker module command)。

Docker 鏡像(image

鏡像是創(chuàng)建容器的只讀模板

指令 描述
ls 列出鏡像
build 構(gòu)建鏡像來自Dockerfile
history 查看鏡像歷史
inspect 顯示一個或多個鏡像詳細信息
pull 從鏡像倉庫拉取鏡像
push 推送一個鏡像到鏡像倉庫
rm 移除一個或多個鏡像
prune 移除未使用的鏡像。沒有被標記或被任何容器引用的。
tag 創(chuàng)建一個引用源鏡像標記目標鏡像
export 導出容器文件系統(tǒng)到tar歸檔文件
import 導入容器文件系統(tǒng)tar歸檔文件創(chuàng)建鏡像
save 保存一個或多個鏡像到一個tar歸檔文件
load 加載鏡像來自tar歸檔或標準輸入
[root@cloudfile ~]# docker image -h
Flag shorthand -h has been deprecated, please use --help

Usage:  docker image COMMAND

Manage images

Commands:
  build       Build an image from a Dockerfile
  history     Show the history of an image
  import      Import the contents from a tarball to create a filesystem image
  inspect     Display detailed information on one or more images
  load        Load an image from a tar archive or STDIN
  ls          List images
  prune       Remove unused images
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rm          Remove one or more images
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

Run 'docker image COMMAND --help' for more information on a command.
[root@cloudfile ~]# 

示例:拉取nginx鏡像

[root@cloudfile ~]# docker image ls #查看本地已有的鏡像
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
postgres            9.6                 6782500d4eb1        2 weeks ago         200MB
kong                latest              7a800e1cef72        3 weeks ago         185MB
[root@cloudfile ~]# docker pull nginx    # 可以省略images, 默認拉取最新版本,指定版本可以在鏡像名后面添加版本號,例如 docker pull nginx:1.14
Using default tag: latest
latest: Pulling from library/nginx
afb6ec6fdc1c: Pull complete 
b90c53a0b692: Pull complete 
11fa52a0fdc0: Pull complete 
Digest: sha256:6fff55753e3b34e36e24e37039ee9eae1fe38a6420d8ae16ef37c92d1eb26699
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
You have new mail in /var/spool/mail/root
[root@cloudfile ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
postgres            9.6                 6782500d4eb1        2 weeks ago         200MB
nginx               latest              9beeba249f3e        2 weeks ago         127MB
kong                latest              7a800e1cef72        3 weeks ago         185MB
[root@cloudfile ~]# 

Docker 容器(container

創(chuàng)建容器

Docker 使用 run+image name 命令來創(chuàng)建容器,創(chuàng)建一個容器的時候可以附加許多指令來設(shè)置很多條件

設(shè)置相關(guān)指令
指令 描述
-i, --interactive 交互式
-t, --tty 分配一個偽終端
-d, --detach 運行容器到后臺
-a, --attach list 附加到運行的容器
--dns list 設(shè)置DNS服務(wù)器
-e, --env list 設(shè)置環(huán)境變量
--env-file list 從文件讀取環(huán)境變量
-p, --publish list 發(fā)布容器端口到主機
-P, --publish-all 發(fā)布容器所有EXPOSE的端口到宿主機隨機端口
-h, --hostname string 設(shè)置容器主機名
--ip string 指定容器IP,只能用于自定義網(wǎng)絡(luò)
--link list 添加連接到另一個容器
--network netwokname 連接容器到一個網(wǎng)絡(luò)
--mount mount 掛載宿主機分區(qū)到容器
-v, --volume list 掛載宿主機目錄到容器
--restart string 容器退出時重啟策略,默認no [always|on-failure]
--add-host list 添加其他主機到容器中/etc/hosts
資源限制指令
資源限制指令 描述
-m,--memory 容器可以使用的最大內(nèi)存量
--memory-swap 允許交換到磁盤的內(nèi)存量
--memory-swappiness=<0-100> 容器使用SWAP分區(qū)交換的百分比(0-100,默認為-1)
--memory-reservation 內(nèi)存軟限制,Docker檢測主機容器爭用或內(nèi)存不足時所激活的軟限制,使用此選項,值必須設(shè)置低于—memory,以使其優(yōu)先
--oom-kill-disable 當宿主機內(nèi)存不足時,內(nèi)核會殺死容器中的進程。建議設(shè)置了-memory選項再禁用OOM。如果沒有設(shè)置,主機可能會耗盡內(nèi)存
--cpus 限制容器可以使用多少可用的CPU資源
--cpuset-cpus 限制容器可以使用特定的CPU
--cpu-shares 此值設(shè)置為大于或小于默認1024值,以增加或減少容器的權(quán)重,并使其可以訪問主機CPU周期的更大或更小比例
示例:創(chuàng)建一個nginx容器
docker run -tid --name dev-nginx -p 8080:80 nginx
-tid 這里使用了指令連寫的方式,使用了t i d這三個指令 表示是交互式的、分配偽終端、運行容器到后臺,
--name 將這個容器命名為dev-nginx
-p 8080:80 是將宿主機的8080端口轉(zhuǎn)發(fā)到容器的80端口
[root@cloudfile ~]# docker ps #查看運行中的進程
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@cloudfile ~]# docker run -tid --name dev-nginx -p 8080:80 nginx # 創(chuàng)建dev-nginx容器
e0b8a178c45918bb9b0da9fc696690eb99cdb24db4b008381e8a2b1d959fbbf0 #docker引擎分配的容器id
[root@cloudfile ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
e0b8a178c459        nginx               "nginx -g 'daemon of…"   2 seconds ago       Up 2 seconds        0.0.0.0:8080->80/tcp   dev-nginx
[root@cloudfile ~]# 

使用容器

容器創(chuàng)建成功之后,我們就有許多其他的命令可以用來操作這個容器了。

指令 描述
ls 列出容器
inspect 顯示一個或多個容器詳細信息
attach 附加本地標準輸入,輸出和錯誤到一個運行的容器
exec 在運行容器中執(zhí)行命令
commit 創(chuàng)建一個新鏡像來自一個容器
cp 拷貝文件/文件夾到一個容器
logs 獲取一個容器日志
port 列出或指定容器端口映射
stats 顯示容器資源使用統(tǒng)計
top 顯示一個容器運行的進程
update 更新一個或多個容器配置
stop/start 停止/啟動一個或多個容器
rm 刪除一個或多個容器

容器啟動成功之后,Docker引擎會自動分配一個containerId,這個id可以用來唯一標識這個容器,也可以使用通過--name來命名容器作為唯一標識。我們可以使用docker stop/start/rm ... containerName/prefix4(containerId)格式來操作容器,其中containerName是容器名稱,prefix4(containerId)為containerId至少前4位。

[root@cloudfile ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
e0b8a178c459        nginx               "nginx -g 'daemon of…"   2 hours ago         Up 2 hours          0.0.0.0:8080->80/tcp   dev-nginx
[root@cloudfile ~]# docker stop dev-nginx   # 停止dev-nginx
dev-nginx
[root@cloudfile ~]# docker rm dev-nginx     # 刪除dev-nginx
dev-nginx
[root@cloudfile ~]# docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@cloudfile ~]#
  • 進入容器

    docker exec -it containerId/containerName bash
    

    退出容器的終端 control+p+q

  • 查看容器日志

    docker log containerId/containerName
    
  • 自動重啟

    加上 `--restart=always`
    

Docker 網(wǎng)絡(luò)(network)

Docker 支持自定義network來提供給容器進行通訊,Docker 內(nèi)置了一個默認的bridge network,有未指定 network 的容器,默認連接到此 network 中,其網(wǎng)段為 172.17.0.1/16。所以,兩個未進行任何連接操作的容器是可以通過 IP 地址互相通信的,因為他們同在一個 network 下,但通訊只能通過 IP 地址進行(比如 ping 172.17.0.5),不可以通過容器名通信(比如 ping container-name)。但自定義創(chuàng)建的網(wǎng)絡(luò)可以通過容器名進行通信。

[root@cloudfile ~]# docker network --help

Usage:  docker network COMMAND

Manage networks

Commands:
  connect     Connect a container to a network
  create      Create a network
  disconnect  Disconnect a container from a network
  inspect     Display detailed information on one or more networks
  ls          List networks
  prune       Remove all unused networks
  rm          Remove one or more networks

Run 'docker network COMMAND --help' for more information on a command.
[root@cloudfile ~]# 

可以通過 docker network connect 指令將容器連接到一個 network,也可以在起容器(docker run 指令)時加入 --network 參數(shù)指定即將創(chuàng)建的容器加入到哪個 network,還可以通過 docker network disconnect 命令將容器移出自定義的 network。

示例:創(chuàng)建容器連接網(wǎng)絡(luò)

[root@cloudfile ~]# docker network list # 查看網(wǎng)絡(luò)節(jié)點列表
NETWORK ID          NAME                DRIVER              SCOPE
b0b2ac02a44e        bridge              bridge              local
e6b334287fd0        host                host                local
73cc4dd0610d        none                null                local
[root@cloudfile ~]# docker network create dev # 添加名為dev的列表
570b83327e2b3cfa53922a1b5f5c223d6f6166ace7069648fb0188295a6d6922
[root@cloudfile ~]# docker network list
NETWORK ID          NAME                DRIVER              SCOPE
b0b2ac02a44e        bridge              bridge              local
570b83327e2b        dev                 bridge              local
e6b334287fd0        host                host                local
73cc4dd0610d        none                null                local  
# 創(chuàng)建容器時指定網(wǎng)絡(luò)
[root@cloudfile ~]# docker run -tid --name dev-nginx-01 -p 8080:80 --network=dev nginx 
82c81a5d7ab07a2233e5dc4b74300c661aaef67de7ec7cee98be388c981dfa41
# 先創(chuàng)建容器
[root@cloudfile ~]# docker run -tid --name dev-nginx-02 -p 8081:80 nginx
0408d5b0f61d31d33751512014072d240b5ad25cb05aae57e4aac97071983e2f 
# 將容器連接至dev網(wǎng)絡(luò)
[root@cloudfile ~]# docker network connect dev dev-nginx-02 
[root@cloudfile ~]# 

Docker 卷(volume)

Docker提供三種不同的方式將數(shù)據(jù)從宿主機掛載到容器中:volumes,bind mounts和tmpfs。

  • volumes:Docker管理宿主機文件系統(tǒng)的一部分(/var/lib/docker/volumes)。
  • bind mounts:可以存儲在宿主機系統(tǒng)的任意位置。
  • tmpfs:掛載存儲在宿主機系統(tǒng)的內(nèi)存中,而不會寫入宿主機的文件系統(tǒng)。
Docker 數(shù)據(jù)管理

卷 Volume

當我們創(chuàng)建一個容器的時候,如果沒有指定卷,會自動創(chuàng)建一個圈以供容器存儲數(shù)據(jù),docker的卷默認存儲的位置在/var/lib/docker/volumes下,我們可以創(chuàng)建一個volume在創(chuàng)建容器的時候綁定,這樣能更加方便的管理容器的數(shù)據(jù),不過比起mount來說,mount會更加通用一些。

[root@cloudfile ~]# docker volume --help

Usage:  docker volume COMMAND

Manage volumes

Commands:
  create      Create a volume
  inspect     Display detailed information on one or more volumes
  ls          List volumes
  prune       Remove all unused local volumes
  rm          Remove one or more volumes

Run 'docker volume COMMAND --help' for more information on a command.
[root@cloudfile ~]# 
管理卷
  • 查看volume

    [root@cloudfile volumes]# docker volume list
    DRIVER              VOLUME NAME
    local               c4969d2e6be2f8ffedd12bdb983d6xxxx
    
  • 創(chuàng)建volume

    [root@cloudfile volumes]# docker volume create nginx-volume
    nginx-volume
    [root@cloudfile volumes]# docker volume list
    DRIVER              VOLUME NAME
    local               c4969d2e6be2f8ffedd12bdb983d6xxxx
    local               nginx-volume
    [root@cloudfile volumes]# 
    
  • 查看volume詳情

    [root@cloudfile volumes]# docker volume inspect nginx-volume
    [
        {
            "CreatedAt": "2020-06-01T16:07:20+08:00",
            "Driver": "local",
            "Labels": {},
            "Mountpoint": "/var/lib/docker/volumes/nginx-volume/_data",
            "Name": "nginx-volume",
            "Options": {},
            "Scope": "local"
        }
    ] 
    [root@cloudfile volumes]# 
    
  • 刪除volume

    docker volume rm nginx-volume
    
指定volume創(chuàng)建容器

兩種寫法:

  • docker run -itd --name=nginx-dev --mount src=nginx-volume,dst=/usr/share/nginx/html nginx
  • docker run -itd --name=nginx-dev -v nginx-vol:/usr/share/nginx/html nginx

通過上述命令啟動容器之后,我們可以看到/var/lib/docker/volumes/nginx-volume/_data生成了些nginx的默認文件,這個時候我們就可以將我們的靜態(tài)網(wǎng)頁放置在這里了。

[root@cloudfile _data]# pwd
/var/lib/docker/volumes/nginx-volume/_data
[root@cloudfile _data]# ls
50x.html  index.html
[root@cloudfile _data]# 

綁定volume的好處不言而喻,多個容器可以共享一個數(shù)據(jù)卷 比如多個nginx 指定一個html文件夾 這樣可以提供高可用。

Bind Mounts

我們知道了可以創(chuàng)建volume,然后綁定到容器中,可以達到數(shù)據(jù)管理以及數(shù)據(jù)卷共享的功能。但是為了更加方便管理,我們可以將已有的文件夾綁定到容器中。

  • docker run -d -it --name=nginx-dev --mount type=bind,src=/var/www,dst=/usr/share/nginx/html nginx
  • docker run -d -it --name=nginx-dev -v /var/www:/usr/share/nginx/html nginx

注意:

  • 如果源文件/目錄沒有存在,不會自動創(chuàng)建,會拋出一個錯誤。
  • 如果掛載目標在容器中非空目錄,則該目錄現(xiàn)有內(nèi)容將被隱藏。
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容