简介
Docker是一种轻量级的虚拟化技术,它可以让开发者将应用打包到一个可移植的容器中,然后安装到任何运行Linux或Windows等系统的服务器上。相较于传统虚拟机,Docker容器提供轻量化的虚拟化方式、安装便捷、启停速度快。
Docker 从 17.03 版本之后分为 CE(Community Edition: 社区版) 和 EE(Enterprise Edition: 企业版),我们用社区版就可以了。
下载并启动
在 ubuntu 上安装 Docker 的具体操作如下:
- 更新 apt 软件包缓存
$ sudo apt-get update
- 安装 docker 的 apt 源:
$ sudo apt install apt-transport-https ca-certificates curl software-properties-common
- 添加 docker 的官方 gpg 密钥:
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
- 验证 gpg 密钥是否添加成功:
$ sudo apt-key fingerprint 0EBFCD88
-
添加 docker 的稳定存储库:
$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
- 再次更新软件包列表
$ sudo apt update
-
安装最新版本的 docker CE:
$ sudo apt install docker-ce docker-ce-cli containerd.io
-
启动 docker 守护程序:
$ sudo systemctl start docker
- 为了让 docker 在系统启动时自动运行,请执行以下命令:
$ sudo systemctl enable docker
- 通过以下命令验证 docker 是否正确安装并运行:
$ docker version
下载并运行镜像
以下以运行 busybox 镜像举例:
- 使用 docker pull 命令下载 busybox:latest 镜像:
$ docker pull busybox:latest
- 下载后查看 busybox 镜像:
$ docker image ls
- 运行 busybox 镜像
$ docker run -it busybox
Docker 常用命令
镜像管理
docker pull busybox //拉取 busybox 最新版本镜像
docker pull busybox:1.20.2 //拉取 busybox 指定版本镜像
docker images //查看本地镜像
docker image rm busybox:1.20.2 //删除镜像
容器管理
docker exec -it xxxx /bin/bash //容器中执行 bsah 并进入交互模式 shell
docker ps -a //列出容器
docker start CONTAINER ID //启动容器
docker stop CONTAINER ID //停用容器
docker rm CONTAINER ID //删除容器
docker run -d -p 8088:80 --name CONTAINER ID docker/getting-started //通过 localhost:8080 访问该容器提供的内容
各项参数及说明
run | 创建并启动容器 |
start | 启动指定容器 |
stop | 停用指定容器 |
exec | 在已经运行的容器中执行新命令 |
-it | 连接到容器并在容器中创建&运行交互式终端 |
-p | 将本机端口映射到容器内的端口 |
-d | 后台运行容器 |
--name | 指定容器名 |