开始docker

Posted Aiapple

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了开始docker相关的知识,希望对你有一定的参考价值。

安装

 
docker 目前只支持64位系统
1.下载并安装,简直太方便了
$ curl -fsSL https://get.docker.com/ | sh
用例

 
1.docker run hello-world
$ docker run hello-world
Cannot connect to the Docker daemon. Is the docker daemon running on this host?
需要创建一个用户到docker 组中。
$ sudo usermod -aG docker $(whoami)
$ sudo reboot

注意:需要reboot之后才能使用

 
成功运行
zane@zane-V:~$ sudo docker run 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.
 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/engine/userguide/

 
Docker完成hello-world做的动作
  1. Docker客户端联系Docker用例
  2. Docker用例从Docker Hub 中将“hello-world”image 拉下来。
  3. Docker 用例从“hello-world” image 创建一个新的容器。而这个image运行着产生你现在读到的信息的 可执行性文件。
  4. Docker用例再讲这些输出传送到Docker客户端,然后Docker客户端在将这些信息传送到你的屏幕
 
2.docker ps -a 显示系统中所有的容器
zane@zane-V:~$  docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
767a0a0f0bae        hello-world         "/hello"            30 minutes ago      Exited (0) 30 minutes ago                       silly_mcnulty
9e6a3cb1e18b        hello-world         "/hello"            33 minutes ago      Exited (0) 33 minutes ago                       tender_kare
 
zane@zane-V:~$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
  • docker ps 仅显示当前正在运行的容器
    • hello-world已经退出因此不会显示
 
Images and Containers

 
Docker 引擎提供的核心技术就是启用images和containers。
 
比如:docker run hello-world命令有以下三个部分:

 

  • docker
    • 告诉操作系统正在使用docker程序
  • hello-world
    • 告诉docker将hello-world这个image加载到容器
  • run
    • 一个创建运行docker容器的子命令
 
 
Image
  • 是一个文件系统和参数集合,用在运行时。
  • 没有状态也不可以改变
 
Contaniers
  • 一个正运行的image实例
 
 
当执行docker run hello-world,Docker引擎:
  • 检查你是否有hello-world 这个镜像
  • 从Docker Hub下载这个image
  • 加载这个image到container然后运行
 
取决于image的创建,可能是运行简单,单个命令然后退出,就像 hello-world image一样。
当然也可以是像数据库一样复杂的软件,等待存数据,等待下一位使用者等等。
 
Docker引擎可以通过Docker image创建和分享软件
 
寻找运行image

 
可以在Docker Hub中找到全世界所有人创建的image,当然是在Docker Hub中创建的拉。
 
假设找到一个感兴趣的image docker/whalesay,然后直接用就可以啦。
 
第一次运行image时,docker在本地寻找,如果本地没有则到Docker Hub中找。
zane@zane-V:~$  docker run docker/whalesay cowsay boo
Unable to find image \'docker/whalesay:latest\' locally
latest: Pulling from docker/whalesay
 
e190868d63f8: Pull complete
909cd34c6fd7: Pull complete
0b9bfabab7c1: Pull complete
a3ed95caeb02: Pull complete
00bf65475aba: Pull complete
c57b6bcc83e3: Pull complete
8978f6879e2f: Pull complete
8eed3712d2cf: Pull complete
Digest: sha256:178598e51a26abbc958b8a2e48825c90bc22e641de3d31e18aaf55f3258ba93b
Status: Downloaded newer image for docker/whalesay:latest
 _____
< boo >
 -----
    \\
     \\
      \\     
                    ##        .           
              ## ## ##       ==           
           ## ## ## ##      ===           
       /""""""""""""""""___/ ===       
  ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===- ~~~   
       \\______ o          __/           
        \\    \\        __/             
          \\____\\______/ 

 

 
创建image

 
通过创建"talks on its own"这样一个新的版本,来提升改进whalesay image。
 
步骤一:编写Dockerfile
Dockerfile 是在创建image时,描述 文件,环境,命令的。
 
1.创建新目录:用与创建image所需的所有文件
 
创建目录并进入目录
$ mkdir mydockerbuild
 
$ cd mydockerbuild
 
 
2.编写Dockerfile
 
编写并增加内容
$ vim Dockerfile
 
a.指定基础image
增加:
FROM docker/whalesay:latest
FROM这个关键字告诉Docker,你是以哪个image作为基础的。
 
b.安装fortunes程序到image中
RUN apt-get -y update && apt-get install -y fortunes
fortunes程序会打印一些聪明的话语来自whale。
 
c.增加CMD行
CMD /usr/games/fortune -a | cowsay
CMD 行告诉image在环境安装好后最后运行的命令。
这里是fortune -a 发送它的输出到 cowsay.
 
3.保存,并检查Dockerfile是这样子的
FROM docker/whalesay:latest
RUN apt-get -y update && apt-get install -y fortunes
CMD /usr/games/fortune -a | cowsay
 
 
步骤二: 从Dockerfile创建image
使用docker build 创建image。
-t 参数给image一个标签,在之后更容易的运行它。
不要忘记 . 命令,它告诉docker build 在本目录寻找调用Dockerfile。
 
zane@zane-V:~/mydockerbuild$ docker build -t docker-whale .

 

步骤三:学习创建image的过程
docker build -t docker-whale . 命令从Dockerfile一步步读取说明,然后再本地机器中创建docker-whale这个image。
创建过程中输出信息的说明: 
1.Docker 检查并确认创建所需的所有东西都已经有了,会产生以下信息:
Sending build context to Docker daemon 2.048 kB
 
2.Docker 检查并且确定 whalesay image 是否在本地已经存在,如果不存在则从Docker hub 上拉下来。
   在Dockerfile中对应的行,以及产生的信息:
Step 1 : FROM docker/whalesay:latest
---> 6b362a9f73eb
在每一步的最后,ID会被打印出来。这是此步骤创建的ID图层。在Dockerfile的每一行在image中对应图层,你的ID是不一样的。
 
3.Docker启动零时容器运行whalesay image。在这个零时容器中,Docker运行Dockerfile文件中下一个命令,RUN。
   当RUN命令结束,一个新层被创建,零时容器被删除。
 
zane@zane-V:~/mydockerbuild$ docker build -t docker-whale .
Sending build context to Docker daemon 14.85 kB
Step 1 : FROM docker/whalesay:latest
 ---> 6b362a9f73eb
Step 2 : RUN apt-get -y update && apt-get install -y fortunes
 ---> Running in e9dbb3ff0ba2
Ign http://archive.ubuntu.com trusty InRelease
Get:1 http://archive.ubuntu.com trusty-updates InRelease [65.9 kB]
.......
.......
.......
Setting up fortunes-min (1:1.99.1-7) ...
Setting up fortunes (1:1.99.1-7) ...
Processing triggers for libc-bin (2.19-0ubuntu6.6) ...
 ---> 4c9a56721bad
Removing intermediate container e9dbb3ff0ba2
4.创建一个新中间容器,为CMD行增加另一个层。然后删除中间容器
Step 3 : CMD /usr/games/fortune -a | cowsay
 ---> Running in b1aa7efec08e
 ---> d09756981eeb
Removing intermediate container b1aa7efec08e
Successfully built d09756981eeb
现在已经创建了一个叫做docker-whale的image。
步骤四: 运行你的docker-whale
 
1.查看image是否已经建立
zane@zane-V:~/mydockerbuild$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker-whale        latest              d09756981eeb        19 minutes ago      275.1 MB
hello-world         latest              c54a2cc56cbb        5 months ago        1.848 kB
docker/whalesay     latest              6b362a9f73eb        19 months ago       247 MB
 

 

2.运行
zane@zane-V:~/mydockerbuild$ docker run docker-whale
 ____________________________________
/ This is National Non-Dairy Creamer \\
\\ Week.                              /
 ------------------------------------
    \\
     \\
      \\     
                    ##        .           
              ## ## ##       ==           
           ## ## ## ##      ===           
       /""""""""""""""""___/ ===       
  ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===- ~~~   
       \\______ o          __/           
        \\    \\        __/             
          \\____\\______/
 
zane@zane-V:~/mydockerbuild$ docker run docker-whale
 _______________________________________
/ People who take cold baths never have \\
\\ rheumatism, but they have cold baths. /
 ---------------------------------------
    \\
     \\
      \\     
                    ##        .           
              ## ## ##       ==           
           ## ## ## ##      ===           
       /""""""""""""""""___/ ===       
  ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===- ~~~   
       \\______ o          __/           
        \\    \\        __/             
          \\____\\______/ 
 
看上去,whale可以根据自己的想法说点什么了。
 
创建Docker Hub 账号和存储库

 
 
2.在页面中选择 Create Repository
 
步骤一:  选择本地image,并推送到刚刚建立的 Repository
a.查看本地image并选择id号
zane@zane-V:~/mydockerbuild$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker-whale        latest              d09756981eeb        32 minutes ago      275.1 MB
hello-world         latest              c54a2cc56cbb        5 months ago        1.848 kB
docker/whalesay     latest              6b362a9f73eb        19 months ago       247 MB
b.使用IMAGE ID 和 docker tag 命令,标记你自己的docker-whale image.

 

zane@zane-V:~/mydockerbuild$ docker tag d09756981eeb zane0306/docker-whale:latest

 

c.查看你刚刚标记过的image
zane@zane-V:~/mydockerbuild$ docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
docker-whale            latest              d09756981eeb        37 minutes ago      275.1 MB
zane0306/docker-whale   latest              d09756981eeb        37 minutes ago      275.1 MB
hello-world             latest              c54a2cc56cbb        5 months ago        1.848 kB
docker/whalesay         latest              6b362a9f73eb        19 months ago       247 MB

 

d.命令行登录Dcoker Hub。
zane@zane-V:~/mydockerbuild$ docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don\'t have a Docker ID, head over to https://hub.docker.com to create one.
Username: zane0306
Password:
Login Succeeded
 
e.docker push 命令推送自己image到存储库
zane@zane-V:~/mydockerbuild$ docker push zane0306/docker-whale
The push refers to a repository [docker.io/zane0306/docker-whale]
9748ff4991ff: Pushed
5f70bf18a086: Mounted from docker/whalesay
d061ee1340ec: Mounted from docker/whalesay
d511ed9e12e1: Mounted from docker/whalesay
091abc5148e4: Mounted from docker/whalesay
b26122d57afa: Mounted from docker/whalesay
37ee47034d9b: Mounted from docker/whalesay
528c8710fd95: Mounted from docker/whalesay
1154ba695078: Mounted from docker/whalesay
latest: digest: sha256:62b528c43afd3a2771a167a21ce005a5ee49514109d2af870336f6880ec4eca7 size: 2614
步骤二:  从远程库中拉下image
 
a.从本地删除原有的image
zane@zane-V:~/mydockerbuild$ docker images;
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
docker-whale            latest              d09756981eeb        49 minutes ago      275.1 MB
zane0306/docker-whale   latest              d09756981eeb        49 minutes ago      275.1 MB
hello-world             latest              c54a2cc56cbb        5 months ago        1.848 kB
docker/whalesay         latest              6b362a9f73eb        19 months ago       247 MB
 
zane@zane-V:~/mydockerbuild$ docker rmi -f d09756981eeb
Untagged: docker-whale:latest
Untagged: zane0306/docker-whale:latest
Untagged: zane0306/docker-whale@sha256:62b528c43afd3a2771a167a21ce005a5ee49514109d2af870336f6880ec4eca7
Deleted: sha256:d09756981eebc2a3fa7d200e57be74e89147465969aa92dcbd34b3f541a90219
Deleted: sha256:4c9a56721bad2895ec49a1683737d132f13f2ca99627c5ae3b4368f1b2583919
 
zane@zane-V:~/mydockerbuild$ docker images;
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              c54a2cc56cbb        5 months ago        1.848 kB
docker/whalesay     latest              6b362a9f73eb        19 months ago       247 MB
b.从远程拉取,并加载image
 
zane@zane-V:~/mydockerbuild$ docker run zane0306/docker-whale
Unable to find image \'zane0306/docker-whale:latest\' locally
latest: Pulling from zane0306/docker-whale
 
e190868d63f8: Already exists
909cd34c6fd7: Already exists
0b9bfabab7c1: Already exists
a3ed95caeb02: Already exists
00bf65475aba: Already exists
c57b6bcc83e3: Already exists
8978f6879e2f: Already exists
8eed3712d2cf: Already exists
c7b22951dde1: Already exists
Digest: sha256:62b528c43afd3a2771a167a21ce005a5ee49514109d2af870336f6880ec4eca7
Status: Downloaded newer image for zane0306/docker-whale:latest
 _________________________________________
/ As usual, this being a 1.3.x release, I \\
| haven\'t even compiled this kernel yet.  |
| So if it works, you should be doubly    |
| impressed. (Linus Torvalds, announcing  |
| kernel 1.3.3 on the linux-kernel        |
\\ mailing list.)                          /
 -----------------------------------------
    \\
     \\
      \\     
                    ##        .           
              ## ## ##       ==           
           ## ## ## ##      ===           
       /""""""""""""""""___/ ===       
  ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===- ~~~   
       \\______ o          __/           
        \\    \\        __/             
          \\____\\______/   

zane@zane-V:~/mydockerbuild$ docker run zane0306/docker-whale
 _______________________________________
/ Klingon function calls do not have    \\
| \'parameters\' -- they have \'arguments\' |
|                                       |
\\ -- and they ALWAYS WIN THEM.          /
 ---------------------------------------
    \\
     \\
      \\     
                    ##        .           
              ## ## ##       ==           
           ## ## ## ##      ===           
       /""""""""""""""""___/ ===       
  ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===- ~~~   
       \\______ o          __/           
        \\    \\        __/             
          \\____\\______/  
 
 
总结

    1. Docker Hub 与本地image的push,pull
      • 在Docker Hub上寻找自己感兴趣的image
    2. image 和 contaniers
      • image
        • image 是一个文件系统+参数集合。
        • 没有状态也不可以改变
      • contaniers
        • 一个正在运行的image实例
    3. 利用Dockerfile 创建自己的image
      • 编写Dockerfile
        • a.指定基础image
          • FROM docker/whalesay:latest
        • b.安装fortunes程序到image中
          • RUN apt-get -y update && apt-get install -y fortunes
        • c.增加CMD行
          • CMD /usr/games/fortune -a | cowsay
      • build image
        • zane@zane-V:~/mydockerbuild$ docker build -t docker-whale .
        • -t 参数给image一个标签,在之后更容易的运行它。

以上是关于开始docker的主要内容,如果未能解决你的问题,请参考以下文章

markdown [Docker] Docker片段列表和命令#linux #docker #snippets

无法从 onListItemClick 开始片段

后按时从 onCreate() 开始片段

从零开始配置vim(27)——代码片段

markdown docker-compose片段

sh Docker片段