Medusar's Blog
敬畏知识,谦逊前行
Toggle navigation
Medusar's Blog
主页
Booklist
Resources
About Me
归档
标签
Docker 使用入门
docker
容器
2016-03-12 18:18:43
428
0
0
medusar
docker
容器
## 安装Docker 在Linux系统上,可以通过两种方式。 ### 方式一:安装docker-engine 参考这里:https://docs.docker.com/engine/installation/ ### 方式二:直接通过curl安装 ``` bash curl -fsSL https://get.docker.com/ | sh ``` curl如果没有安装,则需要先安装curl 这种方式安装成功之后,会自动启动docker ## 运行Docker docker启动之后,可以通过`docker run`命令来运行镜像。 ### 运行镜像 ### Hello world ``` bash docker run hello-world ``` 运行举例: ``` bash root@mine:~# docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 03f4658f8b78: Already exists a3ed95caeb02: Already exists Digest: sha256:8be990ef2aeb16dbcb9271ddfe2610fa6658d13f6dfb8bc72074cc1ca36966a7 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. 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 Hub account: https://hub.docker.com For more examples and ideas, visit: https://docs.docker.com/userguide/ ``` 上面的hello-world是官方给的例子,hello-world启动后输出了一些信息,下面的部分说明了我们执行`docker run hello-world`之后的过程: > 1. The Docker client contacted the Docker daemon. > 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. > 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的hub中查找你所需要的镜像。hub地址:https://hub.docker.com 我们在仓库里搜索一个“whalesay”,可以看到类似下面的结果:  列表中图标后面的就是docker镜像的名字,一般格式是`用户名/镜像名` 我们选择docker官方的例子来运行 ``` bash root@medusar:~# docker run docker/whalesay cowsay hello,i am medusar Unable to find image 'docker/whalesay:latest' locally latest: Pulling from docker/whalesay e190868d63f8: Already exists 909cd34c6fd7: Already exists 0b9bfabab7c1: Already exists a3ed95caeb02: Already exists 00bf65475aba: Already exists c57b6bcc83e3: Already exists 8978f6879e2f: Already exists 8eed3712d2cf: Already exists Digest: sha256:178598e51a26abbc958b8a2e48825c90bc22e641de3d31e18aaf55f3258ba93b Status: Downloaded newer image for docker/whalesay:latest ____________________ < hello,i am medusar > -------------------- \ \ \ ## . ## ## ## == ## ## ## ## === /""""""""""""""""___/ === ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ / ===- ~~~ \______ o __/ \ \ __/ \____\______/ ``` 我们运行hello-world的时候,命令是`docker run hello-world`,而运行这个whalesay的时候,命令却成了`docker run docker/whalesay cowsay hello,i am medusar`,这是因为whalesay运行的时候需要提供一些参数,比如`cowsay hello, i am medusar`就是它的参数。其实这个例子的功能就是允许用户自定义图片中鲸鱼说的话,我们可以把参数改一下看看: ``` bash root@medusar:~# docker run docker/whalesay cowsay 哈哈哈哈哈 _________________ < 哈哈哈哈哈 > ----------------- \ \ \ ## . ## ## ## == ## ## ## ## === /""""""""""""""""___/ === ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ / ===- ~~~ \______ o __/ \ \ __/ \____\______/ ``` 有人可能会问了,那我怎么知道一个镜像该如何运行呢,有没有参数我怎么知道呢?这你不需要担心,当你从仓库中找到一个镜像之后,大部分都会告诉你执行方法。 ### 运行本地镜像 如果镜像是第一次在本地执行,那么docker会从仓库中下载镜像然后再执行,如果已经下载过了,就不需要下载,就会立即执行,所以如果我们再次执行hello-world,就会很快,上面的whalesay的例子,第二次执行的时候,也没有下载过程,而是直接输出结果,就是因为本地已经有了该镜像。 ``` bash root@mine:~# docker run docker/whalesay cowsay 哈哈哈哈哈 _________________ < 哈哈哈哈哈 > ----------------- \ \ \ ## . ## ## ## == ## ## ## ## === /""""""""""""""""___/ === ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ / ===- ~~~ \______ o __/ \ \ __/ \____\______/ ``` 我们可以通过`docker images`命令查看本地镜像. ``` bash root@mine:~# docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest 690ed74de00f 5 months ago 960 B docker/whalesay latest 6b362a9f73eb 9 months ago 247 MB ``` repository:镜像仓库ign成 TAG:标签,类似于版本号 ImageID:镜像ID,每个镜像都有一个唯一的ID SIZE:镜像大小 我们可以通过imageid删除本地镜像,删除镜像命令: ` docker rmi -f ${imageID}` 也可以通过镜像名称来删除: ` docker rmi -f ${imageName}` 我们删除docker/whalesay镜像: ``` bash root@mine:~# docker rmi -f 6b362a9f73eb Untagged: docker/whalesay:latest Deleted: sha256:6b362a9f73eb8c33b48c95f4fcce1b6637fc25646728cf7fb0679b2da273c3f4 root@mine:~# docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest 690ed74de00f 5 months ago 960 B ``` ## 自己创建镜像并发布到仓库 首先我们需要在dockerhub上创建自己的账号,然后我们在本地创建镜像,然后把镜像发布到仓库中。 ### 创建本地镜像 1. 创建一个文件夹,作为工作空间 2. 创建Dockerfile文件 3. 将命令写入Dockfile文件 我们把下面的命令写入到Dockerfile文件中: ``` vim FROM docker/whalesay:latest RUN apt-get -y update && apt-get install -y fortunes CMD /usr/games/fortune -a | cowsay ``` `FROM docker/whalesay:latest` 表示我们的镜像是基于`docker/whalesay`的lastest版本的,类似于继承关系。 `RUN apt-get -y update && apt-get install -y fortunes` 这一步是关键的,我们的程序需要安装一个fortunes应用,这行的作用就是把fortune应用安装到镜像中 `CMD /usr/games/fortune -a | cowsay` 这行的作用就是执行fortune程序,然后把结果作为cowsay的参数。(cowsay是docker/whalesay中的命令,它接收一个参数作为蓝鲸的输出)。 说明:fortune是一个可以输出一些名人名言或者谚语的应用 保存文件,然后把该文件build成镜像: 在当前目录下执行`docker build -t docker-whale .`命令,需要注意命令最后的点号(.),该过程比较长,执行完成之后我们的镜像就生成了。镜像名字是docker-whale。完成之后,通过`docker images`查看一下: ``` bash root@mine:~/mydockerbuild# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker-whale latest b35cb5114c0a 4 minutes ago 274.4 MB hello-world latest 690ed74de00f 5 months ago 960 B docker/whalesay latest 6b362a9f73eb 9 months ago 247 MB ``` 发现我们自己创建的镜像已经存在了。 ### 发布镜像 经过上面的步骤,我们已经把镜像创建在本地了,接下来我们需要把它发布到仓库中,以便在任意机器上执行。 首先我们需要登录到hub中,创建一个新的Repositor,然后填写镜像信息:  点击创建即可。 创建完仓库之后,我们需要给我们的镜像打Tag,我们把我们的镜像命名改一下,加上我们的用户名。 ``` bash docker tag d7ec665fbc09 medusar/docker-whale:latest ``` 格式:docker tag ${imageID} ${name}:${tag}。 打完标签我们再看一下镜像: ``` bash root@mine:~/mydockerbuild# docker tag d7ec665fbc09 medusar/docker-whale:latest root@mine:~/mydockerbuild# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker-whale latest d7ec665fbc09 6 minutes ago 274.4 MB medusar/docker-whale latest d7ec665fbc09 6 minutes ago 274.4 MB hello-world latest 690ed74de00f 5 months ago 960 B docker/whalesay latest 6b362a9f73eb 9 months ago 247 MB ``` 打完标签,我们就可以发布了,我们首先要登录我们的账号: ``` bash docker login --username=yourhubusername --email=youremail@company.com ``` 登陆成功会有提示。 登录之后,我们就可以提交了,使用`docker push `命令: ``` bash docker push medusar/docker-whale ``` 提交成功之后,我们先把本地镜像删除,然后通过`docker run`命令来获取我们仓库中的镜像并执行。 ## 参考资料 1. [docker Linux入门](https://docs.docker.com/linux/)
上一篇:
简单工厂,工厂方法,抽象工厂模式的区别
下一篇:
Redis集群学习
0
赞
428 人读过
新浪微博
微信
腾讯微博
QQ空间
人人网
Please enable JavaScript to view the
comments powered by Disqus.
comments powered by
Disqus
文档导航