Docker 简介
Docker :The Modern Platform for High-Velocity Innovation
维基百科:Docker是一个开源项目,可自动部署软件容器内的应用程序。
百度百科:Docker是一个开源的应用容器引擎,让开发者可以打包他们的应用及依赖包到一
个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化。
一、更新yum
[root@ecs-f4ff bin]# yum update
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.huaweicloud.com
* epel: mirrors.tuna.tsinghua.edu.cn
* extras: mirrors.huaweicloud.com
* updates: mirrors.huaweicloud.com
Resolving Dependencies
--> Running transaction check
---> Package binutils.x86_64 0:2.27-41.base.el7 will be updated
....
Verifying : python-perf-3.10.0-1062.1.1.el7.x86_64 50/50
Installed:
kernel.x86_64 0:3.10.0-1062.4.1.el7 kernel-devel.x86_64 0:3.10.0-1062.4.1.el7
Updated:
binutils.x86_64 0:2.27-41.base.el7_7.1 centos-release.x86_64 0:7-7.1908.0.el7.centos device-mapper.x86_64 7:1.02.158-2.el7_7.2 device-mapper-libs.x86_64 7:1.02.158-2.el7_7.2 dnsmasq.x86_64 0:2.76-10.el7_7.1
firewalld.noarch 0:0.6.3-2.el7_7.2 firewalld-filesystem.noarch 0:0.6.3-2.el7_7.2 hostname.x86_64 0:3.13-3.el7_7.1 initscripts.x86_64 0:9.49.47-1.el7 kernel-headers.x86_64 0:3.10.0-1062.4.1.el7
kernel-tools.x86_64 0:3.10.0-1062.4.1.el7 kernel-tools-libs.x86_64 0:3.10.0-1062.4.1.el7 libgudev1.x86_64 0:219-67.el7_7.2 microcode_ctl.x86_64 2:2.1-53.2.el7_7 patch.x86_64 0:2.7.1-12.el7_7
procps-ng.x86_64 0:3.3.10-26.el7_7.1 python-firewall.noarch 0:0.6.3-2.el7_7.2 python-perf.x86_64 0:3.10.0-1062.4.1.el7 rsyslog.x86_64 0:8.24.0-41.el7_7.2 sos.noarch 0:3.7-7.el7.centos
sudo.x86_64 0:1.8.23-4.el7_7.1 systemd.x86_64 0:219-67.el7_7.2 systemd-libs.x86_64 0:219-67.el7_7.2 systemd-sysv.x86_64 0:219-67.el7_7.2
Complete!
二、安装需要的软件包,yum-util提供yum-config-manager功能
[root@ecs-f4ff bin]# yum install -y yum-utils
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.huaweicloud.com
* epel: mirrors.tuna.tsinghua.edu.cn
* extras: mirrors.huaweicloud.com
* updates: mirrors.huaweicloud.com
Resolving Dependencies
--> Running transaction check
---> Package yum-utils.noarch 0:1.1.31-52.el7 will be installed
--> Processing Dependency: python-kitchen for package: yum-utils-1.1.31-52.el7.noarch
--> Running transaction check
---> Package python-kitchen.noarch 0:1.1.1-5.el7 will be installed
--> Processing Dependency: python-chardet for package: python-kitchen-1.1.1-5.el7.noarch
--> Running transaction check
---> Package python-chardet.noarch 0:2.2.1-3.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
====================================================================================================================================================================================================================================================================
Package Arch Version Repository Size
====================================================================================================================================================================================================================================================================
Installing:
......
Installed:
yum-utils.noarch 0:1.1.31-52.el7
Dependency Installed:
python-chardet.noarch 0:2.2.1-3.el7 python-kitchen.noarch 0:1.1.1-5.el7
Complete!
三、设置yum源
[root@ecs-f4ff bin]# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
Loaded plugins: fastestmirror
adding repo from: https://download.docker.com/linux/centos/docker-ce.repo
grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
四、查看仓库中Docker版本
[root@ecs-f4ff bin]# yum list docker-ce --showduplicates | sort -r
* updates: mirrors.huaweicloud.com
Loading mirror speeds from cached hostfile
Loaded plugins: fastestmirror
* extras: mirrors.huaweicloud.com
* epel: mirrors.tuna.tsinghua.edu.cn
docker-ce.x86_64 3:19.03.4-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.3-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.2-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.1-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.0-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.9-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.8-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.7-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.6-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.5-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.4-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.3-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.2-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.1-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.0-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.3.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.2.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.1.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.0.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.03.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 18.03.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.12.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.12.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.09.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.09.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.06.2.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.06.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.06.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.03.3.ce-1.el7 docker-ce-stable
docker-ce.x86_64 17.03.2.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.03.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.03.0.ce-1.el7.centos docker-ce-stable
* base: mirrors.huaweicloud.com
Available Packages
五、正式安装Docker,yum install docker-ce-18.06.3.ce(最后为版本号)
[root@ecs-f4ff bin]# yum install docker-ce-18.06.3.ce
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.huaweicloud.com
* epel: mirrors.tuna.tsinghua.edu.cn
* extras: mirrors.huaweicloud.com
* updates: mirrors.huaweicloud.com
Resolving Dependencies
--> Running transaction check
---> Package docker-ce.x86_64 0:18.06.3.ce-3.el7 will be installed
--> Processing Dependency: container-selinux >= 2.9 for package: docker-ce-18.06.3.ce-3.el7.x86_64
--> Processing Dependency: libseccomp >= 2.3 for package: docker-ce-18.06.3.ce-3.el7.x86_64
--> Processing Dependency: libcgroup for package: docker-ce-18.06.3.ce-3.el7.x86_64
--> Processing Dependency: libltdl.so.7()(64bit) for package: docker-ce-18.06.3.ce-3.el7.x86_64
--> Processing Dependency: libseccomp.so.2()(64bit) for package: docker-ce-18.06.3.ce-3.el7.x86_64
--> Running transaction check
---> Package container-selinux.noarch 2:2.107-3.el7 will be installed
......
Installed:
docker-ce.x86_64 0:18.06.3.ce-3.el7
Dependency Installed:
audit-libs-python.x86_64 0:2.8.5-4.el7 checkpolicy.x86_64 0:2.5-8.el7 container-selinux.noarch 2:2.107-3.el7 libcgroup.x86_64 0:0.41-21.el7 libseccomp.x86_64 0:2.3.1-3.el7 libsemanage-python.x86_64 0:2.5-14.el7 libtool-ltdl.x86_64 0:2.4.2-22.el7_3
policycoreutils-python.x86_64 0:2.5-33.el7 python-IPy.noarch 0:0.75-6.el7 setools-libs.x86_64 0:3.3.8-4.el7
Complete!
六、已经安装完成,然后启动Docker服务
1. 启动命令: systemctl start docker
2. 检验Docker是否正确安装命令: docker container run -i -t hello-world
如此,Docker第一个程序 HelloWorld完成,如下
[root@ecs-f4ff bin]# systemctl start docker
[root@ecs-f4ff bin]# docker container run -i -t hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:c3b4ada4687bbaa170745b3e4dd8ac3f194ca95b2d0518b417fb47e5879d9b5f
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相关命令
docker image pull [OPTIONS] NAME[:TAG|@DIGEST] //pull从仓库拉取镜像到本地
docker image ls [OPTIONS] [REPOSITORY[:TAG]] //列出镜像
docker image ls [OPTIONS] [REPOSITORY[:TAG]] //查看镜像详细信息
docker image history [OPTIONS] NAME[:TAG] //查看构建过程
docker run [OPTIONS] IMAGE [COMMAND] [ARG...] //创建并启动容器
docker cp [OPTIONS] SRC_PATH CONTAINER:DEST_PATH //将本地文件拷贝到容器
docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]] //将容器提交为新的镜像
docker image tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG] //给镜像打标签
docker push [OPTIONS] NAME[:TAG] //上传镜像或仓库到注册服务器
docker image rm [OPTIONS] IMAGE [IMAGE...] //将本地镜像删除
docker image load [OPTIONS] //从tar文件中载入镜像
docker image save [OPTIONS] IMAGE [IMAGE...] //将镜像保存为tar文件
docker container start [OPTIONS] CONTAINER [CONTAINER...] //启动已终止的容器
docker container ls [OPTIONS] //查看容器
docker container logs [OPTIONS] CONTAINER //打印容器日志
docker container attach [OPTIONS] CONTAINER //进入正在运行的容器里面
docker container exec [OPTIONS] CONTAINER COMMAND [ARG...] //再指定运行的容器中执行某个命令
docker container stop [OPTIONS] CONTAINER [CONTAINER...] //停止容器运行并保存状态
docker container kill [OPTIONS] CONTAINER [CONTAINER...] //强行停止运行的容器
docker container rm [OPTIONS] CONTAINER [CONTAINER...] //删除某个容器
docker login [OPTIONS] [SERVER] //登录注册的服务器
docker search [OPTIONS] TERM //搜索Docker Hub上的某个镜像或者仓库
docker pull [OPTIONS] NAME[:TAG|@DIGEST] //从注册服务器拉去镜像(或仓库)到本地
docker logout [SERVER] //注销登录状态