Ubuntu docker 命令行安装、运行测试

89 篇文章 0 订阅

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

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Ubuntu 18.04 是一种操作系统,而 Docker 是一种容器化技术。在 Ubuntu 18.04 上安装 Docker 可以让用户更方便地管理和部署应用程序。Docker 可以将应用程序及其依赖项打包成一个容器,使其可以在任何地方运行,而不必担心环境差异和依赖项冲突的问题。在 Ubuntu 18.04 上使用 Docker,可以提高应用程序的可移植性和可靠性,同时也可以节省部署和维护的时间和成本。 ### 回答2: Ubuntu 18.04是一个流的Linux操作系统版本,而Docker是一种开源的容器化平台。在Ubuntu 18.04上安装和使用Docker可以带来许多好处。 首先,Docker可以提供一个隔离的运行环境,使同时运行多个应用程序变得更加简单。通过容器化技术,应用程序和它们的依赖可以被打包在一个容器中,并在任何支持Docker的系统上运行,而不用担心运行环境的差异。这使得部署和迁移应用程序变得更加容易和灵活。 其次,Docker提供了轻量级和快速的应用程序部署方式。相比传统的虚拟化技术,Docker容器的启动和停止速度更快,资源消耗更低。这意味着开发人员可以更快速地进应用程序的测试和部署,并且可以更有效地利用服务器的资源。 此外,Docker还提供了丰富的容器管理工具和生态系统。通过Docker Hub,开发人员可以方便地分享和获取各种容器镜像,加快了应用程序的开发和部署速度。同时,Docker还提供了广泛的命令工具和API,使得容器的创建、配置和管理变得更加方便。 最后,Docker还支持自动化部署和扩展。通过使用Docker Compose和Docker Swarm等工具,可以轻松地编排和管理多个容器,实现应用程序的自动化部署和水平扩展。 总之,Ubuntu 18.04和Docker的结合提供了一个强大的开发和部署平台,使应用程序的开发、测试和部署变得更加快速、灵活和可靠。无论是开发人员还是系统管理员,都能从Ubuntu 18.04和Docker的组合中获得巨大的好处。 ### 回答3: Ubuntu 18.04是一个最新的稳定版本的Linux操作系统,而Docker是一个开源的容器化平台。下面是关于Ubuntu 18.04与Docker之间的相关内容。 首先是Ubuntu 18.04。它是由Canonical开发的一个免费的操作系统。它具有强大的性能和稳定性,并且支持广泛的硬件设备。Ubuntu 18.04的特点包括新的用户界面、改进的系统管理工具、强大的网络和安全功能等。它还提供了强大的软件库和应用商店,用户可以方便地安装和管理各种应用程序。 而Docker是一个开源的容器化平台,它可以帮助我们轻松地构建、部署和运行应用程序。Docker使用容器技术来隔离不同的应用程序和环境,从而实现更高效的资源利用和更简化的部署流程。Docker允许我们通过编写Dockerfile来定义容器的配置,然后使用Docker Engine来构建和运行容器。Docker Hub是Docker的官方仓库,我们可以从这里获取和分享Docker镜像,而Docker Compose可以帮助我们管理多个容器应用。 在Ubuntu 18.04中安装Docker非常简单。我们只需要运行几个命令就可以完成安装过程。一旦安装完毕,我们就可以使用Docker命令来管理和操作容器。例如,我们可以使用"docker run"命令运行一个容器,使用"docker ps"命令来查看正在运行的容器,使用"docker stop"命令来停止一个容器等等。 总结一下,在Ubuntu 18.04上安装和使用Docker非常方便。Ubuntu 18.04提供了强大的基础操作系统,而Docker则提供了灵活和高效的容器化平台。这两者可以相互结合,使我们能够更轻松地构建、部署和管理应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值