玖叶教程网

前端编程开发入门

Dockerfile 构建nginx images

将需要的包直接放到工作目录下

# pwd
/root/docker
node2 root@node2:~/docker/nginx# pwd
/root/docker/nginx
node2 root@node2:~/docker/nginx# ls
Dockerfile  nginx-1.14.1.tar.gz  nginx.conf
Dockerfile 内容如下:

注意:Dockerfile文件中不好包含空行,可能会warning

当然也可以在线wget需要的软件包

[WARNING]: Empty continuation lines will become errors in a future release

node2 root@node2:~/docker/nginx# cat Dockerfile
FROM centos:7
MAINTAINER [email protected]
RUN yum install -y gcc gcc-c++ make \
    openssl-devel pcre-devel gd-devel \
    iproute net-tools telnet wget curl && \
    yum clean all && \
    rm -rf /var/cache/yum/*
#RUN wget http://nginx.org/download/nginx-1.14.1.tar.gz && \
RUN mkdir /var/tmp/nginx
ADD nginx-1.14.1.tar.gz /var/tmp/nginx/
RUN cd /var/tmp/nginx && \
    #tar zxf nginx-1.14.1.tar.gz && \
    cd nginx-1.14.1 &&\
    ./configure --prefix=/usr/local/nginx \
    --with-http_ssl_module \
    --with-http_realip_module \
    --with-threads \
    --with-stream \
    --with-stream_ssl_preread_module \
    --with-stream_ssl_module \
    --with-http_stub_status_module && \
    make -j 4 && make install && \
    rm -rf /usr/local/nginx/html/* && \
    echo "hello world" >> /usr/local/nginx/html/status.html && \
    cd / && rm -rf nginx-1.14.1*
ENV PATH $PATH:/usr/local/nginx/sbin
COPY nginx.conf /usr/local/nginx/conf/nginx.conf
WORKDIR /usr/local/nginx
EXPOSE 443
CMD ["nginx", "-g", "daemon off;"]

node2 root@node2:~# docker build -t centos:nginx /root/docker/nginx/

一堆命令之后:

Successfully built 5e2cf50a9378

Successfully tagged centos:nginx

查看我构建的这个images

node2 root@node2:~# docker images
REPOSITORY                          TAG                     IMAGE ID            CREATED             SIZE
centos                              nginx                   5e2cf50a9378        4 minutes ago       402MB

尝试启动进去看看:

-i, --interactive Keep STDIN open even if not attached

-t, --tty Allocate a pseudo-TTY

-d, --detach Run container in background and print container ID

-p, --publish list Publish a container's port(s) to the host

--name string Assign a name to the container

docker run -itd -p 443:443 --name=fpnginx centos:nginx
# docker run -itd -p 443:443 --name=fpnginx centos:nginx
fe06dd0689a2154f9ca48273486884b23fb0eee315baca66bb4224ecbd9cab75
node2 root@node2:~# docker ps
CONTAINER ID        IMAGE                                COMMAND                  CREATED             STATUS                          PORTS                  NAMES
fe06dd0689a2        centos:nginx                         "nginx -g 'daemon of…"   2 minutes ago       Up 2 minutes                    0.0.0.0:443->443/tcp   fpnginx


# docker exec --help

Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

Run a command in a running container

Options:

-d, --detach Detached mode: run command in the background

--detach-keys string Override the key sequence for detaching a container

-e, --env list Set environment variables

-i, --interactive Keep STDIN open even if not attached

--privileged Give extended privileges to the command

-t, --tty Allocate a pseudo-TTY

-u, --user string Username or UID (format: <name|uid>[:<group|gid>])

-w, --workdir string Working directory inside the container


登录进去测试看看

node2 root@node2:~/docker/nginx# docker exec -it fe06dd0689a2 /bin/bash
[root@fe06dd0689a2 nginx]# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.1  46080  3288 pts/0    Ss+  09:37   0:00 nginx: master process nginx -g daemon off;
nobody       6  0.0  0.1  46540  1892 pts/0    S+   09:37   0:00 nginx: worker process
root         7  0.0  0.1  11828  1884 pts/1    Ss   09:42   0:00 /bin/bash
root        20  0.0  0.0  51732  1720 pts/1    R+   09:42   0:00 ps aux
[root@fe06dd0689a2 nginx]# netstat -nltp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      1/nginx: master pro

[root@fe06dd0689a2 nginx]# cat /usr/local/nginx/conf/nginx.conf

worker_processes  1;
error_log  logs/error.log  info;
events {
    worker_connections  1024;
}

stream {
    log_format  main '$remote_addr [$time_local] '
                     'protocol $status $bytes_sent $bytes_received '
                     '$session_time '
                     'ssl_preread_server_name $server_port ';
    resolver 114.114.114.114;
    server {
        listen 443;
        ssl_preread on;
        proxy_connect_timeout 5s;
        proxy_pass $ssl_preread_server_name:$server_port;
    access_log /usr/local/nginx/logs/proxy-access.log main;
    }
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
}  


绑定hosts做测试:

[root@fe06dd0689a2 ~]# tail -n 1 /etc/hosts

172.17.0.2 www.baidu.com

请求之后,查看日志

[root@fe06dd0689a2 ~]# curl -vs https://www.baidu.com

[root@fe06dd0689a2 ~]# tail -f /usr/local/nginx/logs/proxy-access.log

172.17.0.2 [24/Apr/2021:09:44:26 +0000] protocol 200 4599 264 0.348 ssl_preread_server_name 443

172.17.0.2 [24/Apr/2021:09:46:01 +0000] protocol 200 7110 263 0.345 ssl_preread_server_name 443

node2 root@node2:~/docker/nginx# docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

centos nginx 5e2cf50a9378 26 minutes ago 402MB

以推送到对应的地方

docker push

指令简介参考:

指令的一般格式为INSTRUNCTION arguments,指令包括FROM、MAINTAINER、RUN等。具体指令及说明如下:

指令

说明

FROM

指定所创建镜像的基础镜像

MAINTAINER

指定维护者信息

RUN

运行命令

CMD

指定启动容器时默认执行的命令

LABEL

指定生成镜像的元数据标签信息

EXPOSE

声明镜像内服务所监听的端口

ENV

指定环境变量

ADD

复制指定的<src>路径下的内容到容器中的<dest>路径下,<src>可以为URL;如果为tar文件,会自动解压到<dest>路径下

COPY

复制本地主机的<scr>路径下的内容到容器中的<dest>路径下;一般情况下推荐使用COPY而不是ADD

ENTRYPOINT

指定镜像的默认入口

VOLUME

创建数据挂载点

USER

指定运行容器时的用户名或UID

WORKDIR

配置工作目录

ARG

指定镜像内使用的参数(例如版本号信息等)

ONBUILD

配置当前所创建的镜像作为其他镜像的基础镜像时,所执行的创建操作的命令

STOPSIGNAL

容器退出的信号

HEALTHCHECK

如何进行健康检查

SHELL

指定使用SHELL时的默认SHELL类型

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言