docker net core

Dockerfile自动构建镜像

2018-10-08  本文已影响20人  anlgo

1、环境

Docker swarm集群(192.168.3.204【controller】,192.168.3.202【node2】, 192.168.3.201【node1】)

基础镜像:centos

构建应用: nginx 1.14.0

2、操作

创建目录:

 mkdir -p /mnt/docker

创建dockerfile文件

cd /mnt/docker
touch Dockerfile

文件内容如下:

[root@controller docker]# cat Dockerfile 
# Base image
FROM centos


# MAINTAINER
MAINTAINER farmer


# ADD
ADD nginx-1.14.0.tar.gz /usr/local/src


# RUN
RUN yum -y install wget gcc gcc-c++ make pcre pcre-devel zlib zlib-devel openssl-devel
RUN useradd -s /sbin/nologin -M nginx


# WORKDIR
WORKDIR /usr/local/src/nginx-1.14.0


RUN ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --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
ENV PATH /usr/local/nginx/sbin:$PATH


# EXPOSE
EXPOSE 80


# CMD
# CMD /bin/sh -c 'nginx -g "daemon off;"'
CMD ["nginx"]

指令说明

FROM: 设置镜像使用的基础镜像

MAINTAINER:设置镜像的作者

CMD:设置容器的启动命令

LABEL:设置镜像的标签

EXPOSE:设置镜像暴露的端口

ENV:设置容器的环境变量

ADD:编译镜像时复制文件到镜像中

COPY:编译镜像时复制文件到镜像中

ENTRYPOINT:设置容器的入口程序

VOLUME:设置容器挂载卷

USER:设置运行RUN CMD ENTRYPOINT的用户名

WORKDIR:设置RUN CMD ENTRYPOINT COPY ADD指令的工作目录

ARG:设置编译镜像时加入的参数

ONBUILD:设置镜像的ONBUILD指令

STOPSIGNAL:设置容器退出的信息量

执行构建镜像:

cd /mnt/docker
docker build -t nginx:v1 .

构建完成查看镜像:

[root@controller docker]# docker images
REPOSITORY                 TAG                 IMAGE ID            CREATED                 SIZE
nginx                      v1                  c67185cd9c7c        About an hour ago   400 MB
centos                     latest              5182e96772bf        5 weeks ago         200 MB
nginx                      <none>              c82521676580        7 weeks ago         109 MB
registry                   latest              b2b03e9146e1        2 months ago        33.3 MB

设置Tag:

docker tag c67185cd9c7c 192.168.3.204:5000/nginx:v2

上传镜像:

[root@controller docker]# docker push 192.168.3.204:5000/nginx:v2
The push refers to a repository [192.168.3.204:5000/nginx]
d63b196c2272: Pushed 
a8d5b5942f8b: Pushed 
1e8da162852a: Pushed 
33b640181c2f: Pushed 
fac0ef479b31: Pushed 
1d31b5806ba4: Pushed 
v2: digest:     sha256:daae58d543ef42e59d2808e4bde0ad5cf877f5cdbd5d5b2a9536cd6a7a674c51 size: 1580

查看镜像:

[root@controller docker]# docker images
REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE
192.168.3.204:5000/nginx   v2                  c67185cd9c7c        About an hour ago       400 MB
nginx                      v1                  c67185cd9c7c        About an hour ago   400 MB
centos                     latest              5182e96772bf        5 weeks ago         200 MB
nginx                      <none>              c82521676580        7 weeks ago         109 MB
registry                   latest              b2b03e9146e1        2 months ago        33.3 MB

Swarm集群创建Service:

[root@controller docker]# docker service create --replicas 2 --network nginx --name CL -p 8000:80 192.168.3.204:5000/nginx:v2
7x3ts5iyfamrkvzsoqfayqnfi

查看服务:

[root@controller docker]# docker service ls
ID            NAME   MODE        REPLICAS  IMAGE
7x3ts5iyfamr  CL     replicated  0/2       192.168.3.204:5000/nginx:v2
pj3vhyfuvz7i  nginx  replicated  2/2       nginx:latest

稍等一会儿在查看:

[root@controller docker]# docker service ls
ID            NAME   MODE        REPLICAS  IMAGE
7x3ts5iyfamr  CL     replicated  2/2       192.168.3.204:5000/nginx:v2
pj3vhyfuvz7i  nginx  replicated  2/2       nginx:latest

查看运行的节点:

[root@controller docker]# docker service ps CL
ID            NAME  IMAGE                        NODE   DESIRED STATE  CURRENT       STATE           ERROR  PORTS
2kw03dx2b3iq  CL.1  192.168.3.204:5000/nginx:v2  node1  Running        Running 26 seconds ago         
3nmsmknudn9u  CL.2  192.168.3.204:5000/nginx:v2  node2  Running        Running 25 seconds ago

打开浏览器查看:

image

Nginx发布完成。

上一篇下一篇

猜你喜欢

热点阅读