文章目录
Docker简介
Docker是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任何接口。
Docker的优点
要了解Docker的优点,首先我们得看看传统项目的发布流程。
传统项目发布流程
传统的项目发布到Linux服务器的大致流程:
1、安装JDK
2、安装Tomcat
3、将项目war包上传到tomcat的webapp下
4、修改配置文件
5、启动Tomcat
这样看似没问题,其实我们想一想,发一台机器还好,这几步就完成了,但若发布到多台机器,其效率是非常低下的,不仅仅浪费了太多的不必要时间,而且还增大了人为操作的出错率。
使用Docker发布项目的优点
作为一种新兴的虚拟机方式,Docker跟传统的虚拟机方式相比具有众多的优势。
首先,Docker容器的启动可以在毫秒级实现,这相比传统的虚拟机方式要快得多。其次,Docker对系统资源的利用率很高,一台主机上可以同时运行数千个Docker容器。
容器除了运行其中应用外,基本不消耗额外的系统资源,使得应用的性能很高,同时系统的开销尽量小。传统虚拟机方式运行10个不同的应用就要起10个虚拟机,而Docker只需要启动10个隔离的应用即可。
Docker的三大核心概念
镜像
镜像简介
镜像是Docker的三大核心概念之一。
Docker运行容器前需要本地存在对应的镜像,如果镜像不存在本地,Docker会尝试先从默认镜像仓库下载(默认使用DockerHub公共注册服务器中的仓库),用户也可以通过配置,使用自定义的镜像仓库。
Docker对镜像的基本操作
docker对镜像的基本操作,我们以使用hello-world
镜像为例来进行说明
搜索:
docker search hello
搜索的结果为:
[root@localhost ~]# docker search hello
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/hello-world Hello World! (an example of minimal Docker... 770 [OK]
docker.io docker.io/tutum/hello-world Image to test docker deployments. Has Apac... 59 [OK]
docker.io docker.io/openshift/hello-openshift Simple Example for Running a Container on ... 31
docker.io docker.io/google/nodejs-hello 24 [OK]
docker.io docker.io/dockercloud/hello-world Hello World! 14 [OK]
docker.io docker.io/karthequian/helloworld A simple helloworld nginx container to get... 12 [OK]
docker.io docker.io/nginxdemos/hello NGINX webserver that serves a simple page ... 8 [OK]
在这里,我们直接受用第一个docker.io/hello-world
。
拉取:
docker pull docker.io/hello-world
拉取的结果为:
[root@localhost ~]# docker pull docker.io/hello-world
Using default tag: latest
Trying to pull repository docker.io/library/hello-world ...
latest: Pulling from docker.io/library/hello-world
d1725b59e92d: Pull complete
Digest: sha256:0add3ace90ecb4adbf7777e9aacf18357296e799f81cabc9fde470971e499788
Status: Downloaded newer image for docker.io/hello-world:latest
查看:
当我们拉取了镜像后,如果我们想查看自己所拉取的镜像,可以采用这种方式:
docker images
查看的结果为:
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
springcloud-eureka 1.0 978a5f35200e 19 hours ago 686 MB
docker.io/hello-world latest 4ab4c602aa5e 3 months ago 1.84 kB
docker.io/wordpress latest 34947222d667 11 months ago 410 MB
docker.io/owncloud latest 683714670c5b 12 months ago 601 MB
docker.io/mysql latest 7d83a47ab2d2 12 months ago 408 MB
docker.io/tomcat latest 72d2be374029 16 months ago 292 MB
docker.io/java 8-jre e44d62cf8862 23 months ago 311 MB
docker.io/java 8 d23bdf5b1b1b 23 months ago 643 MB
删除:
docker rmi docker.io/hello-world
删除结果为:
[root@localhost ~]# docker rmi docker.io/hello-world
Untagged: docker.io/hello-world:latest
Untagged: docker.io/hello-world@sha256:0add3ace90ecb4adbf7777e9aacf18357296e799f81cabc9fde470971e499788
Deleted: sha256:4ab4c602aa5eed5528a6620ff18a1dc4faef0e1ab3a5eddeddb410714478c67f
Deleted: sha256:428c97da766c4c13b19088a471de6b622b038f3ae8efa10ec5a37d6d31a2df0b
容器
容器简介
容器是Docker的另一个核心概念。
简单的说,容器是镜像的一个运行实例,所不同的是,它带有额外的可写文件层。
如果认为虚拟机是模拟运行的一整套操作系统(提供了运行态环境和其它系统环境)和跑在上面的应用。那么Docker容器就是独立运行的一个或一组应用,以及它们的必须运行环境。
容器的基本操作
创建:
这里,为了简单,我们还是以之前拉取的docker.io/hello-world
镜像为例来进行说明。
创建容器:
docker create --name hello docker.io/hello-world
查看:
查看容器的指令为:
docker ps
此时我们查看到的容器是已经启动的容器,如果我们想要查看包括未启动在内的容器,此时应该执行下面指令:
docker ps -a
查看的结果如下:
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3453f05ee7aa docker.io/hello-world "/hello" 2 minutes ago Exited (0) About a minute ago hello
启动:
注意在Docker中id等于name,都是具有唯一性的,只要是唯一,便可以用于操作相应的组件,因而我们启动容器的指令可以为:
docker start hello
容器运行后,显示的结果为:
[root@localhost ~]# docker start hello
hello
启动并查看日志:
当然,上面的那种方式看着不太明显,我们也可以在运行时打印相关的运行日志,从这里我们可以看得更直接。
启动并查看日志的相关指令为:
docker start hello && docker logs -f hello
此时显示的结果为:
[root@localhost ~]# docker start hello && docker logs -f hello
hello
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.
...
我们也可以换用id,即可以使用:
docker start 3453f05ee7aa
或者干脆就是id的简写 :
docker start 3453
创建 + 启动:
如果我们不想使用麻烦的先创建,后使用,那么我们可以使用docker的run
指令,该指令可以实现容器的创建与运行,当然,如果容器所依赖的镜像不存在的话,其也会自动下载相应的镜像,因而这是个非常方便的使用指令。
如下创建并运行指令:
docker run --name hello2 docker.io/hello-world
运行的结果:
[root@localhost ~]# docker run --name hello2 docker.io/hello-world
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.
...
停止:
docker stop hello
删除:
删除未使用的容器:
docker rm hello
强制删除使用中的容器:
docker rm -f hello
后台运行:
docker run -dit busybox
参数说明:
-d: 后台运行容器,并返回容器ID;
-i: 以交互模式运行容器,通常与 -t 同时使用;
-t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用;
通过上述指令,我们的容器就可以在后台运行了。
登陆容器:
首先是查看我们现在正在运行的容器:
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
74849623ffd3 busybox "sh" 3 minutes ago Up 3 minutes busy2
faa871023d4e busybox "sh" 6 minutes ago Up 6 minutes happy_agnesi
然后执行登陆容器happy_agnesi
的操作:
docker exec -it happy_agnesi /bin/sh
当然其它的大多数容器,可以使用/bin/bash
来替换/bin/sh
退出容器:
Ctrl + D
仓库
仓库简介
仓库(Repository)是集中存放镜像的地方。
一个容易与之混淆的概念是注册服务器(Registry)。实际上注册服务器是存放仓库的具体服务器,每个服务器上可以有多个仓库,而每个仓库下面有多个镜像。从这方面来说,仓库可以被认为是一个具体的项目或目录。
仓库的基本操作
比如说从仓库中拉取镜像:
docker pull docker.io/mysql
镜像的推送:
docker push 仓库地址/镜像名/tag
由于关于Docker的文章,我在很早前就写过,因而这里就不再一一编写说明,不清楚的可以去看之前的文章: