Docker的简介
Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器或Windows 机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之间不会有任何接口。
一个完整的Docker有以下几个部分组成:
DockerClient客户端
Docker Daemon守护进程
Docker Image镜像
DockerContainer容器
Docker官网地址
Docker操作手册地址
Doker安装
使用环境:centos7
1、安装之前现卸载系统上原有的Docker
yum remove docker \
> docker-client \
> docker-client-latest \
> docker-common \
> docker-latest \
> docker-latest-logrotate \
> docker-logrotate \
> docker-engine
执行结果
[root@instance ~]# yum remove docker \
> docker-client \
> docker-client-latest \
> docker-common \
> docker-latest \
> docker-latest-logrotate \
> docker-logrotate \
> docker-engine
Loaded plugins: langpacks, versionlock
No Match for argument: docker
No Match for argument: docker-client
No Match for argument: docker-client-latest
No Match for argument: docker-common
No Match for argument: docker-latest
No Match for argument: docker-latest-logrotate
No Match for argument: docker-logrotate
No Match for argument: docker-engine
No Packages marked for removal
[root@instance ~]#
2、安装需要的安装包yum-utils
[root@instance ~]# yum install -y yum-utils
Loaded plugins: langpacks, versionlock
Excluding 1 update due to versionlock (use "yum versionlock status" to show it)
Package yum-utils-1.1.31-54.el7_8.noarch already installed and latest version
Nothing to do
此主机已安装最新的yum-utils
3、设置镜像仓库地址
docker默认的官方仓库地址
[root@instance ~]# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo ##此地址为官方的仓库地址,在国内建议不要用
阿里云的镜像仓库地址
[root@instance ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
还有很多其他的仓库地址,如:网友云、有道等
4、安装docker相关的引擎
#先更新yum软件包索引
[root@instance ~]# yum makecache fase
docker社区、ee企业版 ce为社区版 官方推荐使用ce版,默认安装最新的docker
版本,也可以指定版本安装
[root@instance ~]# yum install docker-ce docker-ce-cli containerd.io
5、启动docker
启动命令
[root@instance ~]# systemctl start docker
6、使用docker version 查看dockers是否启动
[root@instance ~]# docker version
Client: Docker Engine - Community
Version: 26.0.0
API version: 1.45
Go version: go1.21.8
Git commit: 2ae903e
Built: Wed Mar 20 15:21:09 2024
OS/Arch: linux/amd64
Context: default
Server: Docker Engine - Community
Engine:
Version: 26.0.0
API version: 1.45 (minimum version 1.24)
Go version: go1.21.8
Git commit: 8b79278
Built: Wed Mar 20 15:20:06 2024
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.6.28
GitCommit: ae07eda36dd25f8a1b98dfbf587313b99c0190bb
runc:
Version: 1.1.12
GitCommit: v1.1.12-0-g51d5e94
docker-init:
Version: 0.19.0
GitCommit: de40ad0
7、使用hello-world镜像测试docker
[root@instance ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
c1ec31eb5944: Pull complete
Digest: sha256:53641cd209a4fecfc68e21a99871ce8c6920b2e7502df0a20671c6fccc73a7c6
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/
以下为执行结果:
docker run运行思维导图:
8、查看本机存在那些docker镜像
[root@instance ~]# docker images
可以看到刚才在执行docker run hello-world的时候,已经将hello-world镜像拉去到本地镜像中了
9、了解 卸载docker
#卸载依赖
[root@instance ~]# yum remove docker-ce docker-ce-cli containerd.io
#删除资源
[root@instance ~]# rm -rf /var/lib/docker
/var/lib/docker 为docker默认的工作路径!
10、Docker的常见命令
1. 创建容器:
docker create
2. 启动容器:
docker start
3. 停止容器:
docker stop
4. 重启容器:
docker restart
5. 查看运行中的容器:
docker ps
6. 查看所有容器(包括已停止的):
docker ps -a
7. 删除容器:
docker rm
8. 构建镜像:
docker build -t
9. 查看本地镜像:
docker images
10. 删除本地镜像:
docker rmi
11. 进入容器的 shell:
docker exec -it
12. 查看容器日志:
docker logs
13. 将本地端口映射到容器端口:
docker run -p :
14. 后台运行容器:
docker run -d
参考:
https://blog.csdn.net/qq_26400011/article/details/113856681
https://blog.csdn.net/u014212540/article/details/130504873