前言
在上一篇文章中,我们进行了Linux操作系统的搭建安装,方便我们进行Docker技术的研究使用。那在本篇文章中,我们来开始安装搭建一个Docker,简单体验下Docker的魅力。
ubuntu环境
- 旧版本移除
sudo apt-get remove docker docker-engine docker.io containerd runc
- 安装
# 方式一
sudo apt install -y docker.io
# 方式二
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin
- 启动、停止
sudo service docker start
sudo service docker stop
sudo service docker restart
- 验证使用
sudo docker run hello-world
### 返回下面信息 说明安装成功
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:c77be1d3a47d0caf71a82dd893ee61ce01f32fc758031a6ec4cf1389248bb833
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/
- 查看版本信息
sudo docker version
# 以下返回版本信息
Client:
Version: 20.10.12
API version: 1.41
Go version: go1.17.3
Git commit: 20.10.12-0ubuntu4
Built: Mon Mar 7 17:10:06 2022
OS/Arch: linux/amd64
Context: default
Experimental: true
Server:
Engine:
Version: 20.10.12
API version: 1.41 (minimum version 1.12)
Go version: go1.17.3
Git commit: 20.10.12-0ubuntu4
Built: Mon Mar 7 15:57:50 2022
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.5.9-0ubuntu3.1
GitCommit:
runc:
Version: 1.1.0-0ubuntu1.1
GitCommit:
docker-init:
Version: 0.19.0
GitCommit:
- 卸载
sudo apt-get purge docker-ce docker-ce-cli containerd.io docker-compose-plugin
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd
- 授权当前用户使用docker
sudo usermod -aG docker ${USER}
# 执行命令后退出登录 重新进入
docker version
Centos环境
- 卸载旧版本
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
- 安装工具及设置仓库
yum install -y yum-utils
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
- 安装
yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin
# 指定版本安装
yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io docker-compose-plugin
- 启动、停止服务
systemctl start docker
systemctl restart docker
systemctl stop docker
- 验证
docker run hello-world
# 返回以下说明正常使用
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:faa03e786c97f07ef34423fccceeec2398ec8a5759259f94d99078f264e9d7af
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/
- 卸载
yum remove docker-ce docker-ce-cli containerd.io docker-compose-plugin
# 删除相关文件
rm -rf /var/lib/docker
rm -rf /var/lib/containerd
后语
到这里,Docker的安装基本就行了,我们可以开始去使用Docker了。可以发现整个安装过程还是蛮简单的,一般都不会遇到啥大问题。但是在生产上去安装时,我们就需要进行更多的一些配置了,比如一些访问限制,权限等问题。接下来,我们就来看看怎么使用docker 技术吧。
喜欢的朋友可以扫描下方二维码点个关注,谢谢您的阅读。