Docker 使用简介

 

5.1 Docker 版本更换
如果需要更换 Docker 版本,可按以下方法进行操作。
首先,使用“apt-cache madison docker-ce”命令列出系统当前可用的 Docker 版本,执
行示例如下:
root@localhost:~# apt-cache madison docker-ce
docker-ce | 18.03.1~ce-0~ubuntu | https://download.docker.com/linux/ubuntu xenial/stable armhf Packages
docker-ce | 18.03.0~ce-0~ubuntu | https://download.docker.com/linux/ubuntu xenial/stable armhf Packages
docker-ce | 17.12.1~ce-0~ubuntu | https://download.docker.com/linux/ubuntu xenial/stable armhf Packages
docker-ce | 17.12.0~ce-0~ubuntu | https://download.docker.com/linux/ubuntu xenial/stable armhf Packages
docker-ce | 17.09.1~ce-0~ubuntu | https://download.docker.com/linux/ubuntu xenial/stable armhf Packages
docker-ce | 17.09.0~ce-0~ubuntu | https://download.docker.com/linux/ubuntu xenial/stable armhf Packages
docker-ce | 17.06.2~ce-0~ubuntu | https://download.docker.com/linux/ubuntu xenial/stable armhf Packages
docker-ce | 17.06.1~ce-0~ubuntu | https://download.docker.com/linux/ubuntu xenial/stable armhf Packages
docker-ce | 17.06.0~ce-0~ubuntu | https://download.docker.com/linux/ubuntu xenial/stable armhf Packages
docker-ce | 17.03.2~ce-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu xenial/stable armhf Packages
docker-ce | 17.03.1~ce-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu xenial/stable armhf Packages
docker-ce | 17.03.0~ce-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu xenial/stable armhf Packages
然后,使用“apt-get install docker-ce=<VERSION>”命令指定安装相应的版本,以
18.03.1~ce-0~ubuntu 为例,安装命令为:
root@localhost:~# apt-get install docker-ce=18.03.1~ce-0~ubuntu
5.2 Docker 基本操作
5.2.1 Docker 守护进程
已安装 Docker 软件的系统,启动时默认会启动 Docker 守护进程,守护进程会监听
/var/run/docker.sock 这个 Unix 套接字文件,来获取来自客户端的 Docker 请求。如果 Docker
守护进程没有启动,运行 Docker 客户端时,会报如下提示信息:
docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.
See 'docker run --help'.
关于 Docker 守护进程开机自启动的查询、设置,以及手动启动/关闭的命令如下所示。
(1) 查询 Docker 守护进程是否开机自启动
root@localhost:~# systemctl is-enabled docker.service
2) 使能 Docker 守护进程开机自启动
root@localhost:~# systemctl enable docker.service
3) 禁止 Docker 守护进程开机自启动
root@localhost:~# systemctl disable docker.service
4) 手动启动 Docker 守护进程
root@localhost:~# systemctl start docker.service
5) 手动关闭 Docker 守护进程
root@localhost:~# systemctl stop docker.service
6) 手动重启 Docker 守护进程
root@localhost:~# systemctl restart docker.service
使用 docker version 命令可查看 Docker 版本,如下所示,正常情况下,会输出系统当前
Docker 客户端和守护进程的版本信息。
root@localhost:~# docker version
Client:
Version: 17.03.1-ce
API version: 1.27
Go version: go1.7.5
Git commit: c6d412e
Built: Mon Mar 27 17:18:59 2017
OS/Arch: linux/arm
Server:
Version: 17.03.1-ce
API version: 1.27 (minimum version 1.12)
Go version: go1.7.5
Git commit: c6d412e
Built: Mon Mar 27 17:18:59 2017
OS/Arch: linux/arm
Experimental: false
5.2.2 运行 hello-world 容器
hello-world 镜像是 Docker Hub Registry 上一个简单的测试镜像,可用于快速检测 Docker
是否安装成功,本节以运行 hello-world 镜像为例,演示一下运行一个 Docker 镜像的过程,
其执行命令和结果如下所示:
root@localhost:~# docker run hello-world
Unable to find image 'hello-world:latest' locally
<- 本地没有 hello-world:latest 镜像
latest: Pulling from library/hello-world
<- 从 Docker Hub Registry 上 pull hello-world 镜像
61ddb93a5f93: Pull complete
Digest: sha256:f5233545e43561214ca4891fd1157e1c3c563316ed8e237750d59bde73361e77
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
<- hello-world 容器运行时所输出的信息
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.
(arm32v5)
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 y
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/engine/userguide/
如上所示,其中“docker run”命令用于创建和启动一个容器,“hello-world”是要使用
的镜像,执行结果中的第一行:“Unable to find image 'hello-world:latest' locally” 说明本地
没有 hello-world:latest 镜像,于是 Docker 会在 Docker Hub Registry 上进行在线搜索,并下载
最新版本的镜像 hello-world:latest。镜像下载后,Docker 会将它转成容器并运行,此容器运
行的结果是打印“Hello from Docker!”等信息,打印结束后退出容器。
如果以后再运行 hello-world 镜像,Docker 检测本地已经存在 hello-world 镜像时,会直
接尝试将其转成容器并运行,执行结果如下所示。
root@localhost:~# docker run hello-world
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.
(arm32v5)
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/engine/userguide/
5.2.3 运行 ubuntu 容器
为了进一步了解 Docker,可以启动一个 ubuntu 容器,并在容器中尝试 ubuntu 相关操作。
在 Docker Hub Registry 上,Docker 公司提供了一个 ubuntu 基础镜像(类似的还有 fedora、
debian、centOS 等镜像),我们可以选择在此基础镜像上,增加自己创建的模块单元,来构
建自己所需的镜像。
1. 下载并启动 ubuntu 容器
如下所示,使用 docker run 创建和启动一个 ubuntu 容器。其中-h 参数用来设定容器的
主机名(hostname),以“M6708-T”为例,当容器启动完成后,其命令行提示符中包含 M6708-T。
-i 参数使得容器中的 STDIN 是开启的,-t 参数使得容器分配一个伪 tty 终端,这样新创建的
容器才能提供一个交互式 shell。如果想创建一个运行后台服务的容器,则不需要-i 和-t 参数。
执行此命令后,Docker 检测到本地没有 ubuntu:lates 镜像时,会在 Docker Hub Registry
上搜索并下载镜像,然后将它转成容器并运行。该容器运行后会拥有自己的网络、IP 地址,
以及一个用来和宿主机进行通信的桥接网络接口,命令中的/bin/bash 是告诉 Docker 在此容
器中要运行的命令,本例以“/bin/bash”为例,在容器中启动了一个 Bash shell,如下所示。
root@localhost:~# docker run -h M6708-T -i -t ubuntu /bin/bash
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/Ubuntu
<- 从 Docker Hub Registry 上 pull ubuntu 镜像
253c7c36e8ff: Pull complete
fe7c768a65c7: Pull complete
ba8d0d2ce80e: Pull complete
28492cd57e46: Pull complete
bdfd257d89dc: Pull complete
Digest: sha256:5f4bdc3467537cbbe563e80db2c3ec95d548a9145d64453b06939c4592d67b6d
Status: Downloaded newer image for ubuntu:latest
root@M6708-T:/#
<- 容器运行后,启动 Bash shell
接下来,可以尝试在此容器中执行 ubuntu 的 Bash shell 相关操作,比如:
root@M6708-T:/# hostname
M6708-T
root@M6708-T:/# ls /
bin boot dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var
root@M6708-T:/# ps -aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.2 2960 2220 ? Ss 02:06 0:00 /bin/bash
root 16 0.0 0.1 4488 1820 ? R+ 02:51 0:00 ps -aux
2. 在容器中安装软件包
由于此容器是由 ubuntu 基础镜像启动的,很多工具是缺少的,需要根据自己需求进行
安装,比如,尝试使用 ifconfig 查看系统网络时,提示无法找到此命令:
root@M6708-T:/# ifconfig
bash: ifconfig: command not found
很多工具可以使用 apt 命令来安装,由于容器需要通过桥接宿主机网卡来联网下载软件
包,所以需确保容器宿主机是联网状态。先使用 apt-get update 更新软件包列表:
root@M6708-T:/# apt-get update
Get:1 http://ports.ubuntu.com/ubuntu-ports bionic InRelease [242 kB]
„„
<- 省略若干行
Get:17 http://ports.ubuntu.com/ubuntu-ports bionic-security/main armhf Packages [76.9 kB]
Fetched 24.8 MB in 4min 36s (89.9 kB/s)
Reading package lists... Done
5.2.4 查询容器
使用 docker ps -a 命令可列出当前系统中的所有容器,包括运行的和停止的:
root@localhost:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES
7fc2b0e52f2c
ubuntu "/bin/bash" 3 hours ago Exited (0) 2 minutes ago
vibrant_mcclintock
4c5e8600a6ae hello-world "/hello" 3 hours ago Exited (0) 3 hours ago
elastic_hypatia
由以上输出信息,可以看到当前系统中有两个已经停止(STATUS 为 Exited)的容器:
ubuntu 和 hello-world。其中 NAMES 字段是 Docker 为每个容器所命的名称,用于标识各个
容器,比如 vibrant_mcclintock 用于标识 ubuntu 容器,在 docker 一些指令中可用 NAMES 中
的名称作为参数来指定所标识的容器,比如查询 ubuntu 容器操作日志:
root@localhost:~# docker logs vibrant_mcclintock
root@M6708-T:/# hostname
M6708-T
root@M6708-T:/# ls
bin boot dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var
„„
<- 省略若干行
此外,CONTAINER ID 字段也可以用于标识各个容器,用法与 NAMES 相同。
5.2.5 重新启动容器
对于已经停止的容器,可使用 docker restart 命令来重新启动,比如重启 ubuntu 容器:
root@localhost:~# docker restart vibrant_mcclintock
vibrant_mcclintock
5.2.6 删除容器
如果容器已经不再使用,可使用 docker rm 命令来删除,比如删除 hello-world 容器:
root@localhost:~# docker rm elastic_hypatia
elastic_hypatia
 
 

本公司成本价甩卖工控主板,欢迎大家选购:
PCIE总线转八串口卡,PCIE总线转IO卡,瑞芯微3568板卡,寒武纪CE3226摄像头板卡,龙芯3A4000工控板卡,龙芯3A5000工控板卡,海光3250工控板卡,飞腾FT-2000/4板卡

联系方式:
电话、微信:15918785568 罗生
Email:13279654@qq.com
公众号:开发之美

 
posted @ 2022-05-06 13:40  DMCF  阅读(94)  评论(0编辑  收藏  举报