docker简介
什么是docker
docker是一个开源的应用容器引擎,开发者可以打包自己的应用到容器里面,然后迁移到其他机器的docker应用中,可以实现快速部署。如果出现的故障,可以通过镜像,快速恢复服务。
docker原理
docker是利用Linux内核虚拟机化技术(LXC),提供轻量级的虚拟化,以便隔离进程和资源。LXC不是硬件的虚拟化,而是Linux内核的级别的虚拟机化,相对于传统的虚拟机,节省了很多硬件资源。
NameSpace
LXC是利用内核namespace技术,进行进程隔离。其中pid, net, ipc, mnt, uts 等namespace将container的进程, 网络, 消息, 文件系统和hostname 隔离开。
Control Group
LXC利用的宿主机共享的资源,虽然用namespace进行隔离,但是资源使用没有收到限制,这里就需要用到Control Group技术,对资源使用进行限制,设定优先级,资源控制等。
环境准备
操作系统:CentOS 7.6.1810
软件源:阿里云镜像(在阿里云镜像站上面可以找到docker-ce的软件源,使用国内的源速度比较快)
安装docker-ce
安装步骤
1、安装依赖
docker依赖于系统的一些必要的工具,需要提前安装
yum install -y yum-utils device-mapper-persistent-data lvm2
2、添加软件源
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
3、安装docker-ce
yum clean all
yum makecache fast
yum -y install docker-ce
4、启动docker服务
systemctl start docker
也可使用 systemctl enable docker
设置开机启动docker
5、查看docker版本
docker version
如果返回docker版本,则表示成功
[root@izbp17tc3pp4mvcytrz3bcz ~]# docker version
Client:
Version: 1.13.1
API version: 1.26
Package version: docker-1.13.1-103.git7f2769b.el7.centos.x86_64
Go version: go1.10.3
Git commit: 7f2769b/1.13.1
Built: Sun Sep 15 14:06:47 2019
OS/Arch: linux/amd64
Server:
Version: 1.13.1
API version: 1.26 (minimum version 1.12)
Package version: docker-1.13.1-103.git7f2769b.el7.centos.x86_64
Go version: go1.10.3
Git commit: 7f2769b/1.13.1
Built: Sun Sep 15 14:06:47 2019
OS/Arch: linux/amd64
Experimental: false
使用阿里云镜像加速器
docker hub下载速度很慢,而可以使用阿里云docker加速器,来提高速度,管理你的镜像和拉取别人的镜像
- 登录阿里云 https://cr.console.aliyun.com/
- 配置镜像加速器
针对Docker客户端版本大于 1.10.0 的用户
修改daemon配置文件/etc/docker/daemon.json来使用加速器.
registry-mirrors后面配置上图中红框内的地址
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://XXXXX.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
镜像操作
创建docker容器要以镜像为基础。
搜索镜像
docker search [name: versionId] 搜索可以下载的镜像, versionID是可选的
也可以到docker hub官网上搜索镜像
[root@izbp17tc3pp4mvcytrz3bcz ~]# docker search tomcat
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/tomcat Apache Tomcat is an open source implementa... 2587 [OK]
docker.io docker.io/tomee Apache TomEE is an all-Apache Java EE cert... 73 [OK]
docker.io docker.io/dordoka/tomcat Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 ba... 53 [OK]
docker.io docker.io/bitnami/tomcat Bitnami Tomcat Docker Image 30 [OK]
docker.io docker.io/kubeguide/tomcat-app Tomcat image for Chapter 1 28
docker.io docker.io/consol/tomcat-7.0 Tomcat 7.0.57, 8080, "admin/admin" 16 [OK]
docker.io docker.io/cloudesire/tomcat Tomcat server, 6/7/8 15 [OK]
docker.io docker.io/aallam/tomcat-mysql Debian, Oracle JDK, Tomcat & MySQL 12 [OK]
docker.io docker.io/arm32v7/tomcat Apache Tomcat is an open source implementa... 10
docker.io docker.io/rightctrl/tomcat CentOS , Oracle Java, tomcat application s... 5 [OK]
docker.io docker.io/maluuba/tomcat7-java8 Tomcat7 with java8. 4
docker.io docker.io/unidata/tomcat-docker Security-hardened Tomcat Docker container. 4 [OK]
docker.io docker.io/amd64/tomcat Apache Tomcat is an open source implementa... 2
docker.io docker.io/arm64v8/tomcat Apache Tomcat is an open source implementa... 2
docker.io docker.io/99taxis/tomcat7 Tomcat7 1 [OK]
docker.io docker.io/camptocamp/tomcat-logback Docker image for tomcat with logback integ... 1 [OK]
docker.io docker.io/i386/tomcat Apache Tomcat is an open source implementa... 1
docker.io docker.io/oobsri/tomcat8 Testing CI Jobs with different names. 1
docker.io docker.io/ppc64le/tomcat Apache Tomcat is an open source implementa... 1
docker.io docker.io/appsvc/tomcat 0
docker.io docker.io/cfje/tomcat-resource Tomcat Concourse Resource 0
docker.io docker.io/jelastic/tomcat An image of the Tomcat Java application se... 0
docker.io docker.io/picoded/tomcat7 tomcat7 with jre8 and MANAGER_USER / MANAG... 0 [OK]
docker.io docker.io/s390x/tomcat Apache Tomcat is an open source implementa... 0
docker.io docker.io/secoresearch/tomcat-varnish Tomcat and Varnish 5.0 0 [OK]
OFFICIAL 为[OK]的表示为官方提供的镜像
查看本机已安装的镜像
docker images
[root@izbp17tc3pp4mvcytrz3bcz docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/tomcat latest 6fa48e047721 7 days ago 507 MB
拉取镜像
docker pull [name: versionId] versionId是可选的
[root@izbp17tc3pp4mvcytrz3bcz docker]# docker pull tomcat
Using default tag: latest
Trying to pull repository docker.io/library/tomcat ...
latest: Pulling from docker.io/library/tomcat
844c33c7e6ea: Downloading [===> ] 2.743 MB/45.38 MB
ada5d61ae65d: Download complete
f8427fdf4292: Downloading [=======================================> ] 2.104 MB/2.638 MB
f025bafc4ab8: Downloading [=====================> ] 21.85 MB/50.07 MB
67b8714e1225: Waiting
64b12da521a3: Waiting
2e38df533772: Waiting
4144d55bbb47: Waiting
fc059d90e2b2: Waiting
9d8f80ed8620: Waiting
删除镜像
docker rmi imageId 镜像id可以通过docker images命令查看
[root@izbp17tc3pp4mvcytrz3bcz docker]# docker rmi 6fa48e047721
Untagged: docker.io/tomcat:latest
Untagged: docker.io/tomcat@sha256:68355b27adee5fc76c23e3d3cb994bd2733f05aa8e2c070a61346e16eed308ac
Deleted: sha256:6fa48e04772109a282efcc0eb42211b44d50c27156cd5f3bae77754882d67bb6
Deleted: sha256:62d023e77b1252586eed9b494feef7d3064fa6840be3371c81cb512440566406
Deleted: sha256:a106a7fb50963e543f857e3116c161cbfa57cfcd91ccf2117b159a8e5af2a4cb
Deleted: sha256:ab0e4bc9eb374158707db581ef55a732332cfd3fcc505baf22490e901eda185f
Deleted: sha256:b35dd16822b6808f5f8bd5da55f3241ff5cade5eb7d25d1440df90209ca159b8
Deleted: sha256:97200aa46a2ef0247e91462cc37b0bfa10b167da4a9fce0d54d598f70edb06d5
Deleted: sha256:7f3e89512c144c481c815517998f69b6063f33f36d6946ef1ec6cda6c42cd6e5
Deleted: sha256:23b818b261d944ed79f99d8b96ff917835f68b296a0002e094b3db2555e26f12
Deleted: sha256:8b595b250565c6ab1163e71f4104587188518185719929ce0429e16fdfb76b1e
Deleted: sha256:d692181dfebae7a739bdcbd55ce40348dcedecaa741d2978933ecc830955c44c
Deleted: sha256:e4b20fcc48f4a225fd29ce3b3686cc51042ce1f076d88195b3705b5bb2f38c3d
docker 容器创建与管理
创建容器
docker run
-i:允许你对容器内的标准输入 (STDIN) 进行交互。
-t: 在新容器内指定一个伪终端或终端
通过以上两个选项 就可以进入到容器内部,查看并管理容器内部的结构
-p: 指定容器映射到宿主机的端口,前者为映射到宿主机的端口,后者为容器的端口
容器运行时默认是以foreground模式运行。
在Docker容器退出时,默认容器内部的文件系统仍然被保留,以方便调试并保留用户数据。
对于foreground容器,由于其只是在开发调试过程中短期运行,其用户数据并无保留的必要,因而可以在容器启动时设置--rm选项,这样在容器退出时就能够自动清理容器内部的文件系统。
-d:指定容器以Detached模式运行,容器会在后台运行,并打印出容器ID。以detached模式运行的容器当它停止时无法自动删除,因此–rm选项和-d选项不能一起使用。
--name :为创建的容器命名
-v:表示目录映射关系(前者是宿主机目录,后者是映射到宿主机上的目录),可以使用多个-v做多个目录映射。 最好在宿主机上修改,然后共享到容器中
- 交互式方式创建容器
docker run -it --name=容器名 镜像名[: 标签] /bin/bash
创建后可以通过docker container ps
命令查看容器的状态
退出容器可以使用exit
[root@izbp17tc3pp4mvcytrz3bcz ~]# docker run -it -p 8080:8080 --name mytomcat tomcat /bin/bash
#进入容器后,root@后面的ID变成了容器ID,此时可以用命令对容器内的文件进行操作
root@97cf00d1fa5f:/usr/local/tomcat# ls
BUILDING.txt LICENSE README.md RUNNING.txt conf lib native-jni-lib webapps
CONTRIBUTING.md NOTICE RELEASE-NOTES bin include logs temp work
root@97cf00d1fa5f:/usr/local/tomcat# cd webapps/ROOT/
root@97cf00d1fa5f:/usr/local/tomcat/webapps/ROOT# ls
RELEASE-NOTES.txt asf-logo-wide.svg bg-middle.png bg-upper.png index.jsp tomcat.css tomcat.png
WEB-INF bg-button.png bg-nav.png favicon.ico tomcat-power.gif tomcat.gif tomcat.svg
root@97cf00d1fa5f:/usr/local/tomcat/webapps/ROOT#
#使用exit可以退出当前容器。当前为交互式方式运行,当退出时容器也就停止了
root@97cf00d1fa5f:/usr/local/tomcat/webapps/ROOT# exit
exit
#此时再用ps命令即可看到创建成功的Tomcat容器了
[root@izbp17tc3pp4mvcytrz3bcz ~]# docker container ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
97cf00d1fa5f tomcat "/bin/bash" 3 minutes ago Exited (0) 11 seconds ago mytomcat
[root@izbp17tc3pp4mvcytrz3bcz ~]#
查看正在运行的容器
docker container ps
docker container ps -a #查看所有容器,包括已经停止的
[root@izbp17tc3pp4mvcytrz3bcz ~]# docker container ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
97cf00d1fa5f tomcat "/bin/bash" 3 minutes ago Exited (0) 11 seconds ago mytomcat
CONTAINER ID:容器ID
IMAGE:容器对应的镜像
COMMAND: 启动容器时运行的命令。
CREATED: 容器的创建时间。
STATUS: 容器状态。
一共有其中状态
· created(已创建)
· restarting(重启中)
· running(运行中)
· removing(迁移中)
· paused(暂停)
· exited(停止)
· dead(死亡)
PORT:端口映射
NAMES:容器名
- 守护式方式创建容器
docker run -di --name [容器名] [镜像名:标签]
启动容器后可以通过docker exec it [容器名] /bin/bash
进入容器内
这种方式创建的容器,通过exit
命令退出容器后,容器还是在后台运行
[root@izbp17tc3pp4mvcytrz3bcz ~]# docker run -di -p 8080:8080 --name mytomcat2 tomcat
6330b1f6a99480771ac68bb44ac71f3c7068005a1f66076c013f0d77818d5313
[root@izbp17tc3pp4mvcytrz3bcz ~]# docker exec -it mytomcat2 /bin/bash
root@6330b1f6a994:/usr/local/tomcat# ls
BUILDING.txt LICENSE README.md RUNNING.txt conf lib native-jni-lib webapps
CONTRIBUTING.md NOTICE RELEASE-NOTES bin include logs temp work
root@6330b1f6a994:/usr/local/tomcat# exit
exit
#可见从容器中退出后,该容器状态仍然为Up,即仍在后台运行
[root@izbp17tc3pp4mvcytrz3bcz ~]# docker container ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6330b1f6a994 tomcat "catalina.sh run" 32 seconds ago Up 31 seconds 0.0.0.0:8080->8080/tcp mytomc
at2
97cf00d1fa5f tomcat "/bin/bash" 21 minutes ago Exited (0) 18 minutes ago mytomc
at
[root@izbp17tc3pp4mvcytrz3bcz ~]#
启动、停止、杀死、删除指定的容器
当容器创建成功,就可以对容器进行启动、停止、杀死、删除等操作
docker start [容器名 or 容器ID] #启动容器
docker stop [容器名 or 容器ID] #停止容器
docker kill [容器名 or 容器ID] #杀死容器
docker rm [容器名 or 容器ID] #删除容器
[root@izbp17tc3pp4mvcytrz3bcz ~]# docker stop mysql_dyw
mysql_dyw
[root@izbp17tc3pp4mvcytrz3bcz ~]# docker start mysql_dyw
mysql_dyw
查看日志
docker logs [容器名]
[root@izbp17tc3pp4mvcytrz3bcz ~]# docker logs 6330b1f6a994
21-Dec-2019 04:40:38.683 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version name: Apache Tomcat/8.5.50
21-Dec-2019 04:40:38.771 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built: Dec 7 2019 19:19:46 UTC
21-Dec-2019 04:40:38.771 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version number: 8.5.50.0
21-Dec-2019 04:40:38.771 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name: Linux
21-Dec-2019 04:40:38.771 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version: 3.10.0-514.26.2.el7.x86_6
4
21-Dec-2019 04:40:38.771 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture: amd64
21-Dec-2019 04:40:38.771 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home: /usr/local/openjdk-8/jre
21-Dec-2019 04:40:38.771 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version: 1.8.0_232-b09
21-Dec-2019 04:40:38.771 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor: Oracle Corporation
21-Dec-2019 04:40:38.772 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE: /usr/local/tomcat
21-Dec-2019 04:40:38.772 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME: /usr/local/tomcat
21-Dec-2019 04:40:38.772 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.confi
g.file=/usr/local/tomcat/conf/logging.properties
21-Dec-2019 04:40:38.772 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manag
er=org.apache.juli.ClassLoaderLogManager
21-Dec-2019 04:40:38.772 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeyS
ize=2048
21-Dec-2019 04:40:38.772 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.p
kgs=org.apache.catalina.webresources
21-Dec-2019 04:40:38.772 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dorg.apache.catalina.sec
urity.SecurityListener.UMASK=0027
21-Dec-2019 04:40:38.772 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs=
21-Dec-2019 04:40:38.772 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/usr/loca
l/tomcat
21-Dec-2019 04:40:38.773 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/usr/loca
l/tomcat
21-Dec-2019 04:40:38.773 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/usr/loc
al/tomcat/temp
21-Dec-2019 04:40:38.773 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded APR based Apache Tomcat Native lib
rary [1.2.23] using APR version [1.5.2].
21-Dec-2019 04:40:38.773 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [
true], accept filters [false], random [true].
21-Dec-2019 04:40:38.773 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR/OpenSSL configuration: useAprConnecto
r [false], useOpenSSL [true]
21-Dec-2019 04:40:38.785 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized [OpenSSL
1.1.0l 10 Sep 2019]
21-Dec-2019 04:40:39.017 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]
21-Dec-2019 04:40:39.038 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write
/read
21-Dec-2019 04:40:39.060 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-nio-8009"]
21-Dec-2019 04:40:39.069 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write
/read
21-Dec-2019 04:40:39.074 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 1563 ms
21-Dec-2019 04:40:39.124 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]
21-Dec-2019 04:40:39.124 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.5.50
21-Dec-2019 04:40:39.146 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application dire
ctory [/usr/local/tomcat/webapps/docs]
21-Dec-2019 04:44:53.020 WARNING [localhost-startStop-1] org.apache.catalina.util.SessionIdGeneratorBase.createSecureRandom Creation of Sec
ureRandom instance for session ID generation using [SHA1PRNG] took [253,343] milliseconds.
21-Dec-2019 04:44:53.055 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application
directory [/usr/local/tomcat/webapps/docs] has finished in [253,908] ms
21-Dec-2019 04:44:53.055 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application dire
ctory [/usr/local/tomcat/webapps/manager]
21-Dec-2019 04:44:53.118 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application
directory [/usr/local/tomcat/webapps/manager] has finished in [63] ms
21-Dec-2019 04:44:53.118 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application dire
ctory [/usr/local/tomcat/webapps/host-manager]
21-Dec-2019 04:44:53.161 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application
directory [/usr/local/tomcat/webapps/host-manager] has finished in [43] ms
21-Dec-2019 04:44:53.171 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application dire
ctory [/usr/local/tomcat/webapps/ROOT]
21-Dec-2019 04:44:53.215 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application
directory [/usr/local/tomcat/webapps/ROOT] has finished in [54] ms
21-Dec-2019 04:44:53.215 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application dire
ctory [/usr/local/tomcat/webapps/examples]
21-Dec-2019 04:44:53.764 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application
directory [/usr/local/tomcat/webapps/examples] has finished in [548] ms
21-Dec-2019 04:44:53.783 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
21-Dec-2019 04:44:53.816 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"]