哈喽大家好,欢迎来到虚拟化时代君(XNHCYL),收不到通知请将我点击星标!
“ 大家好,我是虚拟化时代君,一位潜心于互联网的技术宅男。这里每天为你分享各种你感兴趣的技术、教程、软件、资源、福利…(每天更新不间断,福利不见不散)
一、Docker简介
Docker是一个开源的应用容器引擎,它允许开发者打包他们的应用以及应用的依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器或Windows服务器上。Docker的容器技术有助于实现应用的快速开发、部署和扩展。以下是Docker的一些主要优点:
-
1. 高效:容器将应用及其依赖环境打包在一起,避免了在部署时因环境不一致导致的问题。
-
轻量级:与传统虚拟机相比,Docker容器占用的系统资源更少,启动速度更快。
-
可移植性:Docker容器可以在任何支持Docker的操作系统上运行。
-
易于管理:Docker容器可以通过命令行或图形界面进行管理。
二、安装Docker
1. 更新系统软件包
在安装Docker之前,首先确保系统软件包是最新的。打开终端,执行以下命令:
sudo apt-get update
2. 安装依赖软件包
安装Docker之前,需要安装一些必要的软件包。执行以下命令:
sudo apt-get install ca-certificates curl
3. 创建目录并设置权限
sudo mkdir -p /etc/apt/keyringssudo install -m 0755 -d /etc/apt/keyrings
4. 添加DockerGPG密钥
下载 Docker 的 GPG 公钥,并将其保存到 /etc/apt/keyrings/docker.asc 文件中并设置文件的权限设置为所有用户可读:
sudo curl -fsSL https://mirrors.cloud.tencent.com/docker-ce/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.ascsudo chmod a+r /etc/apt/keyrings/docker.asc
4. 添加Docker仓库
接下来,添加Docker仓库到系统:
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://mirrors.cloud.tencent.com/docker-ce/linux/ubuntu/ \$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
5. 安装Docker
更新软件包列表并安装Docker:
sudo apt-get updatesudo apt-get install -y \docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
6. 验证安装
安装完成后,可以通过以下命令验证Docker是否已成功安装:
sudo docker --version
三、基本使用
1. 启动Docker服务
安装完成后,启动Docker服务:
sudo systemctl start docker
设置Docker服务开机自启:
sudo systemctl enable docker
2. 运行第一个容器
下面我们以运行一个Hello World容器为例,来体验Docker的基本使用:
sudo docker run hello-world
运行上述命令后,Docker将会从官方镜像仓库下载一个测试镜像并运行一个新的容器。容器运行后,会在终端输出一段欢迎信息。
3. 查看运行中的容器
使用以下命令查看当前运行中的容器:
sudo docker ps
如果要查看所有容器(包括已停止的),可以使用:
sudo docker ps -a
4. 停止容器
如果要停止运行中的容器,可以使用以下命令:
sudo docker stop [容器ID或名称]
5. 删除容器
删除已停止的容器:
sudo docker rm [容器ID或名称]
四、换源
1. 更换国内源
在拉取Docker镜像时可能会出现超时无法拉取等现象,这时需要配置镜像加速源,这里以腾讯云Docker源举例:
ubuntu@VM-4-16-ubuntu:~$ sudo docker run hello-worldUnable to find image 'hello-world:latest' locallydocker: Error response from daemon: Get "https://registry-1.docker.io/v2/": net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers).See 'docker run --help'.
2. 创建并编辑 /etc/docker/daemon.json 配置文件。
sudo mkdir -p /etc/dockersudo tee /etc/docker/daemon.json <<-'EOF'{"registry-mirrors": ["https://mirror.ccs.tencentyun.com"]}EOFsudo systemctl daemon-reloadsudo systemctl restart docker
3. 拉取镜像测试
ubuntu@VM-4-16-ubuntu:~$ sudo docker run hello-worldUnable to find image 'hello-world:latest' locallylatest: Pulling from library/hello-worldc1ec31eb5944: Pull completeDigest: sha256:d211f485f2dd1dee407a80973c8f129f00d54604d2c90732e8e320e5038a0348Status: Downloaded newer image for hello-world:latestHello 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 theexecutable that produces the output you are currently reading.4. The Docker daemon streamed that output to the Docker client, which sent itto your terminal.To try something more ambitious, you can run an Ubuntu container with:$ docker run -it ubuntu bashShare 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/
五、卸载Docker
1. 卸载 Docker 相关软件包:
sudo apt-get purge docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras
2. 主机上的镜像、容器、卷或自定义配置文件默认不会自动删除。
如果需要删除执行以下命令:
sudo rm -rf /var/lib/dockersudo rm -rf /var/lib/containerd
资源申明:小编更新资源文章只是为了给大家提供一个绿色学习的平台,如果你在本站看到的任何图片文字有涉及到你的利益以及版权都可以联系小编删除。
1117

被折叠的 条评论
为什么被折叠?



