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安装成功,可以正常使用。

发布了269 篇原创文章 · 获赞 156 · 访问量 29万+

猜你喜欢

转载自blog.csdn.net/wuyujin1997/article/details/104341882