Docker的安装与启动

安装Docker

更新 apt 包索引,安装 apt 依赖包,用于通过HTTPS来获取仓库:

sudo apt update
sudo apt install apt-transport-https ca-certificates curl gnupg-agent software-properties-common

添加 Docker 的官方 GPG 密钥:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

验证现在是否拥有带有指纹的密钥:

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]

设置稳定版仓库:

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

更新 apt 包索引:

sudo apt-get update

以下列出两种方式安装不同版本的docker,如果安装最新版本,直接跳到2. 安装最新版本的 Docker Engine-Community 和 containerd

  1. 列出仓库中可用的版本:
apt-cache madison docker-ce
# docker-ce | 5:20.10.4~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
# docker-ce | 5:20.10.3~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
# docker-ce | 5:20.10.2~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
# docker-ce | 5:20.10.1~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
# docker-ce | 5:20.10.0~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
# docker-ce | 5:19.03.15~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
# docker-ce | 5:19.03.14~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
# docker-ce | 5:19.03.13~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
# docker-ce | 5:19.03.12~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
# docker-ce | 5:19.03.11~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
# docker-ce | 5:19.03.10~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
# docker-ce | 5:19.03.9~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages

要安装特定版本的 Docker Engine-Community,请在仓库中列出可用版本,然后选择一种安装。

sudo apt-get install docker-ce=<VERSION_STRING> docker-ce-cli=<VERSION_STRING> containerd.io

使用版本字符串安装特定版本,例如 5:20.10.4~3-0~ubuntu-focal

sudo apt-get install docker-ce=5:20.10.4~3-0~ubuntu-focal docker-ce-cli=5:20.10.4~3-0~ubuntu-focal containerd.io
  1. 安装最新版本的 Docker Engine-Community 和 containerd :
sudo apt-get install docker-ce docker-ce-cli containerd.io

查看docker版本:

docker --version
# Docker version 20.10.4, build d3cb89e

运行一个hello-world,测试 Docker 是否安装成功,输入以下指令,打印出以下信息则安装成功:

sudo docker run hello-world
# Unable to find image 'hello-world:latest' locally
# latest: Pulling from library/hello-world
# 0e03bdcc26d7: Pull complete 
# Digest: sha256:7e02330c713f93b1d3e4c5003350d0dbe215ca269dd1d84a4abc577908344b30
# 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/

列出容器:

sudo docker ps -a
# CONTAINER ID   IMAGE         COMMAND    CREATED              STATUS                          PORTS     NAMES
# 725112b9bd48   hello-world   "/hello"   About a minute ago   Exited (0) About a minute ago             silly_einstein

-a:显示所有的容器,包括未运行的

运行一个ubuntu的容器:

docker run -it ubuntu bash

-i: 以交互模式运行容器,通常与 -t 同时使用
-t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用

退出交互模式:

exit

列出容器:

sudo docker ps -a
# CONTAINER ID   IMAGE         COMMAND    CREATED              STATUS                        PORTS     NAMES
# 1ef02bc9515d   ubuntu        "bash"     About a minute ago   Exited (130) 10 seconds ago             confident_hermann
# 725112b9bd48   hello-world   "/hello"   6 minutes ago        Exited (0) 6 minutes ago                silly_einstein

非Root用户身份执行Docker

如果要使用 Docker 作为非 root 用户,将用户添加到 docker 组:

sudo usermod -aG docker $USER

-a: 仅和-G一块使用,将用户添加到附属组群
-G: 修改用户所属的附加群组;在改变用户sudo权限时就可以使用这个选
$USER是一个环境变量,代表当前用户名
登出,并且重新登录,以便用户组会员信息刷新,也可以使用newgrp docker临时生效

查看当前用户所属的组:

groups $USER | grep docker

现在可以不用sudo,直接查看进程:

docker ps -a
# CONTAINER ID   IMAGE         COMMAND    CREATED             STATUS                           PORTS     NAMES
# 1ef02bc9515d   ubuntu        "bash"     About an hour ago   Exited (130) About an hour ago             confident_hermann
# 725112b9bd48   hello-world   "/hello"   About an hour ago   Exited (0) About an hour ago               silly_einstein

假如出现权限问题Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock

sudo chmod a+rw /var/run/docker.sock

启动Docker

以下命令运行一个ubuntu容器,以交互方式附加到本地命令行会话,然后运行/bin/bash

docker run -i -t ubuntu /bin/bash

运行此命令时,会发生以下情况(假设使用的是默认注册表配置):

  1. 如果在本地没有ubuntu镜像,则Docker会将其从已配置的注册表中拉出,就像手动运行docker pull ubuntu一样。
  2. Docker会创建一个新容器,就像运行命令docker container create一样。
  3. Docker将一个读写文件系统分配给容器,作为其最后一层。这允许运行中的容器在其本地文件系统中创建或修改文件和目录。
  4. Docker创建了一个网络接口,将容器连接到默认网络,因为您没有指定任何网络选项。这包括为容器分配IP地址。默认情况下,容器可以使用主机的网络连接连接到外部网络。
  5. Docker启动容器并执行/bin/bash。由于容器是交互式运行的,并且已附加到您的终端(由于-i-t标志),因此您可以在输出记录到终端时使用键盘提供输入。
  6. 当您输入exit终止/bin/bash时,容器将停止但不会被删除。您可以重新启动或删除它。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ayiya_Oese

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值