Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任何接口。
此处主要讲解在CentOS-7.3中安装Docker环境
1、安装环境
CentOS Linux release 7.3.1611
最新版本 Docker 18.03.1-ce
2、Docker安装步骤
注意 yum默认安装的docker为 1.13 版本,比较旧而且并不是开源社区维护的版本,目前开源社区最新版本为 18.03.1,社区分为开源版本(docker-ce)和企业版本(docker-ee)。 需要配置Docker官方源才可以安装最新版本,此处我们安装开源社区版本 docker-ce。
1、为yum配置docker官方源
为docker 增加一个新的yum配置文件:
vi /etc/yum.repos.d/docker.repo
具体内容如下:
[docker-ce-stable]name=Docker CE Stable - $basearchbaseurl=https://download.docker.com/linux/centos/7/$basearch/stableenabled=1gpgcheck=0gpgkey=https://download.docker.com/linux/centos/gpg[docker-ce-stable-debuginfo]name=Docker CE Stable - Debuginfo $basearchbaseurl=https://download.docker.com/linux/centos/7/debug-$basearch/stableenabled=0gpgcheck=1gpgkey=https://download.docker.com/linux/centos/gpg[docker-ce-stable-source]name=Docker CE Stable - Sourcesbaseurl=https://download.docker.com/linux/centos/7/source/stableenabled=0gpgcheck=1gpgkey=https://download.docker.com/linux/centos/gpg[docker-ce-edge]name=Docker CE Edge - $basearchbaseurl=https://download.docker.com/linux/centos/7/$basearch/edgeenabled=0gpgcheck=1gpgkey=https://download.docker.com/linux/centos/gpg[docker-ce-edge-debuginfo]name=Docker CE Edge - Debuginfo $basearchbaseurl=https://download.docker.com/linux/centos/7/debug-$basearch/edgeenabled=0gpgcheck=1gpgkey=https://download.docker.com/linux/centos/gpg[docker-ce-edge-source]name=Docker CE Edge - Sourcesbaseurl=https://download.docker.com/linux/centos/7/source/edgeenabled=0gpgcheck=1gpgkey=https://download.docker.com/linux/centos/gpg[docker-ce-test]name=Docker CE Test - $basearchbaseurl=https://download.docker.com/linux/centos/7/$basearch/testenabled=0gpgcheck=1gpgkey=https://download.docker.com/linux/centos/gpg[docker-ce-test-debuginfo]name=Docker CE Test - Debuginfo $basearchbaseurl=https://download.docker.com/linux/centos/7/debug-$basearch/testenabled=0gpgcheck=1gpgkey=https://download.docker.com/linux/centos/gpg[docker-ce-test-source]name=Docker CE Test - Sourcesbaseurl=https://download.docker.com/linux/centos/7/source/testenabled=0gpgcheck=1gpgkey=https://download.docker.com/linux/centos/gpg
配置完成后即可搜索到最新的Docker文件如下图:
[root@localhost yum.repos.d]# yum search docker-ceLoaded plugins: fastestmirror, langpacksLoading mirror speeds from cached hostfile * base: mirrors.sohu.com * epel: mirrors.sohu.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com=========================================================================================================================== N/S matched: docker-ce ===========================================================================================================================docker-ce.x86_64 : The open-source application container enginedocker-ce-selinux.noarch : SELinux Policies for the open-source application container engine Name and summary matches only, use "search all" for everything.
安装最新版docker,此处安装社区开源版,如下:
[root@localhost yum.repos.d]# yum install docker-ce
2、查看已安装Docker的版本
执行如下命令可查看Docker版本:
docker -v
显示版本信息如下:
Docker version 18.03.1-ce, build 9ee9f40
3、启动Docker服务
systemctl start docker.service
4、执行测试hello-world镜像
docker run hello-world
此时会出现无法连接的情况,因为Docker的官网被墙,默认的镜像源地址无法连接,会提示如下信息:
[root@localhost docker]# docker run hello-world/usr/bin/docker-current: Cannot connect to the Docker daemon. Is the docker daemon running on this host?.See '/usr/bin/docker-current run --help'.
解决此问题则需要修改镜像源地址,修改 /etc/docker/daemon.json 文件,填写Docker 官方中国区镜像源地址,修改后/etc/docker/daemon.json 内容如下:
{ "registry-mirrors": ["https://registry.docker-cn.com"]}
也可使用国内其他Docker镜像源地址
- 网易:http://hub-mirror.c.163.com
- ustc:https://docker.mirrors.ustc.edu.cn
修改完成后重新执行 测试hello-world镜像,本地没有的话则会从远端镜像库下载,成功执行信息如下:
[root@localhost docker]# docker run hello-worldUnable to find image 'hello-world:latest' locallyTrying to pull repository docker.io/library/hello-world ... latest: Pulling from docker.io/library/hello-worldca4f61b1923c: Pull complete Digest: sha256:445b2fe9afea8b4aa0b2f27fe49dd6ad130dfe7a8fd0832be5de99625dad47cdHello 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 bashShare images, automate workflows, and more with a free Docker ID: https://cloud.docker.com/For more examples and ideas, visit: https://docs.docker.com/engine/userguide/
看到此信息,则说明安装Docker成功。
3、docker-compose 安装
Compose 是一个用户定义和运行多个容器的 Docker 应用程序。在 Compose 中你可以使用 YAML 文件来配置你的应用服务。然后,只需要一个简单的命令,就可以创建并启动你配置的所有服务。
使用 Compose 基本会有如下三步流程:
- 在 Dockfile 中定义你的应用环境,使其可以在任何地方复制。
- 在 docker-compose.yml 中定义组成应用程序的服务,以便它们可以在隔离的环境中一起运行。
- 最后,运行
dcoker-compose up
,Compose 将启动并运行整个应用程序。
安装python-pip
yum -y install epel-releaseyum -y install python-pip
安装docker-compose
pip install docker-compose
装完成后,执行查询版本的命令,即可安装docker-compose
[root@localhost ~]# docker-compose versiondocker-compose version 1.21.2, build a133471docker-py version: 3.3.0CPython version: 2.7.5OpenSSL version: OpenSSL 1.0.2k-fips 26 Jan 2017
4、Docker基本命令使用
需要首先清晰的认识Docker中镜像与容器之间的关系,镜像和容器就相当于Java中的类和对象的关系,镜像是对容器的初始定义,运行镜像之后会得到容器,容器提交版本后会生成镜像,Docker 的大部分操作都是针对容器的。
1、对镜像的操作
1、查看本地镜像
查看本地有哪些镜像文件
[root@localhost docker]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEdocker.io/hello-world latest f2a91732366c 4 weeks ago 1.848 kB
2、搜索镜像文件
可通过搜索查找自己想要的镜像,此处我们搜索centos
[root@localhost docker]# docker search centosINDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATEDdocker.io docker.io/centos The official build of CentOS. 3893 [OK] docker.io docker.io/ansible/centos7-ansible Ansible on Centos7 103 [OK]docker.io docker.io/jdeathe/centos-ssh CentOS-6 6.9 x86_64 / CentOS-7 7.4.1708 x8... 90 [OK]docker.io docker.io/consol/centos-xfce-vnc Centos container with "headless" VNC sessi... 37 [OK]docker.io docker.io/imagine10255/centos6-lnmp-php56 centos6-lnmp-php56 35 [OK]docker.io docker.io/tutum/centos Simple CentOS docker image with SSH access 34 docker.io docker.io/gluster/gluster-centos Official GlusterFS Image [ CentOS-7 + Glu... 21 [OK]docker.io docker.io/kinogmt/centos-ssh CentOS with SSH 18 [OK]docker.io docker.io/centos/python-35-centos7 Platform for building and running Python 3... 14 docker.io docker.io/openshift/base-centos7 A Centos7 derived base image for Source-To... 13 docker.io docker.io/openshift/jenkins-2-centos7 A Centos7 based Jenkins v2.x image for use... 9 docker.io docker.io/openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 ima... 6 docker.io docker.io/darksheer/centos Base Centos Image -- Updated hourly 3 [OK]docker.io docker.io/openshift/ruby-20-centos7 DEPRECATED: A Centos7 based Ruby v2.0 imag... 3 docker.io docker.io/blacklabelops/centos CentOS Base Image! Built and Updates Daily! 1 [OK]docker.io docker.io/indigo/centos-maven Vanilla CentOS 7 with Oracle Java Developm... 1 [OK]docker.io docker.io/miko2u/centos6 CentOS6 日本語環境 1 [OK]docker.io docker.io/openshift/php-55-centos7 DEPRECATED: A Centos7 based PHP v5.5 image... 1 docker.io docker.io/pivotaldata/centos-gpdb-dev CentOS image for GPDB development. Tag nam... 1 docker.io docker.io/pivotaldata/centos-mingw Using the mingw toolchain to cross-compile... 1 docker.io docker.io/jameseckersall/sonarr-centos Sonarr on CentOS 7 0 [OK]docker.io docker.io/openshift/wildfly-101-centos7 A Centos7 based WildFly v10.1 image for us... 0 docker.io docker.io/pivotaldata/centos Base centos, freshened up a little with a ... 0 docker.io docker.io/pivotaldata/centos-gcc-toolchain CentOS with a toolchain, but unaffiliated ... 0 docker.io docker.io/smartentry/centos centos with smartentry 0 [OK]
3、获取远程镜像
可按照搜索结果名称进行获取,此处我们获取 发布版本Centos:The official build of CentOS
[root@localhost docker]# docker pull docker.io/centosUsing default tag: latestTrying to pull repository docker.io/library/centos ... latest: Pulling from docker.io/library/centos85432449fd0f: Pull complete Digest: sha256:3b1a65e9a05f0a77b5e8a698d3359459904c2a354dc3b25ae2e2f5c95f0b3667
4、运行镜像
此处运行刚才获取的镜像,镜像运行之后成为容器。
[root@localhost docker]# docker run -i -t docker.io/centos /bin/bash[root@d86eed7c4bf0 /]# lsanaconda-post.log bin dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
此时已进入镜像文件内部,此时我们可以对此容器进行更改,比如在 /home目录下创建test.txt文件,内容为 Hello my test file ! 。
5、退出(关闭)镜像
[root@d86eed7c4bf0 /]# exit exit[root@localhost docker]#
exit 命令会退出容器并且关闭容器,如果只需要退出容器但不需要关闭则可按快捷键:Ctrl+P+Q
6、提交容器成为镜像
此处定义提交的容器的Tab为 v1.1
[zhpt@localhost ~]$ docker commit -m ="Commit message" -a="Author" 22fd7c4c0668 docker.io/centos:v1.1sha256:acef7126a6ac4e5abd3acfcd92655d1e41a45b9cc57b7d60eea7d75dc175667e
可是用 docker images 命令查看新提交的镜像,如下:
[zhpt@localhost ~]$ docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEdocker.io/centos v1.1 acef7126a6ac 4 minutes ago 271.3 MBdocker.io/centos latest 3fa822599e10 2 weeks ago 203.5 MBdocker.io/hello-world latest f2a91732366c 4 weeks ago 1.848 kB
7、删除镜像文件
删除刚才新提交的镜像文件:docker.io/centos:v1.1
[zhpt@localhost ~]$ docker rmi docker.io/centos:v1.1Untagged: docker.io/centos:v1.1Deleted: sha256:acef7126a6ac4e5abd3acfcd92655d1e41a45b9cc57b7d60eea7d75dc175667eDeleted: sha256:47744657ffff5c29bc5f73c0bc8ad9677688a50d89dda5777ef7467603fe2469[zhpt@localhost ~]$ docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEdocker.io/centos latest 3fa822599e10 2 weeks ago 203.5 MBdocker.io/hello-world latest f2a91732366c 4 weeks ago 1.848 kB
2、对容器的操作
1、查看正在运行的镜像
在镜像运行后,关闭之前可以查看正在运行的镜像
[zhpt@localhost ~]$ docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESd86eed7c4bf0 docker.io/centos "/bin/bash" About a minute ago Up About a minute elated_wescoff
2、停止正在执行的容器
[zhpt@localhost ~]$ docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESd86eed7c4bf0 docker.io/centos "/bin/bash" 15 hours ago Up 4 minutes elated_wescoff[zhpt@localhost ~]$ docker kill d86eed7c4bf0d86eed7c4bf0[zhpt@localhost ~]$ docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES[zhpt@localhost ~]$
也可停止所有正在执行的容器
[zhpt@localhost ~]$ docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESd86eed7c4bf0 docker.io/centos "/bin/bash" 15 hours ago Up 2 minutes elated_wescoff[zhpt@localhost ~]$ docker kill $(docker ps -q)d86eed7c4bf0[zhpt@localhost ~]$ docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES[zhpt@localhost ~]$
3、删除已经停止的容器
只可删除已经停止的容器,正在运行的容器是无法删除的。
[zhpt@localhost ~]$ docker rm 22fd7c4c066822fd7c4c0668[zhpt@localhost ~]$ docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESd86eed7c4bf0 docker.io/centos "/bin/bash" 39 minutes ago Exited (127) 38 minutes ago elated_wescoffbda7d545ac4c docker.io/centos "/bin/bash" 2 hours ago Exited (0) About an hour ago clever_mestorfe1c6121e4845 hello-world "/hello" 2 hours ago Exited (0) 2 hours ago suspicious_ardinghelli
删除所有已经停止的容器
[root@localhost zhpt]# docker rm $(docker ps -a -q)2853ae52f76d5c721cd6810bfd2123aa64a284a6405887b4bda7d545ac4ce1c6121e4845
4、启动已经停止的容器
[zhpt@localhost ~]$ docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESd86eed7c4bf0 docker.io/centos "/bin/bash" 15 hours ago Exited (137) 47 seconds ago elated_wescoffbda7d545ac4c docker.io/centos "/bin/bash" 17 hours ago Exited (0) 16 hours ago clever_mestorfe1c6121e4845 hello-world "/hello" 17 hours ago Exited (0) 6 minutes ago suspicious_ardinghelli[zhpt@localhost ~]$ docker start -i d86eed7c4bf0[root@d86eed7c4bf0 /]#
5、对容器重命名
因为容器的ID是随机码,而容器的名字又是看似无意义的命名,我们可以使用命令重命名,可使用新名称对容器进行操作。
[zhpt@localhost ~]$ docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESd86eed7c4bf0 docker.io/centos "/bin/bash" 15 hours ago Exited (137) 4 minutes ago elated_wescoffbda7d545ac4c docker.io/centos "/bin/bash" 17 hours ago Exited (0) 16 hours ago clever_mestorfe1c6121e4845 hello-world "/hello" 17 hours ago Exited (0) 14 minutes ago suspicious_ardinghelli[zhpt@localhost ~]$ docker rename elated_wescoff my_centos[zhpt@localhost ~]$ docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESd86eed7c4bf0 docker.io/centos "/bin/bash" 15 hours ago Exited (137) 4 minutes ago my_centosbda7d545ac4c docker.io/centos "/bin/bash" 17 hours ago Exited (0) 16 hours ago clever_mestorfe1c6121e4845 hello-world "/hello" 17 hours ago Exited (0) 14 minutes ago suspicious_ardinghelli[zhpt@localhost ~]$ docker start -i my_centos [root@d86eed7c4bf0 /]#
6、进入正在运行的容器
[zhpt@localhost ~]$ docker start -i my_centos [root@d86eed7c4bf0 /]# [zhpt@localhost ~]$ [zhpt@localhost ~]$ docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESd86eed7c4bf0 docker.io/centos "/bin/bash" 16 hours ago Up 14 minutes my_centos[zhpt@localhost ~]$ docker attach d86eed7c4bf0[root@d86eed7c4bf0 /]#
推荐使用如下方式:
[root@localhost es]# docker exec -it 0f19337a8ac9 /bin/bashroot@0f19337a8ac9:/usr/src/app#
3、其他常用命令
1、导出镜像为文件
- docker save 2f8543cab450 > /root/images/elasticsearch-head.tar
2、导入文件为镜像
- docker load < /home/zhpt/es/elasticsearch-head.tar
3、重命名镜像
- docker tag 2f8543cab450 elasticsearch-head:v1