docker安装与使用

docker安装

参考网址:https://www.runoob.com/docker/ubuntu-docker-install.html

卸载旧版本

Docker 的旧版本被称为 docker,docker.io 或 docker-engine 。如果已安装,请卸载它们:

$ sudo apt-get remove docker docker-engine docker.io containerd runc

当前称为 Docker Engine-Community 软件包 docker-ce 。

使用 Docker 仓库进行安装

在新主机上首次安装 Docker Engine-Community 之前,需要设置 Docker 仓库。之后,您可以从仓库安装和更新 Docker 。

设置仓库

更新 apt 包索引。

$ sudo apt-get update

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

$ sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common

添加 Docker 的官方 GPG 密钥:

$ curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88 通过搜索指纹的后8个字符,验证您现在是否拥有带有指纹的密钥。

$ sudo apt-key fingerprint 0EBFCD88

使用以下指令设置稳定版仓库
x64版本

$ sudo add-apt-repository \
   "deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/ \
  $(lsb_release -cs) \
  stable"

如果是arm64版本,如下命令:

$ sudo add-apt-repository \
 "deb [arch=arm64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/ \
$(lsb_release -cs) \
stable"

安装 Docker Engine-Community
更新 apt 包索引。

$ sudo apt-get update

安装最新版本的 Docker Engine-Community 和 containerd ,或者转到下一步安装特定版本:

$ sudo apt-get install docker-ce docker-ce-cli containerd.io

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

$ apt-cache madison docker-ce

  docker-ce | 5:18.09.1~3-0~ubuntu-xenial | https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu  xenial/stable amd64 Packages
  docker-ce | 5:18.09.0~3-0~ubuntu-xenial | https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu  xenial/stable amd64 Packages
  docker-ce | 18.06.1~ce~3-0~ubuntu       | https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu  xenial/stable amd64 Packages
  docker-ce | 18.06.0~ce~3-0~ubuntu       | https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu  xenial/stable amd64 Packages
  ...
使用第二列中的版本字符串安装特定版本,例如 5:18.09.1~3-0~ubuntu-xenial。

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

测试

测试 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:c3b4ada4687bbaa170745b3e4dd8ac3f194ca95b2d0518b417fb47e5879d9b5f
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/

容器使用

1. 查看docker版本

sudo docker version

z@ubuntu:~/dockertest$ sudo docker version
Client: Docker Engine - Community
 Version:           20.10.3
 API version:       1.41
 Go version:        go1.13.15
 Git commit:        48d30b5
 Built:             Fri Jan 29 14:33:13 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.3
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       46229ca
  Built:            Fri Jan 29 14:31:25 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.3
  GitCommit:        269548fa27e0089a8b8278fc4fc781d7f65a939b
 runc:
  Version:          1.0.0-rc92
  GitCommit:        ff819c7e9184c13b7c2607fe6c30ae19403a7aff
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
z@ubuntu:~/dockertest$ 

2. 拉取Ubuntu:18.04容器

docker pull ubuntu:18.04

3. 前台运行容器

docker run -it ubuntu:18.04

z@ubuntu:~/dockertest$ sudo docker run -it ubuntu:18.04
Unable to find image 'ubuntu:18.04' locally
18.04: Pulling from library/ubuntu
d519e2592276: Pull complete 
d22d2dfcfa9c: Pull complete 
b3afe92c540b: Pull complete 
Digest: sha256:ea188fdc5be9b25ca048f1e882b33f1bc763fb976a8a4fea446b38ed0efcbeba
Status: Downloaded newer image for ubuntu:18.04

4. 容器中操作

apt-get update
apt-get install inetutils-ping
exit

root@ffbe2f9b9625:/# apt-get update
Get:1 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]           
Get:2 http://security.ubuntu.com/ubuntu bionic-security/multiverse amd64 Packages [43.5 kB]
Get:3 http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages [1964 kB]
Get:4 http://archive.ubuntu.com/ubuntu bionic InRelease [242 kB]     
Get:5 http://security.ubuntu.com/ubuntu bionic-security/restricted amd64 Packages [304 kB]
Get:6 http://security.ubuntu.com/ubuntu bionic-security/universe amd64 Packages [1392 kB]
Get:7 http://archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]             
Get:8 http://archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]           
Get:9 http://archive.ubuntu.com/ubuntu bionic/multiverse amd64 Packages [186 kB]      
Get:10 http://archive.ubuntu.com/ubuntu bionic/main amd64 Packages [1344 kB]          
Get:11 http://archive.ubuntu.com/ubuntu bionic/restricted amd64 Packages [13.5 kB]    
Get:12 http://archive.ubuntu.com/ubuntu bionic/universe amd64 Packages [11.3 MB]      
Get:13 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages [2394 kB]  
Get:14 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 Packages [2163 kB]
Get:15 http://archive.ubuntu.com/ubuntu bionic-updates/multiverse amd64 Packages [50.5 kB]
Get:16 http://archive.ubuntu.com/ubuntu bionic-updates/restricted amd64 Packages [334 kB]
Get:17 http://archive.ubuntu.com/ubuntu bionic-backports/main amd64 Packages [11.3 kB]
Get:18 http://archive.ubuntu.com/ubuntu bionic-backports/universe amd64 Packages [11.4 kB]
Fetched 22.0 MB in 1min 21s (274 kB/s)                                                
Reading package lists... Done
root@ffbe2f9b9625:/# apt-get install inetutils-ping
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  netbase
The following NEW packages will be installed:
  inetutils-ping netbase
0 upgraded, 2 newly installed, 0 to remove and 3 not upgraded.
Need to get 72.8 kB of archives.
After this operation, 389 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://archive.ubuntu.com/ubuntu bionic/main amd64 netbase all 5.4 [12.7 kB]
Get:2 http://archive.ubuntu.com/ubuntu bionic/universe amd64 inetutils-ping amd64 2:1.9.4-3 [60.0 kB]
Fetched 72.8 kB in 3s (21.3 kB/s)         
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package netbase.
(Reading database ... 4045 files and directories currently installed.)
Preparing to unpack .../archives/netbase_5.4_all.deb ...
Unpacking netbase (5.4) ...
Selecting previously unselected package inetutils-ping.
Preparing to unpack .../inetutils-ping_2%3a1.9.4-3_amd64.deb ...
Unpacking inetutils-ping (2:1.9.4-3) ...
Setting up netbase (5.4) ...
Setting up inetutils-ping (2:1.9.4-3) ...
root@ffbe2f9b9625:/# 
root@ffbe2f9b9625:/# exit
exit
z@ubuntu:~/dockertest$ 

查看镜像列表

z@ubuntu:~/dockertest$ sudo docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
ubuntu        18.04     c090eaba6b94   5 weeks ago     63.3MB
hello-world   latest    bf756fb1ae65   13 months ago   13.3kB
z@ubuntu:~/dockertest$ 

5. 将容器生成镜像

查看运行的容器

sudo docker ps -a

在这里插入图片描述

docker commit 容器ID 镜像名字

z@ubuntu:~/dockertest$ sudo docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED          STATUS                      PORTS     NAMES
ffbe2f9b9625   ubuntu:18.04   "/bin/bash"   9 minutes ago    Exited (0) 5 minutes ago              affectionate_goldwasser
40a31d6efa57   hello-world    "/hello"      13 minutes ago   Exited (0) 13 minutes ago             festive_stonebraker
z@ubuntu:~/dockertest$ sudo docker commit ffbe2f9b9625 test_img
sha256:19fd982662ac8708a23c623fb4dac24545af46e033eca24e5810e0af4fe9f5a1
z@ubuntu:~/dockertest$ sudo docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED          STATUS                      PORTS     NAMES
ffbe2f9b9625   ubuntu:18.04   "/bin/bash"   11 minutes ago   Exited (0) 7 minutes ago              affectionate_goldwasser
40a31d6efa57   hello-world    "/hello"      15 minutes ago   Exited (0) 15 minutes ago             festive_stonebraker
z@ubuntu:~/dockertest$ sudo docker images
REPOSITORY    TAG       IMAGE ID       CREATED          SIZE
test_img      latest    19fd982662ac   22 seconds ago   99.7MB
ubuntu        18.04     c090eaba6b94   5 weeks ago      63.3MB
hello-world   latest    bf756fb1ae65   13 months ago    13.3kB
z@ubuntu:~/dockertest$ 

6. 编写容器需要的启动的程序引导

vim Dockerfile
FROM test_img
MAINTAINER test
COPY sample /root/
CMD ["/root/sample"]

其中 sample 是当前目录下的一个用于测试的程序。

7. 在列表中创建一个容器镜像

docker build -t new_img .

z@ubuntu:~/dockertest$ sudo docker build -t new_img .
Sending build context to Docker daemon  11.78kB
Step 1/4 : FROM test_img
 ---> 19fd982662ac
Step 2/4 : MAINTAINER test
 ---> Running in 555ef4895ef0
Removing intermediate container 555ef4895ef0
 ---> 501d26f55e3c
Step 3/4 : COPY sample /root/
 ---> 560e507ccc8b
Step 4/4 : CMD ["/root/sample"]
 ---> Running in d684abf45f5e
Removing intermediate container d684abf45f5e
 ---> 9797e231b8de
Successfully built 9797e231b8de
Successfully tagged new_img:latest
z@ubuntu:~/dockertest$ 

8. 查看容器镜像列表

docker images

z@ubuntu:~/dockertest$ sudo docker images
REPOSITORY    TAG       IMAGE ID       CREATED          SIZE
new_img       latest    9797e231b8de   48 seconds ago   99.7MB
test_img      latest    19fd982662ac   14 minutes ago   99.7MB
ubuntu        18.04     c090eaba6b94   5 weeks ago      63.3MB
hello-world   latest    bf756fb1ae65   13 months ago    13.3kB
z@ubuntu:~/dockertest$ 

9. 从docker镜像表中导出容器包

docker save -o 导出容器包的名字 镜像名字:镜像标签

z@ubuntu:~/dockertest$ sudo docker save -o sample.tar new_img:latest
z@ubuntu:~/dockertest$ ls
Dockerfile  sample  sample.tar
z@ubuntu:~/dockertest$ 

10. 将容器镜像导入到docker镜像表中

docker load -input 容器包的名字

z@ubuntu:~/dockertest$ sudo docker images
REPOSITORY    TAG       IMAGE ID       CREATED          SIZE
new_img       latest    9797e231b8de   2 minutes ago    99.7MB
test_img      latest    19fd982662ac   16 minutes ago   99.7MB
ubuntu        18.04     c090eaba6b94   5 weeks ago      63.3MB
hello-world   latest    bf756fb1ae65   13 months ago    13.3kB
z@ubuntu:~/dockertest$ sudo docker load --input sample.tar
Loaded image: new_img:latest
z@ubuntu:~/dockertest$ 

11. 后台运行容器

docker run –itd –v 主机目录:容器内目录 –p 主机端口:镜像内端口 镜像名字

z@ubuntu:~/dockertest$ sudo docker run -itd -v /dev:/dev -p 1024:1024 new_img
8d64430b5096365a65774ed6107234b5538429b67ebdcb5ec9bda46d6c41b5ed
z@ubuntu:~/dockertest$ 

12. 进入容器

docker exec –it 容器名字 /bin/bash

z@ubuntu:~/dockertest$ sudo docker ps -a
CONTAINER ID   IMAGE          COMMAND          CREATED          STATUS                      PORTS                    NAMES
8d64430b5096   new_img        "/root/sample"   31 seconds ago   Up 29 seconds               0.0.0.0:1024->1024/tcp   cranky_gagarin
ffbe2f9b9625   ubuntu:18.04   "/bin/bash"      30 minutes ago   Exited (0) 25 minutes ago                            affectionate_goldwasser
40a31d6efa57   hello-world    "/hello"         33 minutes ago   Exited (0) 33 minutes ago                            festive_stonebraker
z@ubuntu:~/dockertest$ sudo docker exec -it cranky_gagarin /bin/bash
root@8d64430b5096:/# ps
   PID TTY          TIME CMD
     7 pts/2    00:00:00 bash
    16 pts/2    00:00:00 ps
root@8d64430b5096:/# ps -aux
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          1 99.0  0.0   4380   732 pts/1    Rs+  09:45   1:35 /root/sample
root          7  0.2  0.1  18508  3328 pts/2    Ss   09:46   0:00 /bin/bash
root         17  0.0  0.0  34408  2836 pts/2    R+   09:46   0:00 ps -aux

13. 退出

exit

root@8d64430b5096:/# exit
exit
z@ubuntu:~/dockertest$ 

14. 查看运行中的容器

docker ps -a

z@ubuntu:~/dockertest$ sudo docker ps -a
CONTAINER ID   IMAGE          COMMAND          CREATED          STATUS                      PORTS                    NAMES
8d64430b5096   new_img        "/root/sample"   2 minutes ago    Up 2 minutes                0.0.0.0:1024->1024/tcp   cranky_gagarin
ffbe2f9b9625   ubuntu:18.04   "/bin/bash"      31 minutes ago   Exited (0) 27 minutes ago                            affectionate_goldwasser
40a31d6efa57   hello-world    "/hello"         35 minutes ago   Exited (0) 35 minutes ago                            festive_stonebraker

15. 停止正在运行的容器

docker stop 容器ID

z@ubuntu:~/dockertest$ sudo docker stop 8d64430b5096
8d64430b5096
z@ubuntu:~/dockertest$ sudo docker ps -a
CONTAINER ID   IMAGE          COMMAND          CREATED          STATUS                       PORTS     NAMES
8d64430b5096   new_img        "/root/sample"   3 minutes ago    Exited (137) 2 seconds ago             cranky_gagarin
ffbe2f9b9625   ubuntu:18.04   "/bin/bash"      32 minutes ago   Exited (0) 28 minutes ago              affectionate_goldwasser
40a31d6efa57   hello-world    "/hello"         36 minutes ago   Exited (0) 36 minutes ago              festive_stonebraker
z@ubuntu:~/dockertest$ 

16. 删除加载的容器

docker rm 容器

z@ubuntu:~/dockertest$ sudo docker rm cranky_gagarin
cranky_gagarin
z@ubuntu:~/dockertest$ sudo docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED          STATUS                      PORTS     NAMES
ffbe2f9b9625   ubuntu:18.04   "/bin/bash"   33 minutes ago   Exited (0) 29 minutes ago             affectionate_goldwasser
40a31d6efa57   hello-world    "/hello"      37 minutes ago   Exited (0) 37 minutes ago             festive_stonebraker
z@ubuntu:~/dockertest$ 

17. 查看已加载的容器镜像

docker images

z@ubuntu:~/dockertest$ sudo docker images
REPOSITORY    TAG       IMAGE ID       CREATED          SIZE
new_img       latest    9797e231b8de   8 minutes ago    99.7MB
test_img      latest    19fd982662ac   22 minutes ago   99.7MB
ubuntu        18.04     c090eaba6b94   5 weeks ago      63.3MB
hello-world   latest    bf756fb1ae65   13 months ago    13.3kB

18. 删除加载的容器镜像

docker rmi 加载的镜像

z@ubuntu:~/dockertest$ sudo docker rmi new_img
Untagged: new_img:latest
Deleted: sha256:9797e231b8def04a46967841ecd2cc460bbe3f055c10ab9696944632f3fab998
Deleted: sha256:560e507ccc8be1201443018ebe30df3e34946bdb622c6c6b0b5273274be9726a
Deleted: sha256:ccc74ea77197dcdc1c49a96e2dc5fd44aef45a91b8c2b0dccca7fd5c8b3d9f10
Deleted: sha256:501d26f55e3cce43cb7ef53173320d689dd29af846aac5eaf6946113c5d2b250
z@ubuntu:~/dockertest$ sudo docker images
REPOSITORY    TAG       IMAGE ID       CREATED          SIZE
test_img      latest    19fd982662ac   23 minutes ago   99.7MB
ubuntu        18.04     c090eaba6b94   5 weeks ago      63.3MB
hello-world   latest    bf756fb1ae65   13 months ago    13.3kB
z@ubuntu:~/dockertest$ 

19.重新加载容器镜像

sudo docker load --input sample.tar

z@ubuntu:~/dockertest$  sudo docker load --input sample.tar
c4f8450d4a42: Loading layer [==================================================>]  10.24kB/10.24kB
Loaded image: new_img:latest
z@ubuntu:~/dockertest$ sudo docker images
REPOSITORY    TAG       IMAGE ID       CREATED          SIZE
new_img       latest    9797e231b8de   10 minutes ago   99.7MB
test_img      latest    19fd982662ac   24 minutes ago   99.7MB
ubuntu        18.04     c090eaba6b94   5 weeks ago      63.3MB
hello-world   latest    bf756fb1ae65   13 months ago    13.3kB
z@ubuntu:~/dockertest$ sudo docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED          STATUS                      PORTS     NAMES
ffbe2f9b9625   ubuntu:18.04   "/bin/bash"   35 minutes ago   Exited (0) 31 minutes ago             affectionate_goldwasser
40a31d6efa57   hello-world    "/hello"      39 minutes ago   Exited (0) 39 minutes ago             festive_stonebraker
z@ubuntu:~/dockertest$ 

这里只是加载镜像,并未启动容器。如果启动容器,需执行 docker run 命令.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值