Ubuntu docker 命令行安裝、運行測試

Intro

docker可用於開發、交付和運行應用程序
好處:docker可以使應用程序和基礎架構分開,以快速軟件軟件。
有了它,開發人員可以:專心調試應用,而不調試環境。在任何地方安全地編譯、分享應用程序。
正如官網所說:Debug your app, not your environment. Securely build and share any application, anywhere

docker:是一個應用容器引擎。用google的go語言開發,遵從Apache 2.0開源協議。
docker可以讓開發者打包自己的應用和依賴包到一個輕量級、可移植的容器中。然後發佈到任何流行的Linux實體機器上,也可以實現虛擬化。
容器:一個容器包含了完整的運行時環境:除了應用程序本身之外,這個應用所需的全部依賴、類庫、其他二進制文件、配置文件等,都統一被打入了一個稱爲容器鏡像的包中。通過將應用程序本身,和其依賴容器化,操作系統發行版本和其他基礎環境造成的差異,都被抽象掉了

安裝命令

  • 卸載舊版本
    sudo apt-get remove docker docker-engine docker.io containerd runc

  • 添加docker存儲庫
    sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
    其中$(lsb_release -cs)返回Ubuntu發行版的名稱。

  • 更新源 sudo apt-get update

  • 安裝依賴包(使可以通過HTTPS訪問存儲庫)
    sudo apt-get install apt-transport-https ca-certificates curl software-properties-common

  • 添加官方GPG祕鑰
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

  • 通過搜索指紋的最後8個字符,確認現在擁有指紋9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88的密鑰。
    sudo apt-key fingerprint 0EBFCD88

wuyujin@ubuntu18:~$ sudo apt-key fingerprint 0EBFCD88
pub   rsa4096 2017-02-22 [SCEA]
      9DC8 5822 9FC7 DD38 854A  E2D8 8D81 803C 0EBF CD88
uid           [ unknown] Docker Release (CE deb) <[email protected]>
sub   rsa4096 2017-02-22 [S]

wuyujin@ubuntu18:~$ 
  • 安裝docker-ce sudo apt-get install docker-ce
    或,想安裝指定版本的docker:
    • 查詢可以安裝的軟件包版本docker-ce的版本 apt-cache madison docker-ce
    • 安裝指定版本的軟件包 sudo apt-get install docker-ce=版本號
  • 查詢安裝位置 dpkg -L docker-ce

安裝流程

wuyujin@ubuntu18:~$ # 卸載舊版本
wuyujin@ubuntu18:~$ sudo apt-get remove docker docker-engine docker.io containerd runc
[sudo] password for wuyujin: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Package 'docker-engine' is not installed, so not removed
Package 'docker' is not installed, so not removed
Package 'containerd' is not installed, so not removed
Package 'docker.io' is not installed, so not removed
Package 'runc' is not installed, so not removed
The following packages were automatically installed and are no longer required:
  aufs-tools cgroupfs-mount containerd.io docker-ce-cli pigz
Use 'sudo apt autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 18 not upgraded.
wuyujin@ubuntu18:~$ # 添加docker存儲庫
wuyujin@ubuntu18:~$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
Hit:1 http://mirrors.aliyun.com/ubuntu bionic InRelease
Ign:2 http://dl.google.com/linux/chrome/deb stable InRelease                   
Hit:3 http://mirrors.aliyun.com/ubuntu bionic-security InRelease               
Hit:4 http://dl.google.com/linux/chrome/deb stable Release                     
Hit:5 http://mirrors.aliyun.com/ubuntu bionic-updates InRelease                
Ign:6 http://ppa.launchpad.net/fcitx-team/nightly/ubuntu bionic InRelease      
Hit:8 http://mirrors.aliyun.com/ubuntu bionic-proposed InRelease               
Hit:9 http://mirrors.aliyun.com/ubuntu bionic-backports InRelease              
Hit:10 http://ppa.launchpad.net/git-core/ppa/ubuntu bionic InRelease           
Hit:11 http://ppa.launchpad.net/morphis/anbox-support/ubuntu bionic InRelease  
Err:12 http://ppa.launchpad.net/fcitx-team/nightly/ubuntu bionic Release       
  404  Not Found [IP: 2001:67c:1560:8008::15 80]
Hit:13 http://packages.microsoft.com/repos/vscode stable InRelease             
Hit:14 https://download.docker.com/linux/ubuntu bionic InRelease               
Reading package lists... Done 
E: The repository 'http://ppa.launchpad.net/fcitx-team/nightly/ubuntu bionic Release' does not have a Release file.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.
wuyujin@ubuntu18:~$ # 更新源
wuyujin@ubuntu18:~$ sudo apt-get update
Hit:1 http://mirrors.aliyun.com/ubuntu bionic InRelease
Hit:2 http://mirrors.aliyun.com/ubuntu bionic-security InRelease               
Hit:3 http://mirrors.aliyun.com/ubuntu bionic-updates InRelease                
Hit:4 http://mirrors.aliyun.com/ubuntu bionic-proposed InRelease               
Hit:5 http://mirrors.aliyun.com/ubuntu bionic-backports InRelease              
Ign:6 http://dl.google.com/linux/chrome/deb stable InRelease                   
Hit:7 http://dl.google.com/linux/chrome/deb stable Release                     
Hit:8 https://download.docker.com/linux/ubuntu bionic InRelease                
Hit:10 http://packages.microsoft.com/repos/vscode stable InRelease             
Ign:11 http://ppa.launchpad.net/fcitx-team/nightly/ubuntu bionic InRelease    
Hit:12 http://ppa.launchpad.net/git-core/ppa/ubuntu bionic InRelease
Hit:13 http://ppa.launchpad.net/morphis/anbox-support/ubuntu bionic InRelease  
Err:14 http://ppa.launchpad.net/fcitx-team/nightly/ubuntu bionic Release       
  404  Not Found [IP: 2001:67c:1560:8008::15 80]
Reading package lists... Done
E: The repository 'http://ppa.launchpad.net/fcitx-team/nightly/ubuntu bionic Release' does not have a Release file.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.
wuyujin@ubuntu18:~$ # 安裝HTTPS相關依賴
wuyujin@ubuntu18:~$ sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
Reading package lists... Done
Building dependency tree       
Reading state information... Done
ca-certificates is already the newest version (20180409).
curl is already the newest version (7.58.0-2ubuntu3.8).
software-properties-common is already the newest version (0.96.24.32.12).
apt-transport-https is already the newest version (1.6.12).
The following packages were automatically installed and are no longer required:
  aufs-tools cgroupfs-mount containerd.io docker-ce-cli pigz
Use 'sudo apt autoremove' to remove them.
wuyujin@ubuntu18:~$ # 添加docker的官方GPG祕鑰
wuyujin@ubuntu18:~$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
OK
wuyujin@ubuntu18:~$ # 確認指紋
wuyujin@ubuntu18:~$ sudo apt-key fingerprint 0EBFCD88
pub   rsa4096 2017-02-22 [SCEA]
      9DC8 5822 9FC7 DD38 854A  E2D8 8D81 803C 0EBF CD88
uid           [ unknown] Docker Release (CE deb) <[email protected]>
sub   rsa4096 2017-02-22 [S]

wuyujin@ubuntu18:~$ # 安裝docker社區版(Community Edition)
wuyujin@ubuntu18:~$ sudo apt-get install docker-ce
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed:
  docker-ce
0 upgraded, 1 newly installed, 0 to remove and 18 not upgraded.
Need to get 0 B/22.9 MB of archives.
After this operation, 109 MB of additional disk space will be used.
Selecting previously unselected package docker-ce.
(Reading database ... 182193 files and directories currently installed.)
Preparing to unpack .../docker-ce_5%3a19.03.6~3-0~ubuntu-bionic_amd64.deb ...
Unpacking docker-ce (5:19.03.6~3-0~ubuntu-bionic) ...
Setting up docker-ce (5:19.03.6~3-0~ubuntu-bionic) ...
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /lib/systemd/system/docker.service.
Created symlink /etc/systemd/system/sockets.target.wants/docker.socket → /lib/systemd/system/docker.socket.
Processing triggers for systemd (237-3ubuntu10.39) ...
Processing triggers for ureadahead (0.100.0-21) ...
ureadahead will be reprofiled on next reboot
wuyujin@ubuntu18:~$ # 查看docker-ce的安裝位置
wuyujin@ubuntu18:~$ dpkg -L docker-ce
/.
/etc
/etc/default
/etc/default/docker
/etc/init
/etc/init/docker.conf
/etc/init.d
/etc/init.d/docker
/lib
/lib/systemd
/lib/systemd/system
/lib/systemd/system/docker.service
/lib/systemd/system/docker.socket
/usr
/usr/bin
/usr/bin/docker-init
/usr/bin/docker-proxy
/usr/bin/dockerd
/usr/share
/usr/share/doc
/usr/share/doc/docker-ce
/usr/share/doc/docker-ce/README.md
/usr/share/doc/docker-ce/changelog.Debian.gz
/var
/var/lib
/var/lib/docker-engine
/var/lib/docker-engine/distribution_based_engine.json
wuyujin@ubuntu18:~$ 

運行測試

wuyujin@ubuntu18:~$ # 查看docker版本
wuyujin@ubuntu18:~$ docker --version
Docker version 19.03.6, build 369ce74a3c
wuyujin@ubuntu18:~$ # 運行測試
wuyujin@ubuntu18:~$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
docker: Error response from daemon: Get https://registry-1.docker.io/v2/: net/http: TLS handshake timeout.
See 'docker run --help'.
wuyujin@ubuntu18:~$ 

可以看到,第二步運行hello-world有報錯。
之所以報錯,是因爲docker默認鏡像源的網絡連接問題。
解決方案:修修改docker默認鏡像源爲國內的。
命令如下:
cd /etc/docker 進入docker的配置文件夾
sudo gedit daemon.json 新建配置文件,並編輯,內容如下:
json { "registry-mirrors": ["https://registry.docker-cn.com","http://hub-mirror.c.163.com"] }
docker run hello-world 重新運行

wuyujin@ubuntu18:/etc/docker$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete 
Digest: sha256:9572f7cdcee8591948c2963463447a53466950b3fc15a247fcad1917ca215a2f
Status: Downloaded newer image for hello-world:latest

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/

wuyujin@ubuntu18:/etc/docker$ 

輸出以上內容,則表示Docker安裝成功,可以正常使用。

發佈了283 篇原創文章 · 獲贊 156 · 訪問量 30萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章