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 ~]# - 把当前容器提交为一个镜像,指定名称和版本号
root@Docker-10.2.217.217[20:49:02]$ docker commit -m "nginx-v1" cee964f73ce4 nginx-yum:v1
sha256:84d367dcb28f03a09354051ee6cfe06107068f6890c0e20ded2bf60a87acb9b0
root@Docker-10.2.217.217[20:50:22]$
root@Docker-10.2.217.217[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
root@Docker-10.2.217.217[20:50:37]$ - 使用做好的nginx镜像启动nginx
root@Docker-10.2.217.217[20:59:12]$ docker run -it --rm -d -p 8080:80 nginx-yum:v1 nginx
c1997e0460ebc155333efd3a616d467f2cc8475200234c773b37daf0c1cf7a68
root@Docker-10.2.217.217[20:59:28]$
root@Docker-10.2.217.217[20:59:29]$
root@Docker-10.2.217.217[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
root@Docker-10.2.217.217[20:59:31]$
root@Docker-10.2.217.217[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 :::*
root@Docker-10.2.217.217[20:59:39]$ 注意:如果使用相同的版本号提交镜像会把之前提交的覆盖掉!

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

root@Docker-10.2.217.217[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
root@Docker-10.2.217.217[21:27:03]$
root@Docker-10.2.217.217[21:23:19]$ pwd
/opt/dockerfile/system/centos/nginx
root@Docker-10.2.217.217[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] root@Docker-10.2.217.217[21:21:12]$ ll
total 4
-rw-r--r--. 1 root root 2523 Jun 16 06:22 Centos-7.repo
root@Docker-10.2.217.217[21:21:13]$
root@Docker-10.2.217.217[21:22:55]$ vim Dockerfile
root@Docker-10.2.217.217[21:23:18]$
root@Docker-10.2.217.217[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 root@Docker-10.2.217.217[21:36:20]$ root@Docker-10.2.217.217[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
root@Docker-10.2.217.217[21:35:32]$
root@Docker-10.2.217.217[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
root@Docker-10.2.217.217[21:37:03]$

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

root@Docker-192.168.1.100[00:35:03]$ pwd
/opt/dockerfile/system/centos/nginx
root@Docker-192.168.1.100[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
root@Docker-192.168.1.100[00:35:07]$ cat index.html
<h1>This is index page of nginx running in docker.</h1>
root@Docker-192.168.1.100[00:35:10]$ cat app/index.html
app index page
root@Docker-192.168.1.100[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 {
}
} } root@Docker-192.168.1.100[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"]
root@Docker-192.168.1.100[00:35:22]$ root@Docker-192.168.1.100[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
root@Docker-192.168.1.100[00:21:13]$ root@Docker-192.168.1.100[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
root@Docker-192.168.1.100[00:21:18]$ root@Docker-192.168.1.100[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
root@Docker-192.168.1.100[00:23:52]$
root@Docker-192.168.1.100[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 :::*
root@Docker-192.168.1.100[00:24:03]$

验证结果

进入运行着的容器查看

root@Docker-192.168.1.100[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 /]#

本地访问

Docker制作基础镜像

示例4:制作一个CentOS基础镜像

root@Docker-192.168.1.100[22:00:05]$ ll
total 12
-rw-r--r--. 1 root root 2523 Jul 10 21:53 Centos-7.repo
-rw-r--r--. 1 root root 448 Jul 10 21:55 Dockerfile
-rw-r--r--. 1 root root 664 Jul 10 21:53 epel-7.repo
root@Docker-192.168.1.100[22:00:11]$ 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 # RUN yum install https://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm -y
RUN yum makecache
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 root@Docker-192.168.1.100[22:00:17]$
root@Docker-192.168.1.100[21:57:53]$ docker build -t centos-base:$(date +%s) .
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 : ADD epel-7.repo /etc/yum.repos.d/epel.repo
---> Using cache
---> 7b35b27c806a
Step 6/7 : RUN yum makecache
---> Running in a4a697bfad71
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
...
Complete!
Removing intermediate container 598202d6d454
---> 3a5164a09dae
Successfully built 3a5164a09dae
Successfully tagged centos-base:1531231076
root@Docker-192.168.1.100[21:59:47]$
root@Docker-192.168.1.100[21:59:58]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos-base 1531231076 3a5164a09dae 16 seconds ago 996MB
dockerfile-nginx-make 20180710002000 eb3c0e74d31b 22 hours ago 508MB
dockerfile-nginx-make 20180710001500 d3dd300ab743 22 hours ago 508MB
dockerfile-nginx-make 20180710001200 0e82c28b3e37 22 hours ago 508MB
dockerfile-nginx-make 20180710001000 cfec9eec093c 22 hours ago 508MB
dockerfile-nginx-make 20180710000500 bdf7f5e65a2d 22 hours ago 508MB
dockerfile-nginx-make v2 2f72d76314a3 22 hours ago 508MB
dockerfile-nginx-make v1 d01823870cac 24 hours ago 507MB
dockerfile-yum-ngin v2 6d54c875204b 24 hours ago 561MB
dockerfile-yum-ngin v1 7cebf51fd7b9 25 hours ago 432MB
nginx-yum v2 f66b6be1bed7 25 hours ago 531MB
nginx-yum v1 84d367dcb28f 25 hours ago 531MB
nginx latest 3c5a05123222 3 days ago 109MB
ubuntu latest 113a43faa138 4 weeks ago 81.2MB
centos latest 49f7960eb7e4 5 weeks ago 200MB
centos 7.2.1511 0a2bad7da9b5 8 months ago 195MB
root@Docker-192.168.1.100[22:00:02]$

验证结果

root@Docker-192.168.1.100[22:01:21]$ docker run -it --rm centos-base:1531231076 bash
[root@3172e76a89e7 /]#
[root@3172e76a89e7 /]#
[root@3172e76a89e7 /]# ll /etc/yum.repos.d/
total 8
-rw-r--r--. 1 root root 2523 Jun 15 22:22 CentOS-Base.repo
-rw-r--r--. 1 root root 664 May 11 03:35 epel.repo
[root@3172e76a89e7 /]#
[root@3172e76a89e7 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.3 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:03 txqueuelen 0 (Ethernet)
RX packets 8 bytes 648 (648.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@3172e76a89e7 /]# yum install vim wget tree lrzsz gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel iproute net-tools iotop -y
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Package 2:vim-enhanced-7.4.160-4.el7.x86_64 already installed and latest version
Package wget-1.14-15.el7_4.1.x86_64 already installed and latest version
Package tree-1.6.0-10.el7.x86_64 already installed and latest version
Package lrzsz-0.12.20-36.el7.x86_64 already installed and latest version
Package gcc-4.8.5-28.el7_5.1.x86_64 already installed and latest version
Package gcc-c++-4.8.5-28.el7_5.1.x86_64 already installed and latest version
Package automake-1.13.4-3.el7.noarch already installed and latest version
Package pcre-8.32-17.el7.x86_64 already installed and latest version
Package pcre-devel-8.32-17.el7.x86_64 already installed and latest version
Package zlib-1.2.7-17.el7.x86_64 already installed and latest version
Package zlib-devel-1.2.7-17.el7.x86_64 already installed and latest version
Package 1:openssl-1.0.2k-12.el7.x86_64 already installed and latest version
Package 1:openssl-devel-1.0.2k-12.el7.x86_64 already installed and latest version
Package iproute-4.11.0-14.el7.x86_64 already installed and latest version
Package net-tools-2.0-0.22.20131004git.el7.x86_64 already installed and latest version
Package iotop-0.6-2.el7.noarch already installed and latest version
Nothing to do
[root@3172e76a89e7 /]#
[root@3172e76a89e7 /]# exit
exit
root@Docker-192.168.1.100[22:02:51]$

示例5:基于centos基础镜像创建一个nginx基础镜像

root@Docker-192.168.1.100[22:23:58]$ ll
total 964
-rw-r--r--. 1 root root 343 Jul 10 22:23 Dockerfile
-rw-r--r--. 1 root root 981687 Jul 10 22:07 nginx-1.12.2.tar.gz
root@Docker-192.168.1.100[22:24:02]$
root@Docker-192.168.1.100[22:24:02]$ cat Dockerfile
# Nginx base image. FROM centos-base:1531231076 MAINTAINER standby 71standby@gamil RUN useradd nginx -s /sbin/nologin -M
ADD nginx-1.12.2.tar.gz /usr/local/src/
RUN cd /usr/local/src/nginx-1.12.2 && ./configure --prefix=/usr/local/nginx --with-http_sub_module && make && make install
RUN ln -sv /usr/local/nginx/sbin/nginx /usr/sbin/nginx root@Docker-192.168.1.100[22:24:04]$
root@Docker-192.168.1.100[22:24:06]$ docker build -t nginx-base:$(date +%s) .
Sending build context to Docker daemon 984.6kB
Step 1/6 : FROM centos-base:1531231076
---> 3a5164a09dae
Step 2/6 : MAINTAINER standby 71standby@gamil
---> Using cache
---> 540ae2a4f5c2
Step 3/6 : RUN useradd nginx -s /sbin/nologin -M
---> Using cache
---> 59c9fe720656
Step 4/6 : ADD nginx-1.12.2.tar.gz /usr/local/src/
---> Using cache
---> f7f7cb7efd3c
Step 5/6 : RUN cd /usr/local/src/nginx-1.12.2 && ./configure --prefix=/usr/local/nginx --with-http_sub_module && make && make install
---> Using cache
---> d11d0daaefcf
Step 6/6 : RUN ln -sv /usr/local/nginx/sbin/nginx /usr/sbin/nginx
---> Using cache
---> c845e835b3b5
Successfully built c845e835b3b5
Successfully tagged nginx-base:1531232648
root@Docker-192.168.1.100[22:24:08]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx-base 1531232482 fcfcdd0c059b 2 minutes ago 1.02GB
nginx-base 1531232638 c845e835b3b5 2 minutes ago 1.02GB
nginx-base 1531232648 c845e835b3b5 2 minutes ago 1.02GB
centos-base 1531231076 3a5164a09dae 24 minutes ago 996MB
dockerfile-nginx-make 20180710002000 eb3c0e74d31b 22 hours ago 508MB
dockerfile-nginx-make 20180710001500 d3dd300ab743 22 hours ago 508MB
dockerfile-nginx-make 20180710001200 0e82c28b3e37 22 hours ago 508MB
dockerfile-nginx-make 20180710001000 cfec9eec093c 22 hours ago 508MB
dockerfile-nginx-make 20180710000500 bdf7f5e65a2d 22 hours ago 508MB
dockerfile-nginx-make v2 2f72d76314a3 23 hours ago 508MB
dockerfile-nginx-make v1 d01823870cac 24 hours ago 507MB
dockerfile-yum-ngin v2 6d54c875204b 25 hours ago 561MB
dockerfile-yum-ngin v1 7cebf51fd7b9 25 hours ago 432MB
nginx-yum v2 f66b6be1bed7 25 hours ago 531MB
nginx-yum v1 84d367dcb28f 26 hours ago 531MB
nginx latest 3c5a05123222 3 days ago 109MB
ubuntu latest 113a43faa138 4 weeks ago 81.2MB
centos latest 49f7960eb7e4 5 weeks ago 200MB
centos 7.2.1511 0a2bad7da9b5 8 months ago 195MB
root@Docker-192.168.1.100[22:24:14]$

示例6:基于nginx基础镜像构建一个业务镜像

root@Docker-192.168.1.100[23:09:08]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx-base 1531232648 c845e835b3b5 About an hour ago 1.02GB
centos-base 1531231076 3a5164a09dae About an hour ago 996MB
dockerfile-nginx-make 20180710002000 eb3c0e74d31b 23 hours ago 508MB
dockerfile-nginx-make v2 2f72d76314a3 23 hours ago 508MB
dockerfile-nginx-make v1 d01823870cac 25 hours ago 507MB
dockerfile-yum-ngin v2 6d54c875204b 26 hours ago 561MB
dockerfile-yum-ngin v1 7cebf51fd7b9 26 hours ago 432MB
nginx-yum v2 f66b6be1bed7 26 hours ago 531MB
nginx-yum v1 84d367dcb28f 26 hours ago 531MB
nginx latest 3c5a05123222 3 days ago 109MB
ubuntu latest 113a43faa138 4 weeks ago 81.2MB
centos latest 49f7960eb7e4 5 weeks ago 200MB
centos 7.2.1511 0a2bad7da9b5 8 months ago 195MB
root@Docker-192.168.1.100[23:09:13]$ root@Docker-192.168.1.100[23:10:17]$ cat Dockerfile
# Nginx app01 image. FROM nginx-base:1531232648
MAINTAINER standby 71standby@gamil ADD nginx.conf /usr/local/nginx/conf/ ADD index.html /usr/local/nginx/html/
ADD app/* /usr/local/nginx/html/app/ EXPOSE 80 443 CMD ["nginx"]
root@Docker-192.168.1.100[23:10:20]$
root@Docker-192.168.1.100[23:09:26]$ docker build -t nginx-web01:$(date +%s) .
Sending build context to Docker daemon 8.192kB
Step 1/7 : FROM nginx-base:1531232648
---> c845e835b3b5
Step 2/7 : MAINTAINER standby 71standby@gamil
---> Running in 42a6273faf2a
Removing intermediate container 42a6273faf2a
---> 593028330887
Step 3/7 : ADD nginx.conf /usr/local/nginx/conf/
---> 8fcb465ebab9
Step 4/7 : ADD index.html /usr/local/nginx/html/
---> 1e3b9b844fb3
Step 5/7 : ADD app/* /usr/local/nginx/html/app/
---> e3ea829cc475
Step 6/7 : EXPOSE 80 443
---> Running in 2f320d36ab6c
Removing intermediate container 2f320d36ab6c
---> d744bddf4094
Step 7/7 : CMD ["nginx"]
---> Running in 43f0d0f429bf
Removing intermediate container 43f0d0f429bf
---> 365c798bec11
Successfully built 365c798bec11
Successfully tagged nginx-web01:1531235372
root@Docker-192.168.1.100[23:09:34]$
root@Docker-192.168.1.100[23:09:34]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx-web01 1531235372 365c798bec11 6 seconds ago 1.02GB
nginx-base 1531232648 c845e835b3b5 About an hour ago 1.02GB
centos-base 1531231076 3a5164a09dae About an hour ago 996MB
dockerfile-nginx-make 20180710002000 eb3c0e74d31b 23 hours ago 508MB
dockerfile-nginx-make v2 2f72d76314a3 23 hours ago 508MB
dockerfile-nginx-make v1 d01823870cac 25 hours ago 507MB
dockerfile-yum-ngin v2 6d54c875204b 26 hours ago 561MB
dockerfile-yum-ngin v1 7cebf51fd7b9 26 hours ago 432MB
nginx-yum v2 f66b6be1bed7 26 hours ago 531MB
nginx-yum v1 84d367dcb28f 26 hours ago 531MB
nginx latest 3c5a05123222 3 days ago 109MB
ubuntu latest 113a43faa138 4 weeks ago 81.2MB
centos latest 49f7960eb7e4 5 weeks ago 200MB
centos 7.2.1511 0a2bad7da9b5 8 months ago 195MB
root@Docker-192.168.1.100[23:09:40]$

启动业务镜像容器

root@Docker-192.168.1.100[23:09:13]$ docker run -it -d --rm -p 9009:80 nginx-web01:1531235372
64d98e9e9cf0fb0937aa32e1d6a7f37f953f6a105d83996eb627681b4a1b85f8
root@Docker-192.168.1.100[23:09:58]$
root@Docker-192.168.1.100[23:09:59]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
64d98e9e9cf0 nginx-web01:1531235372 "nginx" 2 seconds ago Up 1 second 443/tcp, 0.0.0.0:9009->80/tcp musing_ride
db15afe6a36f dockerfile-nginx-make:20180710002000 "/usr/local/nginx/sb…" 2 hours ago Up 2 hours 443/tcp, 0.0.0.0:8080->80/tcp pensive_archimedes
root@Docker-192.168.1.100[23:10:00]$

验证结果

Docker制作基础镜像

上一篇:金老师的经典著作《一个普通IT人的十年回顾》


下一篇:解析好的静态页面.shtml浏览器无法解析.需要apache解析后再返回给浏览器