CentOS构建Docker

admin 发布于:2019-1-25 10:08 分类:系统架构  有 1834 人浏览,获得评论 0 条 标签: Docker 

1.安装
[root@localhost test]# yum install docker

2.启动docker
[root@localhost test]# systemctl start docker

3.运行hello-world
[root@localhost test]# 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.
    (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.

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 ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/


4.Docker 以 ubuntu15.10 镜像创建一个新容器,然后在容器里执行 bin/echo "Hello world",然后输出结果
[root@localhost test]#docker run ubuntu:15.10 /bin/echo "Hello world"
Hello world

5.通过docker的两个参数 -i -t,让docker运行的容器实现"对话"的能力
[root@localhost test]# docker run -i -t ubuntu:15.10 /bin/bash
root@8b6cdcd4f1bb:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var


6.后台模式运行命令
[root@localhost test]# docker run -d ubuntu:15.10 /bin/sh -c "while true; do echo hello world; sleep 1; done"
1472118a21aa6d4931b108f75f2d4b3fadefe6bd2e89317245f3a9b1f286930e
[root@localhost test]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
1472118a21aa        ubuntu:15.10        "/bin/sh -c 'while..."   2 minutes ago       Up 2 minutes                            loving_montalcini
[root@localhost test]# docker logs 1472118a21aa
hello world
hello world
hello world
hello world
[root@localhost test]# docker stop 1472118a21aa
1472118a21aa
[root@localhost test]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES



7.列出镜像列表
[root@localhost test]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
docker.io/hello-world   latest              fce289e99eb9        3 weeks ago         1.84 kB
docker.io/ubuntu        15.10               9b9cb95443b5        2 years ago         137 MB

8.搜索镜像
[root@localhost test]# docker search lnmp
INDEX       NAME                                        DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/imagine10255/centos6-lnmp-php56   centos6-lnmp-php56                              50                   [OK]
docker.io   docker.io/twang2218/lnmp-nginx              这是 LNMP 示例中的 nginx 镜像                           16                   [OK]
docker.io   docker.io/winstonpro/lnmp                   based on ubuntu 14.04                           14                   
docker.io   docker.io/maxwhale/ubuntu14-lnmp1.2         Ubuntu14.04 lnmp1.2                             9                    [OK]
docker.io   docker.io/dzer/lnmp                         lnmp环境                                          8                    
docker.io   docker.io/domainer/centos-lnmp                                                              5                    [OK]
docker.io   docker.io/duckll/lnmp                       webservice                                      4                    [OK]
docker.io   docker.io/maxwhale/lnmp-docker              LNMP Docker                                     4                    [OK]
docker.io   docker.io/evagle/lnmp                       ubuntu14.04 + nginx + mysql + php + redis       3                    
docker.io   docker.io/idiswy/lnmp                       Ubuntu 16.04 + nginx 1.8.x + php7 + MySQL ...   3                    
docker.io   docker.io/c21xdx/lnmp13_cen6                php5.4                                          1                    [OK]
docker.io   docker.io/erguotou/docker-centos6-lnmp      lnmp environment in centos6                     1                    [OK]
docker.io   docker.io/fbraz3/lnmp                       An easy-to-use LNMP/LEMP image, with Ubunt...   1                    
docker.io   docker.io/gatherchou/docker-lnmp            docker-lnmp                                     1                    [OK]
docker.io   docker.io/inteye/lnmp                       LNMP1.2 (Linux, Nginx, Mysql, PHP). For de...   1                    
docker.io   docker.io/turtlell/lnmp                     first lnmp demo                                 1                    
docker.io   docker.io/twang2218/lnmp-php                这是 LNMP Docker 容器互联示例的 php 镜像                   1                    [OK]
docker.io   docker.io/zhaojianhui/lnmp                  LNMP环境                                          1                    
docker.io   docker.io/dahaitech/lnmp-douyou             douyou test lnmp                                0                    [OK]
docker.io   docker.io/huangguoji/lnmp                   lnmp.org的包                                      0                    
docker.io   docker.io/jamesatwork/docker-lnmpwp         A docker of LNMP & WordPress                    0                    [OK]
docker.io   docker.io/sixian67/lnmp                     this is my lnmp                                 0                    
docker.io   docker.io/thinksvip/lnmp                    LNMP docker production environment              0                    [OK]
docker.io   docker.io/twang2218/lnmp-mysql              This is the MySQL image of the LNMP docker...   0                    [OK]
docker.io   docker.io/zshtom/lnmp                       lnmp in centos 7                                0                    


9.下载镜像
[root@localhost test]# docker pull docker.io/idiswy/lnmp
Using default tag: latest
Trying to pull repository docker.io/idiswy/lnmp ... 
latest: Pulling from docker.io/idiswy/lnmp
f069f1d21059: Pull complete 
ecbeec5633cf: Pull complete 
ea6f18256d63: Pull complete 
54bde7b02897: Pull complete 
a3ed95caeb02: Pull complete 
ce9e695a6234: Pull complete 
346026b9659b: Pull complete 
929099c3258a: Pull complete 
064503d36445: Pull complete 
4c1032a6f902: Pull complete 
882f4453d31b: Pull complete 
Digest: sha256:ddb256e6834e5bd0dbaa0d9130ff94312ecdb1ae19669cc747e99620c4edd656
Status: Downloaded newer image for docker.io/idiswy/lnmp:latest

[root@localhost test]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
docker.io/hello-world   latest              fce289e99eb9        3 weeks ago         1.84 kB
docker.io/idiswy/lnmp   latest              fbd7b7c5428e        2 years ago         335 MB
docker.io/ubuntu        15.10               9b9cb95443b5        2 years ago         137 MB

10.从已经创建的容器中更新镜像,并且提交这个镜像
[root@localhost test]# docker run -t -i fbd7b7c5428e /bin/bash
[root@e594c6c84c47:/sbin# apt-get update
Get:1 http://archive.ubuntu.com/ubuntu xenial InRelease [247 kB] 
Get:2 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [109 kB]                   
Get:3 http://archive.ubuntu.com/ubuntu xenial-security InRelease [109 kB]                  
Get:4 http://archive.ubuntu.com/ubuntu xenial/main Sources [1103 kB]                                                                                                                          
Get:5 http://archive.ubuntu.com/ubuntu xenial/restricted Sources [5179 B]                                                                                                                     
Get:6 http://archive.ubuntu.com/ubuntu xenial/universe Sources [9802 kB]                                                                                                                      
Get:7 http://nginx.org/packages/ubuntu xenial InRelease [4339 B]                                                                                                                              
Get:8 http://nginx.org/packages/ubuntu xenial/nginx Sources [14.1 kB]                                                                                                                         
Get:9 http://nginx.org/packages/ubuntu xenial/nginx amd64 Packages [20.1 kB]                                                                                                                  
Get:10 http://archive.ubuntu.com/ubuntu xenial/main amd64 Packages [1558 kB]                                                                                                                  
Get:11 http://archive.ubuntu.com/ubuntu xenial/restricted amd64 Packages [14.1 kB]                                                                                                            
Get:12 http://archive.ubuntu.com/ubuntu xenial/universe amd64 Packages [9827 kB]                                                                                                              
Get:13 http://archive.ubuntu.com/ubuntu xenial-updates/main Sources [415 kB]                                                                                                                  
Get:14 http://archive.ubuntu.com/ubuntu xenial-updates/restricted Sources [2684 B]                                                                                                            
Get:15 http://archive.ubuntu.com/ubuntu xenial-updates/universe Sources [303 kB]                                                                                                              
Get:16 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 Packages [1166 kB]                                                                                                          
Get:17 http://archive.ubuntu.com/ubuntu xenial-updates/restricted amd64 Packages [13.1 kB]                                                                                                    
Get:18 http://archive.ubuntu.com/ubuntu xenial-updates/universe amd64 Packages [929 kB]                                                                                                       
Get:19 http://archive.ubuntu.com/ubuntu xenial-security/main Sources [175 kB]                                                                                                                 
Get:20 http://archive.ubuntu.com/ubuntu xenial-security/restricted Sources [2243 B]                                                                                                           
Get:21 http://archive.ubuntu.com/ubuntu xenial-security/universe Sources [116 kB]                                                                                                             
Get:22 http://archive.ubuntu.com/ubuntu xenial-security/multiverse Sources [3513 B]                                                                                                           
Get:23 http://archive.ubuntu.com/ubuntu xenial-security/main amd64 Packages [767 kB]                                                                                                          
Get:24 http://archive.ubuntu.com/ubuntu xenial-security/restricted amd64 Packages [12.7 kB]                                                                                                   
Get:25 http://archive.ubuntu.com/ubuntu xenial-security/universe amd64 Packages [527 kB]                                                                                                      
Get:26 http://archive.ubuntu.com/ubuntu xenial-security/multiverse amd64 Packages [6119 B]                                                                                                    
Fetched 27.3 MB in 4min 1s (113 kB/s)                                                                                                                                                         
Reading package lists... Done


11.通过命令 docker commit来提交容器副本,创建的目标镜像名
//-m:提交的描述信息,-a:指定镜像作者,d11aaceba824:容器ID,test/lnmp:v1 :指定要创建的目标镜像名
[root@localhost test]# docker commit -m="LNMP" -a="test" d11aaceba824 test/lnmp:v1
sha256:30a7c473b0ca8e02c1d1ac0ef3188e09d12649faf60cede37e2d7ccb877331e6

[root@localhost test]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
test/lnmp               v1                  30a7c473b0ca        3 minutes ago       335 MB
docker.io/hello-world   latest              fce289e99eb9        3 weeks ago         1.84 kB
docker.io/idiswy/lnmp   latest              fbd7b7c5428e        2 years ago         335 MB
docker.io/ubuntu        15.10               9b9cb95443b5        2 years ago         137 MB


12.设置镜像标签
docker tag 镜像ID,这里是 860c279d2fec ,用户名称、镜像源名(repository name)和新的标签名(tag)。
使用 docker images 命令可以看到,ID为860c279d2fec的镜像多一个标签。

[root@localhost test]# docker tag 30a7c473b0ca test/lnmp:tagv1
[root@localhost test]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
test/lnmp               tagv1               30a7c473b0ca        15 minutes ago      335 MB
test/lnmp               v1                  30a7c473b0ca        15 minutes ago      335 MB
docker.io/hello-world   latest              fce289e99eb9        3 weeks ago         1.84 kB
docker.io/idiswy/lnmp   latest              fbd7b7c5428e        2 years ago         335 MB
docker.io/ubuntu        15.10               9b9cb95443b5        2 years ago         137 MB


13:通过 Dockerfile 构建 hellodocker 
[root@localhost test]# cat Dockerfile
FROM centos
LABEL maintainer="Hello Docker"
RUN /bin/bash
CMD ["echo","Hello Docker"]

[root@localhost test]# docker build -t hellodocker .
Sending build context to Docker daemon 2.048 kB
Step 1/4 : FROM centos
Trying to pull repository docker.io/library/centos ... 
latest: Pulling from docker.io/library/centos
a02a4930cb5d: Pull complete 
Digest: sha256:184e5f35598e333bfa7de10d8fb1cebb5ee4df5bc0f970bf2b1e7c7345136426
Status: Downloaded newer image for docker.io/centos:latest
 ---> 1e1148e4cc2c
Step 2/4 : LABEL maintainer "Hello Docker"
 ---> Running in 20e2be20052b
 ---> f255f97c4984
Removing intermediate container 20e2be20052b
Step 3/4 : RUN /bin/bash
 ---> Running in a195ff956608

 ---> ec18faa97f10
Removing intermediate container a195ff956608
Step 4/4 : CMD echo Hello Docker
 ---> Running in fcd177b109d9
 ---> 075486018a06
Removing intermediate container fcd177b109d9
Successfully built 075486018a06

[root@localhost test]# docker images hellodocker
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
hellodocker         latest              075486018a06        About a minute ago   202 MB

[root@localhost test]# docker run -i -t hellodocker
Hello Docker

上一篇:CentOS构建Docker2
下一篇:服务器篇