Docker 镜像构建
构建分为两种
- 手动构建
- 自动构建
dockerfile
手动构建
首先启动一个Centos 容器,然后在容器中安装一个nginx
[root@node ~]# docker run -it --name nginx-man centos
[root@b82585fe6777 /]#
安装Nginx
这一步就很简单了,贴一下命令
[root@b82585fe6777 ~]# yum install wget gcc gcc-c++ openssl-devel zlib-devel pcre-devel -y
[root@b82585fe6777 ~]# wget http://nginx.org/download/nginx-1.12.2.tar.gz
[root@b82585fe6777 ~]# tar xf nginx-1.12.2.tar.gz
[root@b82585fe6777 ~]# cd nginx-1.12.2
[root@b82585fe6777 nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-pcre
[root@b82585fe6777 nginx-1.12.2]# make && make install
[root@b82585fe6777 nginx-1.12.2]# ls /usr/local/nginx/
conf html logs sbin
这样就还差一步,配置nginx配置文件,使nginx运行在前台
# 在nginx主配置文件的最上面,增加下面的内容即可
[root@b82585fe6777 conf]# vi nginx.conf
daemon off;
这样就安装完成了
手动构建镜像
现在开始把刚刚已经安装好的nginx的容器,构建成镜像
[root@node ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b82585fe6777 centos "/bin/bash" About an hour ago Exited (0) About an hour ago nginx-man
[root@node ~]# docker commit -m 'my nginx' b82585fe6777 sijiayong/my-nginx:v1
sha256:a80ecf40e4d253416b61a69a8572e856d621b0ca02423e1de4afedea01626670
[root@node ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
sijiayong/my-nginx v1 a80ecf40e4d2 5 seconds ago 328 MB
docker.io/centos latest 5182e96772bf 2 weeks ago 200 MB
docker.io/nginx latest c82521676580 4 weeks ago 109 MB
这里看到,手动构建完成,可以看到一个sijiayong/my-nginx
的镜像。
启动刚刚手动构建的容器
[root@node ~]# docker run -d -p 99:80 --name mynginx sijiayong/my-nginx:v1 /usr/local/nginx/sbin/nginx
417102138d7a516c1c5ce236c8647f1f64199d8e7ca89358f6d457432bf7501f
[root@node ~]#
[root@node ~]#
[root@node ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
417102138d7a sijiayong/my-nginx:v1 "/usr/local/nginx/..." 8 seconds ago Up 8 seconds 0.0.0.0:99->80/tcp mynginx
已经起来了,下面来测试访问:
Dockerfile自动构建
- 基础镜像信息
- 维护者信息
- 镜像操作指令
- 容器启动时执行指令
Dockerfile文件内容:
字段
解释
FROM
基础镜像
MAINTAINER
维护者信息
RUN
把命令前面加上RUN
ADD
COPY文件,会自动解压
WORKDIR
当前目录
VOLUME
目录挂在
EXPOSE
端口
RUN
进程要一直运行下去
下面来开始
准备工作:
- Centos 镜像
- nginx源码安装包
- Centos7国内源的repo文件,(阿里云或者163的,下载下来即可)
- 创建号目录
/opt/docker-file/nginx
需要把nginx源码包和yum源repo文件都放在创建好的nginx目录下,否则在build的时候,会找不到
[root@node nginx]# pwd
/opt/docker-file/nginx
[root@node nginx]# ls
CentOS7-Base-163.repo Dockerfile nginx-1.12.2.tar.gz
[root@node nginx]# cat Dockerfile
# This is My first Dockerfile
# Version 1.0
# Author: sijiayong
# Base images
FROM centos
# weihuzhe
MAINTAINER sijiayong000@163.com
# ADD file
ADD nginx-1.12.2.tar.gz /usr/local/src
# RUN
RUN rm -rf /etc/yum.repos.d/*
ADD CentOS7-Base-163.repo /etc/yum.repos.d
RUN yum install wget gcc gcc-c++ make openssl-devel zlib-devel pcre-devel -y
RUN useradd -s /sbin/nologin -M www
# WORKDIR
WORKDIR /usr/local/src/nginx-1.12.2
RUN ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-pcre && make && make install
RUN echo "daemon off;" >>/usr/local/nginx/conf/nginx.conf
#ENV PATH /usr/local/nginx/sbin:$PATH
# EXPOST
EXPOSE 80
CMD ["/usr/local/nginx/sbin/nginx"]
然后来构建:
[root@node ~]# docker build -t nginx-file:v1 /opt/docker-file/nginx/
...
这中间内容太多
Removing intermediate container 2b71cba52a7a
Step 11/12 : EXPOSE 80
---> Running in 0405f2f283f9
---> 858ab9af7659
Removing intermediate container 0405f2f283f9
Step 12/12 : CMD /usr/local/nginx/sbin/nginx
---> Running in 83e8ed856aee
---> 592d69098f3a
Removing intermediate container 83e8ed856aee
Successfully built 592d69098f3a
[root@node ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx-file v1 592d69098f3a 17 hours ago 399 MB
sijiayong/my-nginx v1 a80ecf40e4d2 19 hours ago 328 MB
docker.io/centos latest 5182e96772bf 2 weeks ago 200 MB
docker.io/nginx latest c82521676580 4 weeks ago 109 MB
[root@node nginx]# docker run -d -it --name nginx-test -h nginx -p 80:80 nginx-file:v1
8d632d8079a4d72f0034a4982c84997b321e082c7bb5a453fed7ebeb2a3e7e3d
[root@node nginx]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8d632d8079a4 nginx-file:v1 "/usr/local/nginx/..." 4 seconds ago Up 4 seconds 0.0.0.0:80->80/tcp nginx-test
[root@node nginx]# cd
[root@node ~]# ./in.sh nginx-test
[root@nginx /]# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.2 0.1 45944 5580 ? Ss+ 10:02 0:00 nginx: master process /usr/local/nginx/sbin/nginx
www 6 0.0 0.0 46380 3544 ? S+ 10:02 0:00 nginx: worker process
root 7 0.1 0.0 15264 3268 ? S 10:02 0:00 -bash
root 20 0.0 0.0 55152 3936 ? R+ 10:02 0:00 ps aux
查看