Docker_01_安裝

Docker的安裝使用

  • 詳細信息可以 自行查看 Docker_官網
  • 本次使用環境 Centos 7, 使用 root 超級用戶登錄
  • Docker 版本 19.03, 需要Centos7
  • 服務器的版本如果和官網上的不同, 會遇到不同的問題, 建議使用指定版本.

1. Install On Centos In Yum

1.1 查看服務器之前是否, 安裝Docker

使用 yum 查看服務器之前是否安裝Docker 的歷史版本,
如果存在先, 刪除

# 查看按照列表
yum list installed | grep 'docker'

yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine
1.2 安裝Docker 引擎 (docker Engine)
  1. 安裝最新版本的命令
yum install docker-ce docker-ce-cli containerd.io
  1. 安裝指定的版本的docker
    a. 獲得所有版本信息
  • 獲得所有docker-ce 的版本信息, 並按照 版本排序
yum list docker-ce --showduplicates | sort -r

docker-ce.x86_64    3:19.03.8-3.el7                            docker-ce-test   
docker-ce.x86_64    3:19.03.8-3.el7                            docker-ce-stable 
docker-ce.x86_64    3:19.03.8-3.el7                            @docker-ce-stable
docker-ce.x86_64    3:19.03.7-3.el7                            
...
  • 上面返回的是所有可以安裝的信息, 需要根據自己Centos的版本 下載對應版本的docker(.el7)
    b. 安裝指定版本命令
    例如安裝19.03.8 的命令
yum install docker-ce-19.03.8 docker-ce-cli-19.03.8 containerd.io
1.3. Hello World

安裝成功後, 我們可以使用hello world來測試
a. 啓動docker

systemctl start docker

# 查看docker狀態
systemctl status docker
# or
ps -ef | grep docker

b. 運行hello-world
如果第一次運行, 會發現本地不存在對應鏡像, 會從遠程倉庫下載, 再運行

$ docker run hello-world

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete 
Digest: sha256:8e3114318a995a1ee497790535e7b88365222a21771ae7e53687ad76563e8e76
Status: Downloaded newer image for hello-world:latest

#下面是hello world的內容
Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

1.3 添加組, 不使用 sudo 命令操作docker

# 創建組
$ groupadd docker

# 將用戶添加到組
$ usermod -aG docker $USER

# 登入 docker 羣組
$ newgrp docker

# 以後命令在不是root 的用戶下, 也可以不使用 sudo 了
$ docker run hello-world

2. Install from a package

如果你不想使用 docker 倉庫來按照docker, 可以下載 docker 的 .rpm文件來安裝.

2.1. 下載 rpm 文件

去 官網 https://download.docker.com/linux/centos/ 選擇下載, 對應需要版本的 .rpm文件

2.2 命令安裝

$ yum install /path/to/package.rpm

3. 安裝完成後的常用配置

3.1 設置docker服務開機啓動

# 開啓自啓動
$ systemctl enable docker 
## or
$ chkconfig docker on

# 關閉開機自啓動
$ systemctl disable docker
## or
chkconfig docker off

4. 卸載 Docker

4.1. 使用 yum卸載

# 查看安裝列表 
$ yum list installed | grep docker

containerd.io.x86_64               1.2.13-3.1.el7                 @docker-ce-nightly
docker-ce.x86_64                   3:19.03.8-3.el7                @docker-ce-stable
docker-ce-cli.x86_64               1:19.03.8-3.el7                @docker-ce-stable

# 卸載
$ yum remove docker-ce docker-ce-cli containerd.io

4.2. 移除 docker 的鏡像 等相關文件

$ rm -rf /var/lib/docker
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章