环境准备:轻量级服务器(centos 7.8)
涉及到的软件:XShell(连接服务器以及进行操作)
XShell
xshell的使用这里不做说明,十分简单,连接上服务器登录即可
ps: 这里使用的是腾讯云,连接服务器可以通过腾讯云给出的步骤进行连接(通过SSH登录linux实例)
docker 安装
docker的官方文档十分详细,只不过是全英文的,所以看起来可能有些麻烦
这里跟着步骤文档的命令操作即可
文档中的
sudo关键字是用来提权的,即一个操作系统中分为管理员和普通用户,普通用户某些操作需要提高权限,这时候需要用到sudo关键字。因为这里登录的是root用户,即管理员,所以不需要用到
sudo关键字
-
如果服务器中已经存在docker,可以先进行卸载,如果不存在可以跳过这一步骤
移除docker以及docker的相关运行环境
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
- 设置存储库
sudo yum install -y yum-utils # 下载需要的安装包
$ sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
- 安装docker
yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin
输入 y 确认下载
等待下载完成之后出现complete!表示完成
3.启动docker
systemctl start docker
启动成功之后可以通过service docker status 查看docker版本
4. 简单使用
docker run hello-world
运行之后打印出这一段
[root@VM-12-9-centos ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally # 表示本地没有找到该镜像
latest: Pulling from library/hello-world # 没有找到镜像,所以开始下载该镜像
2db29710123e: Already exists # 打印出镜像的一些信息
Digest: sha256:80f31da1ac7b312ba29d65080fddf797dd76acfb870e677f390d5acba9741b17 # 镜像防伪码
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/
服务器镜像加速
下载镜像是需要去国外下载,所以有时候会因为网速问题下载很慢,所以服务器服务商会根据这个提供不同的对策,阿里云可以设置镜像加速地址。
腾讯云镜像加速方法: 按照教程完成之后运行提供的命令即可
腾讯云pull镜像的时候需要本地先下载一个镜像。
使用命令查看本地镜像
docker images
第一个镜像就是已经pull到腾讯云上,删除之后可以直接从腾讯云上下载
镜像、容器说明
镜像和容器的区别就像是类和实例的区别,镜像是类,可以通过镜像创建多个容器实例。容器之间互相隔离,不会影响。
基本命令:
systemctl start docker # 开启docker服务
systemctl stop docker # 关闭docker服务
systemctl stop docker.socket # 关闭docker的socket服务(docker关闭时输入docker相关指令时会自动激活docker)
service docker status # docker现在的运行状态
docker images # 查看当前服务器下所有的镜像
docker run <镜像名称|镜像id> # 创建容器并运行