Docker制作基础镜像

 Docker镜像制作

方式一:手动运行一个容器,做好所有配置,然后把容器提交成一个镜像

方式二:使用DockerFile

示例1:做一个yum安装的nginx镜像

- 运行并进入一个centos容器:docker run -it --rm 49f7960eb7e4 bash
	- yum install wget -y
	- wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo  # 替换为阿里的yum源
	- yum install https://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm   # 安装epel源
	- yum install vim pcre pcre-devel zlib zlib-devel openssl openssl-devel iproute net-tools iotop -y  # 安装基础命令
	- yum install nginx -y  # yum安装nginx

	- 更改/etc/nginx/nginx.conf,关闭nginx的后台运行,即增加 daemon off;

	- 自定义主页
		[root@cee964f73ce4 ~]# cat /usr/share/nginx/html/index.html

		<h1> Yum Nginx 2018-07-09 21:00:00 </h1>

		[root@cee964f73ce4 ~]#


- 把当前容器提交为一个镜像,指定名称和版本号
[email protected][20:49:02]$ docker commit -m "nginx-v1" cee964f73ce4 nginx-yum:v1
sha256:84d367dcb28f03a09354051ee6cfe06107068f6890c0e20ded2bf60a87acb9b0
[email protected][20:50:22]$ 
[email protected][20:50:34]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx-yum           v1                  84d367dcb28f        15 seconds ago      531MB
nginx               latest              3c5a05123222        2 days ago          109MB
ubuntu              latest              113a43faa138        4 weeks ago         81.2MB
centos              latest              49f7960eb7e4        4 weeks ago         200MB
centos              7.2.1511            0a2bad7da9b5        8 months ago        195MB
[email protected][20:50:37]$


- 使用做好的nginx镜像启动nginx
[email protected][20:59:12]$ docker run -it --rm -d -p 8080:80 nginx-yum:v1 nginx
c1997e0460ebc155333efd3a616d467f2cc8475200234c773b37daf0c1cf7a68
[email protected][20:59:28]$ 
[email protected][20:59:29]$ 
[email protected][20:59:29]$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                  NAMES
c1997e0460eb        nginx-yum:v1        "nginx"             3 seconds ago       Up 2 seconds        0.0.0.0:8080->80/tcp   naughty_bose
cee964f73ce4        49f7960eb7e4        "bash"              19 minutes ago      Up 19 minutes                              tender_darwin
[email protected][20:59:31]$ 
[email protected][20:59:37]$ ss -tnl 
State      Recv-Q Send-Q                             Local Address:Port                                            Peer Address:Port              
LISTEN     0      128                                            *:22                                                         *:*                  
LISTEN     0      100                                    127.0.0.1:25                                                         *:*                  
LISTEN     0      128                                           :::8080                                                      :::*                  
LISTEN     0      128                                           :::22                                                        :::*                  
LISTEN     0      100                                          ::1:25                                                        :::*                  
[email protected][20:59:39]$ 


注意:如果使用相同的版本号提交镜像会把之前提交的覆盖掉!

示例2:编写DockerFile构建yum安装的nginx镜像

[email protected][21:26:59]$ docker images 
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
nginx-yum             v2                  f66b6be1bed7        22 minutes ago      531MB
nginx-yum             v1                  84d367dcb28f        36 minutes ago      531MB
nginx                 latest              3c5a05123222        2 days ago          109MB
ubuntu                latest              113a43faa138        4 weeks ago         81.2MB
centos                latest              49f7960eb7e4        4 weeks ago         200MB
centos                7.2.1511            0a2bad7da9b5        8 months ago        195MB
[email protected][21:27:03]$ 
[email protected][21:23:19]$ pwd
/opt/dockerfile/system/centos/nginx
[email protected][21:21:10]$ wget http://mirrors.aliyun.com/repo/Centos-7.repo
--2018-07-09 21:21:12--  http://mirrors.aliyun.com/repo/Centos-7.repo
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 121.18.239.215, 121.18.239.213, 121.18.239.211, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|121.18.239.215|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2523 (2.5K) [application/octet-stream]
Saving to: ‘Centos-7.repo’

100%[==========================================================================================================>] 2,523       --.-K/s   in 0s      

2018-07-09 21:21:12 (194 MB/s) - ‘Centos-7.repo’ saved [2523/2523]

[email protected][21:21:12]$ ll
total 4
-rw-r--r--. 1 root root 2523 Jun 16 06:22 Centos-7.repo
[email protected][21:21:13]$  
[email protected][21:22:55]$ vim Dockerfile 
[email protected][21:23:18]$
[email protected][21:35:47]$ cat Dockerfile 
# Nginx dockerfile 2018-07-09

FROM centos

MAINTAINER standby 71standby@gamil

RUN rm -rf /etc/yum.repos.d/*

ADD Centos-7.repo /etc/yum.repos.d/CentOS-Base.repo

RUN yum install https://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm -y

RUN yum install vim pcre pcre-devel zlib zlib-devel openssl openssl-devel iproute net-tools iotop -y

RUN yum install nginx -y

[email protected][21:36:20]$

[email protected][21:35:11]$ docker build -t dockerfile-yum-nginx:v2 . 
Sending build context to Docker daemon  6.656kB
Step 1/7 : FROM centos
 ---> 49f7960eb7e4
Step 2/7 : MAINTAINER standby 71standby@gamil
 ---> Using cache
 ---> 9470da6f3f63
Step 3/7 : RUN rm -rf /etc/yum.repos.d/*
 ---> Using cache
 ---> d284b3695812
Step 4/7 : ADD Centos-7.repo /etc/yum.repos.d/CentOS-Base.repo
 ---> Using cache
 ---> c748ad3e145d
Step 5/7 : RUN yum install https://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm -y
 ---> Using cache
 ---> 8c91b0da526d
Step 6/7 : RUN yum install vim pcre pcre-devel zlib zlib-devel openssl openssl-devel iproute net-tools iotop -y
 ---> Using cache
 ---> 7cebf51fd7b9
Step 7/7 : RUN yum install nginx -y
 ---> Running in 95087cf396ad
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * epel: mirrors.tuna.tsinghua.edu.cn
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 1:1.12.2-2.el7 will be installed
...
Complete!
Removing intermediate container 95087cf396ad
 ---> 6d54c875204b
Successfully built 6d54c875204b
Successfully tagged dockerfile-yum-nginx:v2
[email protected][21:35:32]$ 
[email protected][21:36:20]$ docker images
REPOSITORY            TAG                 IMAGE ID            CREATED              SIZE
dockerfile-yum-nginx  v2                  6d54c875204b        About a minute ago   561MB
dockerfile-yum-nginx  v1                  7cebf51fd7b9        10 minutes ago       432MB
nginx-yum             v2                  f66b6be1bed7        32 minutes ago       531MB
nginx-yum             v1                  84d367dcb28f        About an hour ago    531MB
nginx                 latest              3c5a05123222        2 days ago           109MB
ubuntu                latest              113a43faa138        4 weeks ago          81.2MB
centos                latest              49f7960eb7e4        4 weeks ago          200MB
centos                7.2.1511            0a2bad7da9b5        8 months ago         195MB
[email protected][21:37:03]$ 

示例3:编写DockerFile构建编译安装nginx的镜像  

[email protected][00:35:03]$ pwd
/opt/dockerfile/system/centos/nginx
[email protected][00:35:05]$ ll
total 980
drwxr-xr-x. 2 root root     24 Jul 10 00:01 app
-rw-r--r--. 1 root root   2523 Jun 16 06:22 Centos-7.repo
-rw-r--r--. 1 root root    855 Jul 10 00:13 Dockerfile
-rw-r--r--. 1 root root    664 May 11 11:35 epel-7.repo
-rw-r--r--. 1 root root     56 Jul 10 00:01 index.html
-rw-r--r--. 1 root root 981687 Oct 17  2017 nginx-1.12.2.tar.gz
-rw-r--r--. 1 root root   1026 Jul 10 00:21 nginx.conf
[email protected][00:35:07]$ cat index.html 
<h1>This is index page of nginx running in docker.</h1>
[email protected][00:35:10]$ cat app/index.html 
app index page
[email protected][00:35:12]$ cat nginx.conf 

user nginx nginx;
worker_processes auto;

pid  logs/nginx.pid;

daemon off;

events {
    worker_connections  102400;
    use epoll;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;
    error_log  logs/error.log;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             mime.types;
    default_type        application/octet-stream;


    server {
        listen       80 default_server;
        server_name  _;
        root         /usr/local/nginx/html;

        include conf.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

}

[email protected][00:35:16]$ cat Dockerfile 
# Nginx dockerfile 2018-07-09

FROM centos
MAINTAINER standby 71standby@gamil

RUN rm -rf /etc/yum.repos.d/*
ADD Centos-7.repo /etc/yum.repos.d/CentOS-Base.repo
ADD epel-7.repo /etc/yum.repos.d/epel.repo
ADD nginx-1.12.2.tar.gz /usr/local/src/

# RUN yum install https://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm -y

RUN yum install vim wget tree lrzsz gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel iproute net-tools iotop -y

RUN useradd nginx -s /sbin/nologin -M
RUN cd /usr/local/src/nginx-1.12.2 && ./configure --prefix=/usr/local/nginx --with-http_sub_module && make && make install
ADD nginx.conf /usr/local/nginx/conf/
# RUN ln -sv /usr/local/nginx/sbin/nginx /usr/sbin/nginx

ADD index.html /usr/local/nginx/html/
ADD app/* /usr/local/nginx/html/app/

EXPOSE 80 443

CMD ["/usr/local/nginx/sbin/nginx"]
[email protected][00:35:22]$ 

[email protected][00:21:03]$ docker build -t dockerfile-nginx-make:20180710002000 .
Sending build context to Docker daemon  994.3kB
Step 1/14 : FROM centos
 ---> 49f7960eb7e4
Step 2/14 : MAINTAINER standby 71standby@gamil
 ---> Using cache
 ---> 9470da6f3f63
Step 3/14 : RUN rm -rf /etc/yum.repos.d/*
 ---> Using cache
 ---> d284b3695812
Step 4/14 : ADD Centos-7.repo /etc/yum.repos.d/CentOS-Base.repo
 ---> Using cache
 ---> c748ad3e145d
Step 5/14 : ADD epel-7.repo /etc/yum.repos.d/epel.repo
 ---> Using cache
 ---> 7b35b27c806a
Step 6/14 : ADD nginx-1.12.2.tar.gz /usr/local/src/
 ---> Using cache
 ---> 4729a577fad3
Step 7/14 : RUN yum install vim wget tree lrzsz gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel iproute net-tools iotop -y
 ---> Using cache
 ---> abfd8019dff7
Step 8/14 : RUN useradd nginx -s /sbin/nologin -M
 ---> Using cache
 ---> b29432e170eb
Step 9/14 : RUN cd /usr/local/src/nginx-1.12.2 && ./configure --prefix=/usr/local/nginx --with-http_sub_module && make && make install
 ---> Using cache
 ---> 076ea8c60bae
Step 10/14 : ADD nginx.conf /usr/local/nginx/conf/
 ---> 52b98678a1be
Step 11/14 : ADD index.html /usr/local/nginx/html/
 ---> 2228a36de4c0
Step 12/14 : ADD app/* /usr/local/nginx/html/app/
 ---> d09b507721dd
Step 13/14 : EXPOSE 80 443
 ---> Running in aa3a30431bb4
Removing intermediate container aa3a30431bb4
 ---> c06e9e266fce
Step 14/14 : CMD ["/usr/local/nginx/sbin/nginx"]
 ---> Running in ab2eef1158d0
Removing intermediate container ab2eef1158d0
 ---> eb3c0e74d31b
Successfully built eb3c0e74d31b
Successfully tagged dockerfile-nginx-make:20180710002000
[email protected][00:21:13]$

[email protected][00:21:14]$ docker images 
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
dockerfile-nginx-make   20180710002000      eb3c0e74d31b        5 seconds ago       508MB
dockerfile-nginx-make   20180710001500      d3dd300ab743        6 minutes ago       508MB
dockerfile-nginx-make   20180710001200      0e82c28b3e37        9 minutes ago       508MB
dockerfile-nginx-make   20180710001000      cfec9eec093c        11 minutes ago      508MB
dockerfile-nginx-make   20180710000500      bdf7f5e65a2d        15 minutes ago      508MB
dockerfile-nginx-make   v2                  2f72d76314a3        33 minutes ago      508MB
dockerfile-nginx-make   v1                  d01823870cac        2 hours ago         507MB
dockerfile-yum-ngin     v2                  6d54c875204b        3 hours ago         561MB
dockerfile-yum-ngin     v1                  7cebf51fd7b9        3 hours ago         432MB
nginx-yum               v2                  f66b6be1bed7        3 hours ago         531MB
nginx-yum               v1                  84d367dcb28f        4 hours ago         531MB
nginx                   latest              3c5a05123222        3 days ago          109MB
ubuntu                  latest              113a43faa138        4 weeks ago         81.2MB
centos                  latest              49f7960eb7e4        4 weeks ago         200MB
centos                  7.2.1511            0a2bad7da9b5        8 months ago        195MB
[email protected][00:21:18]$

[email protected][00:23:41]$ docker ps 
CONTAINER ID        IMAGE                                  COMMAND                  CREATED             STATUS              PORTS                           NAMES
c742b2ed9faf        dockerfile-nginx-make:20180710002000   "/usr/local/nginx/sb…"   50 seconds ago      Up 49 seconds       443/tcp, 0.0.0.0:8080->80/tcp   naughty_spence
[email protected][00:23:52]$ 
[email protected][00:24:00]$ ss -tnl
State      Recv-Q Send-Q                             Local Address:Port                                            Peer Address:Port              
LISTEN     0      128                                            *:22                                                         *:*                  
LISTEN     0      100                                    127.0.0.1:25                                                         *:*                  
LISTEN     0      128                                           :::8080                                                      :::*                  
LISTEN     0      128                                           :::22                                                        :::*                  
LISTEN     0      100                                          ::1:25                                                        :::*                  
[email protected][00:24:03]$ 

验证结果

进入运行着的容器查看

[email protected][00:47:01]$ docker exec -it c742b2ed9faf bash
[root@c742b2ed9faf /]# 
[root@c742b2ed9faf /]# ps -ef |grep nginx
root          1      0  0 16:23 pts/0    00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx         5      1  0 16:23 pts/0    00:00:00 nginx: worker process
nginx         6      1  0 16:23 pts/0    00:00:00 nginx: worker process
root         23      7  0 16:47 pts/1    00:00:00 grep --color=auto nginx
[root@c742b2ed9faf /]# cat /usr/local/nginx/conf/nginx.conf

user nginx nginx;
worker_processes auto;

pid  logs/nginx.pid;

daemon off;

events {
    worker_connections  102400;
    use epoll;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;
    error_log  logs/error.log;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             mime.types;
    default_type        application/octet-stream;


    server {
        listen       80 default_server;
        server_name  _;
        root         /usr/local/nginx/html;

        include conf.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

}

[root@c742b2ed9faf /]# 
[root@c742b2ed9faf /]# 
[root@c742b2ed9faf /]# ll /usr/local/nginx/html/          
total 8
-rw-r--r--. 1 root root 537 Jul  9 15:48 50x.html
drwxr-xr-x. 2 root root  24 Jul  9 16:21 app
-rw-r--r--. 1 root root  56 Jul  9 16:01 index.html
[root@c742b2ed9faf /]# cat /usr/local/nginx/html/index.html
<h1>This is index page of nginx running in docker.</h1>
[root@c742b2ed9faf /]# 
[root@c742b2ed9faf /]# cat /usr/local/nginx/html/app/index.html 
app index page
[root@c742b2ed9faf /]# 

本地访问

猜你喜欢

转载自www.cnblogs.com/standby/p/9286839.html