基于centos生成docker镜像

创建基础镜像,用于提供一个集成jdk、redis及nginx的centos环境

Dockerfile文件

# 基础镜像,这里选用centos
FROM centos:7
# 镜像所有者
MAINTAINER xiaoyu "xiaoyu@123.com"
# 1.准备工作创建文件夹
RUN \
    mkdir -p /opt/tools \
    && mkdir -p /etc/redis \
    && mkdir /opt/logs 

# 复制文件,将要安装的jdk、redis及nginx安装包复制到镜像内
COPY softwares/jdk-8u191-linux-x64.tar.gz /opt/tools
COPY softwares/redis-5.0.5.tar.gz /opt/tools
COPY redis.conf /etc/redis/redis.conf
COPY supervisord.conf /etc/supervisord.conf

# 复制nginx安装文件
COPY softwares/nginx-1.16.1.tar.gz /opt/tools
#COPY nginx.conf /opt/tools/nginx.conf

#安装vim编辑器
RUN yum -y install vim

#安装gcc等编译环境
RUN yum -y install gcc automake autoconf libtool make

# 安装 sshd 修改密码
RUN \
    yum install passwd openssl openssh-server -y \
    && ssh-keygen -q -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N '' \
    && ssh-keygen -q -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N '' \
    && ssh-keygen -t dsa -f /etc/ssh/ssh_host_ed25519_key  -N '' \
    && sed -i "s/#UsePrivilegeSeparation.*/UsePrivilegeSeparation no/g" /etc/ssh/sshd_config \
    && sed -i "s/UsePAM.*/UsePAM no/g" /etc/ssh/sshd_config \
    && echo 123456 | passwd --stdin root \
    && echo root:123456|chpasswd \
    && rm -rf /var/cache/yum/**
	
# 安装redis
RUN \
    yum install gcc -y \
    && yum install tcl -y \
    && cd /opt/tools \
    && tar -zxvf redis-5.0.5.tar.gz \
    && rm redis-5.0.5.tar.gz \
    && cd /opt/tools/redis-5.0.5 \
    && make && make install \
    && cd /opt/tools \ 
    && rm -rf /var/cache/yum/*
	
# 安装 nginx
RUN \
    yum install -y gcc-c++ \
    && yum install -y pcre pcre-devel \
    && yum install -y zlib zlib-devel \
    && yum install -y openssl openssl-devel \
    && cd /opt/tools \
    && tar -zxvf nginx-1.16.1.tar.gz \
    && rm -rf nginx-1.16.1.tar.gz \
    && cd nginx-1.16.1 \
    && ./configure --with-http_stub_status_module \
    && make && make install \
    && cd .. \
    && rm -rf nginx-1.16.1 


# 安装supervisor,这个工具会提供一个主线程,避免出现由于后台应用无法启动而造成镜像无法启动
RUN \
    yum install -y python-setuptools \
    && easy_install supervisor 
	
#清除yum缓存
RUN \
	rm -rdf /var/cache/yum/**

# 设置java环境变量
RUN \
    cd /opt/tools \
        && tar -zxvf jdk-8u191-linux-x64.tar.gz \
        && rm -rf jdk-8u191-linux-x64.tar.gz 

#配置jdk环境变量
ENV JAVA_HOME=/opt/tools/jdk1.8.0_191
ENV CLASSPATH=$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV PATH=$PATH:$JAVA_HOME/bin
#配置系统编码
ENV LANG en_US.utf8

#CMD /usr/bin/supervisord -c /etc/supervisord.conf
#CMD /usr/sbin/init


redis配置文件(redis.conf)

# 所有ip均可以访问
bind 0.0.0.0
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
# 如果这个 redis 是让 supervisorctl 来管理的,那么这个地方就需要设置为no,如果是yes的话就无法启动了
daemonize no
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile ""
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100

appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes

supersupervisord.conf

[unix_http_server]
file=/tmp/supervisor.sock   ; (the path to the socket file)
;chmod=0700                 ; socket file mode (default 0700)
;chown=nobody:nogroup       ; socket file uid:gid owner
;username=user              ; (default is no username (open server))
;password=123               ; (default is no password (open server))

;[inet_http_server]         ; inet (TCP) server disabled by default
;port=127.0.0.1:9001        ; (ip_address:port specifier, *:port for all iface)
;username=user              ; (default is no username (open server))
;password=123               ; (default is no password (open server))

[supervisord]
#logfile=/opt/logs/supervisord.log ; (main log file;default $CWD/supervisord.log)
#logfile_maxbytes=2MB        ; (max main logfile bytes b4 rotation;default 50MB)
#logfile_backups=2            ; (num of main logfile rotation backups;default 10)
#loglevel=info                ; (log level;default info; others: debug,warn,trace)
pidfile=/tmp/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
nodaemon=true               ; (start in foreground if true;default false)
minfds=1024                  ; (min. avail startup file descriptors;default 1024)
minprocs=200                 ; (min. avail process descriptors;default 200)
;umask=022                   ; (process file creation umask;default 022)
;user=chrism                 ; (default is current user, required if root)
;identifier=supervisor       ; (supervisord identifier, default is 'supervisor')
;directory=/tmp              ; (default is not to cd during start)
;nocleanup=true              ; (don't clean up tempfiles at start;default false)
;childlogdir=/tmp            ; ('AUTO' child log dir, default $TEMP)
;environment=KEY="value"     ; (key value pairs to add to environment)
;strip_ansi=false            ; (strip ansi escape codes in logs; def. false)

; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL  for a unix socket
;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket
;username=chris              ; should be same as http_username if set
;password=123                ; should be same as http_password if set
;prompt=mysupervisor         ; cmd line prompt (default "supervisor")
;history_file=~/.sc_history  ; use readline history if available

; The below sample program section shows all possible program subsection values,
; create one or more 'real' program: sections to be able to control them under
; supervisor.

;[program:theprogramname]
;command=/bin/cat              ; the program (relative uses PATH, can take args)
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
;numprocs=1                    ; number of processes copies to start (def 1)
;directory=/tmp                ; directory to cwd to before exec (def no cwd)
;umask=022                     ; umask for process (default None)
;priority=999                  ; the relative start priority (default 999)
;autostart=true                ; start at supervisord start (default: true)
;autorestart=unexpected        ; whether/when to restart (default: unexpected)
;startsecs=1                   ; number of secs prog must stay running (def. 1)
;startretries=3                ; max # of serial start failures (default 3)
;exitcodes=0,2                 ; 'expected' exit codes for process (default 0,2)
;stopsignal=QUIT               ; signal used to kill process (default TERM)
;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false             ; send stop signal to the UNIX process group (default false)
;killasgroup=false             ; SIGKILL the UNIX process group (def false)
;user=chrism                   ; setuid to this UNIX account to run the program
;redirect_stderr=true          ; redirect proc stderr to stdout (default false)
;stdout_logfile=/a/path        ; stdout log path, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
;stdout_logfile_backups=10     ; # of stdout logfile backups (default 10)
;stdout_capture_maxbytes=1MB   ; number of bytes in 'capturemode' (default 0)
;stdout_events_enabled=false   ; emit events on stdout writes (default false)
;stderr_logfile=/a/path        ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups=10     ; # of stderr logfile backups (default 10)
;stderr_capture_maxbytes=1MB   ; number of bytes in 'capturemode' (default 0)
;stderr_events_enabled=false   ; emit events on stderr writes (default false)
;environment=A="1",B="2"       ; process environment additions (def no adds)
;serverurl=AUTO                ; override serverurl computation (childutils)

; The below sample eventlistener section shows all possible
; eventlistener subsection values, create one or more 'real'
; eventlistener: sections to be able to handle event notifications
; sent by supervisor.

;[eventlistener:theeventlistenername]
;command=/bin/eventlistener    ; the program (relative uses PATH, can take args)
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
;numprocs=1                    ; number of processes copies to start (def 1)
;events=EVENT                  ; event notif. types to subscribe to (req'd)
;buffer_size=10                ; event buffer queue size (default 10)
;directory=/tmp                ; directory to cwd to before exec (def no cwd)
;umask=022                     ; umask for process (default None)
;priority=-1                   ; the relative start priority (default -1)
;autostart=true                ; start at supervisord start (default: true)
;autorestart=unexpected        ; whether/when to restart (default: unexpected)
;startsecs=1                   ; number of secs prog must stay running (def. 1)
;startretries=3                ; max # of serial start failures (default 3)
;exitcodes=0,2                 ; 'expected' exit codes for process (default 0,2)
;stopsignal=QUIT               ; signal used to kill process (default TERM)
;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false             ; send stop signal to the UNIX process group (default false)
;killasgroup=false             ; SIGKILL the UNIX process group (def false)
;user=chrism                   ; setuid to this UNIX account to run the program
;redirect_stderr=true          ; redirect proc stderr to stdout (default false)
;stdout_logfile=/a/path        ; stdout log path, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
;stdout_logfile_backups=10     ; # of stdout logfile backups (default 10)
;stdout_events_enabled=false   ; emit events on stdout writes (default false)
;stderr_logfile=/a/path        ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups        ; # of stderr logfile backups (default 10)
;stderr_events_enabled=false   ; emit events on stderr writes (default false)
;environment=A="1",B="2"       ; process environment additions
;serverurl=AUTO                ; override serverurl computation (childutils)

; The below sample group section shows all possible group values,
; create one or more 'real' group: sections to create "heterogeneous"
; process groups.

;[group:thegroupname]
;programs=progname1,progname2  ; each refers to 'x' in [program:x] definitions

; The [include] section can just contain the "files" setting.  This
; setting can list multiple files (separated by whitespace or
; newlines).  It can also contain wildcards.  The filenames are
; interpreted as relative to this file.  Included files *cannot*
; include files themselves.

#[include]
#files = etc/*.ini

[include]
files = /etc/program.conf

构建镜像

docker build -t 镜像名称:latest .

基于基础镜像,根据不同项目生成具体镜像

# 基础镜像,用上面生成的镜像
FROM 镜像名称:版本号

COPY program.conf /etc/program.conf
COPY nginx.conf /usr/local/nginx/conf/nginx.conf

#创建安放jar包及nginx配置文件的文件夹
RUN \
	mkdir -p /usr/local/project/jar \
	&& mkdir -p /usr/local/project/config \
	&& mkdir -p /usr/local/project/cdn
	
#移植jar包
ADD api.jar /usr/local/project/jar/

#更换docker时区,跟linux服务器保持一致
RUN \
	cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
	&& echo 'Asia/Shanghai' >/etc/timezone

ENV LANG en_US.utf8
EXPOSE 22 80
CMD /usr/bin/supervisord -c /etc/supervisord.conf 

nginx.conf

#user  nobody;
user root;
worker_processes  auto;

events {
    use epoll;
    worker_connections  51200;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
    log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
                        '$status $body_bytes_sent "$http_referer" \n'
                        '====http_user_agent= $http_user_agent \n'
			'====http_x_forwarded_for = $http_x_forwarded_for \n' 
			'====request_uri = $request_uri \n'
			'====real_script_name = $real_script_name \n'
			'====fastcgi_script_name = $fastcgi_script_name \n'
			'====server_name = $server_name \n'
			'====document_root = $document_root  \n'
			'====request_filename = $request_filename \n';
    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  600;
    client_header_timeout 120s;        #调大点
    client_body_timeout 120s;          #调大点
    client_max_body_size 100m;         #主要是这个参数,限制了上传文件大大小

    #gzip  on;
    
    location / {
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_pass http://api;
        }

    upstream api{
    	# 8080为后端接口
        server 127.0.0.1:8080 fail_timeout=0;
    }

}

program.conf

#[program:mysql]
#command=/etc/init.d/mysql start
#user=root

[program:nginx]
autostart=true
autorestart=true
command=/usr/local/nginx/sbin/nginx -g "daemon off;" -c /usr/local/nginx/conf/nginx.conf

[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL  for a unix socket

[program:sshd]
command=/usr/sbin/sshd -D
autostart=true
autorestart=true

[program:redis]
startsecs=50
command=/usr/local/bin/redis-server /etc/redis/redis.conf

[program:api]
directory=/usr/local/project/jar
command=java -jar api.jar --spring.profiles.active=ProServer &
autostart=true
autorestart=true
#startsecs=5

构建镜像

docker build -t api:latest .
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值