三、docker镜像创建

(一)生产环境
[root@hp-uas01~]# dmidecode|grep "System Information" -A9|egrep "Manufacturer|Product"
Manufacturer: HP
Product Name: ProLiant DL380 Gen9
[root@hp-hp-uas01 ~]# uname -a
Linux linux-node2 3.10.0-693.21.1.el7.x86_64 #1 SMP Wed Mar 7 19:03:37 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
[root@hp-uas01 ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
[root@hp-uas01~]# docker --version
Docker version 18.03.1-ce, build 9ee9f40
(二)构建镜像
1、基础镜像:
$docker pull centos
$docker run -dit --name nginx_v1 centos /bin/bash
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
72ff0b0eee01 centos "/bin/bash" 3 minutes ago Up 3 minutes nginx_v1
e99ceea5c674 hello-world "/hello" 2 days ago Exited (0) 2 days ago cranky_euler
 
$ docker image list
REPOSITORY TAG IMAGE ID CREATED SIZE
nmsadm/nginx_v1 v2 7adc249e5fce 9 minutes ago 352MB##安装nginx
hello-world latest e38bc07ac18e 4 weeks ago 1.85kB
centos latest e934aafc2206 4 weeks ago 199MB
[root@linux-node2 ~]# docker start 4137a5511559
4137a5511559
[root@linux-node2 ~]# docker attach 4137a5511559
[root@4137a5511559 /]# vi /etc/nginx/nginx.conf
修改/etc/nginx/nginx.conf添加daemon off;(前台执行)
[root@linux-node2 ~]# docker commit -m "nginx_front" 4137a5511559 nmsadm/nginx_front:v1 #创建镜像
sha256:a90b84340157fcc7b78ac6816e3e80df73ba1f7c768ddab56ff6d84cd8e28d81
$dcoker image list
-bash: dcoker: command not found
$ docker image list
REPOSITORY TAG IMAGE ID CREATED SIZE
nmsadm/nginx_front v1 a90b84340157 11 seconds ago 352MB
nmsadm/nginx_v1 v2 7adc249e5fce 11 minutes ago 352MB
hello-world latest e38bc07ac18e 4 weeks ago 1.85kB
centos latest e934aafc2206 4 weeks ago 199MB
$docker run -d --name "nginx_front" nmsadm/nginx_front:v1 nginx
d5fe125cc0f2e50ef097fa0e6cbab9c44dd670c1632142e5b5428cb17a76147a
$docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d5fe125cc0f2 nmsadm/nginx_front:v1 "nginx" 8 seconds ago Up 8 seconds 80/tcp nginx_front
4137a5511559 nmsadm/nginx_v1:v2 "/bin/bash" 5 minutes ago Exited (0) 2 minutes ago agitated_montalcini
05dccdf6133c centos "/bin/bash" 22 minutes ago Exited (0) 14 minutes ago nginx_v1
e99ceea5c674 hello-world "/hello" 2 days ago Exited (0) 2 days ago cranky_euler
 
(三)dockerfile构建镜像
四要素1、基础镜像信息2、维护者信息3、镜像操作指令4、容器启动时指令;
Dockerfile语法结构
描述
FROM
基础镜像(centos7)
MAINTAINER
名称 邮箱
RUN
安装需要的包
ADD
添加文件ADD . /app拷贝当前目录内容到app目录
WORKDIR
设置工作目录(可指定应用的目录)
VOLUME
目录挂载
EXPOSE
对外开放的端口(比如WEB服务需要开放80端口)
RUN
docker中运行的应用
ENV
 NAME World定义环镜变量
$ more Dockerfile
#This is first dockerfile
#Version v1
#Author:anline
#Base iamge
FROM centos
MAINTAINER test [email protected]
RUN rpm -ivh https://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
RUN yum install -y nginx
ADD index.html /usr/share/nginx/html/indel.html
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
EXPOSE 80
CMD ["nginx"]
 
$docker build -t nginx_dockerfile:v1 /dockfile
Sending build context to Docker daemon 3.072kB
Step 1/9 : FROM centos
---> e934aafc2206
Step 2/9 : MAINTAINER anline [email protected]
---> Running in 7e6ea367066e
Removing intermediate container 7e6ea367066e
---> 58b92b9d405c
---> Running in f2507113912a
warning: /var/tmp/rpm-tmp.EBnqJJ: Header V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEY
Retrieving https://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
Preparing... ########################################
Updating / installing...
epel-release-7-11 ########################################
Removing intermediate container f2507113912a
---> b9fe6bf3b36a
Step 4/9 : RUN yum install -y nginx
---> Running in 64d809002500
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
* base: mirrors.shu.edu.cn
* epel: mirrors.tongji.edu.cn
* extras: mirrors.njupt.edu.cn
* updates: mirrors.shu.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 1:1.12.2-2.el7 will be installed
........
perl-Scalar-List-Utils x86_64 1.27-248.el7 base 36 k
$ docker image list
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx_dockerfile v1 a917bf2cb33f 12 seconds ago 371MB
nmsadm/nginx_front v1 a90b84340157 About an hour ago 352MB
nmsadm/nginx_v1 v2 7adc249e5fce About an hour ago 352MB
hello-world latest e38bc07ac18e 4 weeks ago 1.85kB
centos latest e934aafc2206 4 weeks ago 199MB
$docker run -d -p 83:80 nmsadm/nginx_dockerfile:v1
e0a5e59f47f7c15ed8e4be247d5962d6985f65142a73c1b8b00184b4796dbb74
$docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e0a5e59f47f7 nmsadm/nginx_dockerfile:v1 "nginx" 3 minutes ago Exited (1) 1 second ago keen_tereshkova
d5fe125cc0f2 nmsadm/nginx_front:v1 "nginx" 19 hours ago Exited (0) 18 hours ago nginx_front
4137a5511559 nmsadm/nginx_v1:v2 "/bin/bash" 19 hours ago Exited (0) 19 hours ago agitated_montalcini
05dccdf6133c centos "/bin/bash" 19 hours ago Exited (0) 19 hours ago nginx_v1
e99ceea5c674 hello-world "/hello" 3 days ago Exited (0) 3 days ago cranky_euler
使用新构建的镜像启动容器,但是启动就退出。检查一下容器的报错的LOG
$docker logs e0a5e59f47f7
nginx: [emerg] socket() [::]:80 failed (97: Address family not supported by protocol)
nginx: [emerg] socket() [::]:80 failed (97: Address family not supported by protocol)
需要修改nginx的配置文件:
将 listen 80 default_server;
listen [::]:80 default_server;
 
listen 80;
# listen [::]:80 default_server;
修改Dockerfile内容:
#This is first dockerfile
#Version v1
#Author:anline
#Base iamge
FROM centos
MAINTAINER test [email protected]
RUN rpm -ivh https://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
RUN yum install -y nginx
ADD index.html /usr/share/nginx/html/indel.html
RUN sed -i '39,40d' /etc/nginx/nginx.conf
RUN sed -i '38 alisten 80;' /etc/nginx/nginx.conf
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
EXPOSE 80
CMD ["nginx"]
 
$docker run -d -p 808:80 nmsadm/nginx_dockerfile:v1
2f9ab83be035dd384350d22d1e46347ac35afa94444f0ad38e1f9e938d1875a0
验证:
$docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2f9ab83be035 nmsadm/nginx_dockerfile:v1 "nginx" 3 seconds ago Up 2 seconds 0.0.0.0:808->80/tcp happy_bartik
49b1005174b2 nmsadm/nginx_dockerfile:v1 "nginx" 2 minutes ago Exited (0) 13 seconds ago confident_montalcini
d5fe125cc0f2 nmsadm/nginx_front:v1 "nginx" 20 hours ago Exited (0) About an hour ago nginx_front
4137a5511559 nmsadm/nginx_v1:v2 "/bin/bash" 20 hours ago Exited (0) 6 minutes ago agitated_montalcini
05dccdf6133c centos "/bin/bash" 21 hours ago Exited (0) 20 hours ago nginx_v1
e99ceea5c674 hello-world "/hello" 3 days ago Exited (0) 3 days ago cranky_euler

猜你喜欢

转载自www.cnblogs.com/icerain0/p/9030377.html
今日推荐