首页 > 代码库 > 01-docker 初体验

01-docker 初体验

下载docker:yum install docker

查看本地docker镜像 : docker images

下载docker镜像:官网的镜像拉不下来,可以尝试在网易docker蜂巢中查找

docker pull hub.c.163.com/public/centos:7.2-tools

再次查看本地镜像

[root@carlcompute carl]# docker images
REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
hub.c.163.com/library/centos   latest              a8493f5f50ff        12 weeks ago        192.5 MB
hub.c.163.com/public/centos    7.2-tools           4a4618db62b9        4 months ago        515.4 MB

在容器内运行一个应用程序,输出 Hello World, I‘m  Carl.

[root@carlcompute carl]# docker run hub.c.163.com/public/centos:7.2-tools /bin/echo "Hello World,I‘m carl."
Hello World,Im carl.

通过 -i -t 参数实现互动

[root@carlcompute carl]# docker run -i -t hub.c.163.com/public/centos:7.2-tools  /bin/bash
[root@acf7f67194c7 /]# who   
[root@acf7f67194c7 /]# ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=43 time=288 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=43 time=295 ms
^C
--- 8.8.8.8 ping statistics ---
3 packets transmitted, 2 received, 33% packet loss, time 2002ms
rtt min/avg/max/mdev = 288.350/292.060/295.771/3.749 ms
[root@acf7f67194c7 /]# ls  
anaconda-post.log  bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@acf7f67194c7 /]# cat /proc/version 
Linux version 3.10.0-514.16.1.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC) ) #1 SMP Wed Apr 12 15:04:24 UTC 2017

exit 退出该容器

启动容器,在后台工作,生成的字符串是容器ID,唯一表示,docker ps 可查看当前运行的容器

[root@carlcompute carl]# docker run -d hub.c.163.com/public/centos:7.2-tools  /bin/bash -c "while true; do echo hello world ; sleep 1 ;done"
1f54cb708713ff38d373eed259cbce33a3830217551016294296d951e1e30614
[root@carlcompute carl]# docker ps
CONTAINER ID        IMAGE                                   COMMAND                  CREATED             STATUS              PORTS               NAMES
1f54cb708713        hub.c.163.com/public/centos:7.2-tools   "/bin/bash -c ‘while "   12 seconds ago      Up 10 seconds       22/tcp              romantic_mcclintock

docker logs 容器ID或者容器NAME     可查看容器内的标准输出

docker stop 容器ID   可以停止该容器

[root@carlcompute carl]# docker stop 1f54cb708713
1f54cb708713
[root@carlcompute carl]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

docker 容器的使用

 在docker容器中运行一个Python Flask应用来运行一个web应用,使用不在本地存储的镜像会自动从网上拉取 -P将容器内部使用的网络映射到主机上

[root@carlcompute carl]# docker run -d -P training/webapp python app.py
Unable to find image training/webapp:latest locally
Trying to pull repository docker.io/training/webapp ... 
latest: Pulling from docker.io/training/webapp

e190868d63f8: Pull complete 
909cd34c6fd7: Pull complete 
0b9bfabab7c1: Pull complete 
a3ed95caeb02: Pull complete 
10bbbc0fc0ff: Pull complete 
fca59b508e9f: Pull complete 
e7ae2541b15b: Pull complete 
9dd97ef58ce9: Pull complete 
a4c1b0cb7af7: Pull complete 
Digest: sha256:06e9c1983bd6d5db5fba376ccd63bfa529e8d02f23d5079b8f74a616308fb11d
483a15700820260842a289868d72803dd24ee66460aaa07123b5aca2334c43fb
[root@carlcompute carl]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS                    NAMES
483a15700820        training/webapp     "python app.py"     About a minute ago   Up About a minute   0.0.0.0:1024->5000/tcp   furious_ramanujan

容器内部的5000端口映射到主机1024,访问主机IP:1024

 技术分享

也可以自己指定端口

[root@carlcompute carl]# docker run -d -p 5000:5000 training/webapp python app.py
f4f3a74531cb0b6670aab422063be30fbd954f7e1dd8b03fbbc4670d7e0db147
[root@carlcompute carl]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
f4f3a74531cb        training/webapp     "python app.py"     7 seconds ago       Up 4 seconds        0.0.0.0:5000->5000/tcp   jovial_perlman
483a15700820        training/webapp     "python app.py"     6 minutes ago       Up 6 minutes        0.0.0.0:1024->5000/tcp   furious_ramanujan

查看容器端口映射到主机的端口

[root@carlcompute carl]# docker port f4f3a74531cb
5000/tcp -> 0.0.0.0:5000

查看web应用程序日志

[root@carlcompute carl]# docker logs -f f4f3a74531cb
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
^C
[root@carlcompute carl]# docker logs -f 483a15700820
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
192.168.88.125 - - [04/Jul/2017 02:33:51] "GET / HTTP/1.1" 200 -
192.168.88.125 - - [04/Jul/2017 02:33:51] "GET /favicon.ico HTTP/1.1" 404 -
192.168.88.125 - - [04/Jul/2017 02:33:51] "GET /favicon.ico HTTP/1.1" 404 -
^C

查看web应用程序内部的进程

[root@carlcompute carl]# docker top 483a15700820
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                31736               31721               0                   10:30               ?                   00:00:00            python app.py

停止,删除容器  docker stop  容器ID或NAME

[root@carlcompute carl]# docker stop 483a15700820
483a15700820
[root@carlcompute carl]# docker rm 483a15700820
483a15700820

Docker 镜像的使用

列出镜像列表: docker images

获取一个新的镜像 : docker pull  镜像

查找镜像: docker search 镜像

创建镜像:  1.从已经创建好的镜像中跟新镜像,并且提交镜像

        2.使用Dockerfile指令来创建一个新的镜像

更新镜像前我们要使用镜像创建一个容器

[root@carlcompute carl]# docker run -i -t hub.c.163.com/public/centos:7.2-tools /bin/bash

进入交互模式,可以尝试下载apache 后 exit 推出, docker ps -l 查看上一个运行的容器,docker ps -a 查看所有运行过的容器,找出刚才的容器ID

[root@carlcompute carl]# docker commit -m="add httpd" -a="carl" 256e290dfb6c carltest:v1
sha256:f1bd7ad30914a8f3f8552419166016b5b4f49055f671f627784c2d534ca6ea0c
[root@carlcompute carl]# docker images
REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
carltest                       v1                  f1bd7ad30914        6 seconds ago       648.2 MB
hub.c.163.com/library/centos   latest              a8493f5f50ff        12 weeks ago        192.5 MB
hub.c.163.com/public/centos    7.2-tools           4a4618db62b9        4 months ago        515.4 MB
docker.io/training/webapp      latest              6fae60ef3446        2 years ago         348.7 MB

其中,-m 提交的描述信息,-a 作者name ,256e290dfb6c容器模板ID, carltest:v1 新镜像的名称,创建后docker images 查看镜像。

 

通过dockerfile文件来创建新的镜像:

[root@carlcompute ~]# cat dockerfile-useradd 
FROM    hub.c.163.com/public/centos:7.2-tools
MAINTAINER    carl "121512723@qq.com"

RUN     useradd carl
RUN    /bin/echo 123456 | passwd --stdin carl

EXPOSE    22
EXPOSE    80
CMD    /usr/sbin/sshd -D
[root@carlcompute ~]# docker build -t carltest:v2  - <  dockerfile-useradd
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM hub.c.163.com/public/centos:7.2-tools
 ---> 4a4618db62b9
Step 2 : MAINTAINER carl "121512723@qq.com"
 ---> Running in 659401d02fda
 ---> d1a5aaea3950
Removing intermediate container 659401d02fda
Step 3 : RUN useradd carl
 ---> Running in d97735b3bf9e
 ---> 6005d0501b1c
Removing intermediate container d97735b3bf9e
Step 4 : RUN /bin/echo 123456 | passwd --stdin carl
 ---> Running in d0e0bbc1efb2
Changing password for user carl.
passwd: all authentication tokens updated successfully.
 ---> 38362b52f8d9
Removing intermediate container d0e0bbc1efb2
Step 5 : EXPOSE 22
 ---> Running in c3166729230e
 ---> bcee33c2b054
Removing intermediate container c3166729230e
Step 6 : EXPOSE 80
 ---> Running in 7fbf01afea27
 ---> 6400121f202b
Removing intermediate container 7fbf01afea27
Step 7 : CMD /usr/sbin/sshd -D
 ---> Running in 3b16eee575bb
 ---> 0adfeecf8ec8
Removing intermediate container 3b16eee575bb
Successfully built 0adfeecf8ec8
[root@carlcompute ~]# docker images
REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
carltest                       v2                  0adfeecf8ec8        34 seconds ago      515.4 MB
carltest                       v1                  f1bd7ad30914        22 minutes ago      648.2 MB
hub.c.163.com/library/centos   latest              a8493f5f50ff        12 weeks ago        192.5 MB
hub.c.163.com/public/centos    7.2-tools           4a4618db62b9        4 months ago        515.4 MB
docker.io/training/webapp      latest              6fae60ef3446        2 years ago         348.7 MB
[root@carlcompute ~]# docker run -i -t carltest:v2  /bin/bash
[root@6bd7c60b90de /]# id -u carl
1000

为镜像添加一个新的TAG

[root@carlcompute ~]# docker images
REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
carltest                       v2                  0adfeecf8ec8        5 minutes ago       515.4 MB
carltest                       v3                  0adfeecf8ec8        5 minutes ago       515.4 MB
carltest                       v1                  f1bd7ad30914        27 minutes ago      648.2 MB
hub.c.163.com/library/centos   latest              a8493f5f50ff        12 weeks ago        192.5 MB
hub.c.163.com/public/centos    7.2-tools           4a4618db62b9        4 months ago        515.4 MB
docker.io/training/webapp      latest              6fae60ef3446        2 years ago         348.7 MB

Docker 容器的连接

docker run -d -P training/webapp python app.py
docker run -d -p 5000:5000 training/webapp python app.py

-P是容器的端口随机到主机的高端口

-p是容器内部端口绑定到主机端口

也可以绑定网络地址

docker run -d -p 127.0.0.1:5001:5002 training/webapp python app.py

可以通过访问127.0.0.1:5001 来访问容器的5002端口

默认绑定的TCP,如果要绑定在UDP,可在端口后面加上/udp

docker run -d -p 127.0.0.1:5000:5000/udp training/webapp python app.py

容器命名

docker run -d -P --name carl training/webapp python app.py

 

01-docker 初体验