1. 程式人生 > >一個包含了 java環境,mysql,nginx,redis docker 映象

一個包含了 java環境,mysql,nginx,redis docker 映象

  • 1.目錄結構
    這裡寫圖片描述

  • 2.softwares 目錄下的軟體
    這裡寫圖片描述

  • 3.編譯
# 進入到當前目錄
docker build -t app:web .
  • 4.執行映象
docker run --name web -p 1022:22 -p 13306:3306 -p 80:80 -p 16379:6379 -v /data/mysql:/data/mysql app:web
  • 5.Dockerfile
FROM centos:centos7
MAINTAINER ztd "[email protected]"
# 1.準備工作建立資料夾
RUN \
    mkdir -p /opt/tools \
&& mkdir -p /etc/redis \ && mkdir /opt/logs \ && mkdir -p /data/mysql # 複製檔案 COPY softwares/jdk-8u102-linux-x64.tar.gz /opt/tools COPY softwares/redis-3.2.8.tar.gz /opt/tools COPY softwares/apache-tomcat-7.0.70.tar.gz /opt/tools COPY redis.conf /etc/redis/redis.conf COPY supervisord.conf /etc/supervisord.conf COPY program.conf /etc/program.conf #
複製資料庫檔案 COPY softwares/libaio-0.3.107-10.el6.x86_64.rpm /opt/tools COPY softwares/MySQL-client-5.6.23-1.rhel5.x86_64.rpm /opt/tools COPY softwares/MySQL-devel-5.6.23-1.rhel5.x86_64.rpm /opt/tools COPY softwares/MySQL-server-5.6.23-1.rhel5.x86_64.rpm /opt/tools # 複製nginx安裝檔案 COPY softwares/nginx-1.13.7.tar.gz /opt/tools #
COPY nginx.conf /opt/tools/nginx.conf # 安裝 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 \ && cd /opt/tools \ && tar -xzf redis-3.2.8.tar.gz \ && rm -rf redis-3.2.8.tar.gz \ && cd redis-3.2.8 && yum -y install tcl && make && make install \ && cd /opt/tools \ && rm -rf redis-3.2.8 \ && rm -rf /var/cache/yum/* # 安裝mysql RUN \ yum -y install perl perl-devel perl-Module-Install.noarch net-tools \ && cd /opt/tools \ && rpm -ivh libaio-0.3.107-10.el6.x86_64.rpm \ && rpm -ivh MySQL-server-5.6.23-1.rhel5.x86_64.rpm \ && rpm -ivh MySQL-client-5.6.23-1.rhel5.x86_64.rpm \ && rpm -ivh MySQL-devel-5.6.23-1.rhel5.x86_64.rpm \ && chown -R mysql:mysql /data/mysql \ && /etc/init.d/mysql start \ && mysqltmppwd=`cat /root/.mysql_secret | cut -b 87-102` \ && mysqladmin -u root -p${mysqltmppwd} password "123456" \ && mysql -uroot -p123456 -e"grant all privileges on *.* to [email protected]'%' identified by '123456' with grant option" \ && mysql -uroot -p123456 -e"flush privileges" \ && /etc/init.d/mysql stop \ && rm -rf libaio-0.3.107-10.el6.x86_64.rpm \ && rm -rf MySQL-client-5.6.23-1.rhel5.x86_64.rpm \ && rm -rf MySQL-devel-5.6.23-1.rhel5.x86_64.rpm \ && rm -rf MySQL-server-5.6.23-1.rhel5.x86_64.rpm \ && rm -rf /var/cache/yum/* \ && sed -i -e "10a datadir = /data/mysql" /usr/my.cnf # 安裝 nginx RUN \ yum -y install gcc-c++ zlib zlib-devel openssl openssl--devel pcre pcre-devel \ && cd /opt/tools \ && tar -zxv -f nginx-1.13.7.tar.gz \ && rm -rf nginx-1.13.7.tar.gz \ && cd nginx-1.13.7 \ && ./configure --with-http_stub_status_module \ && make && make install \ && cd .. \ && rm -rf nginx-1.13.7 \ && rm -rf /var/cache/yum/* # && echo "daemon off" >> /usr/local/nginx/conf/nginx.conf # && cp -rf /opt/tools/nginx.conf /usr/local/nginx/conf/nginx.conf # 安裝supervisor RUN \ yum -y install python-setuptools \ && easy_install supervisor \ && rm -rdf /var/cache/yum/* # 設定java環境變數 RUN \ cd /opt/tools \ && tar -zxvf jdk-8u102-linux-x64.tar.gz \ && rm -rf jdk-8u102-linux-x64.tar.gz \ && tar -zxvf apache-tomcat-7.0.70.tar.gz \ && rm -rf apache-tomcat-7.0.70.tar.gz \ && echo 'export JAVA_HOME=/opt/tools/jdk1.8.0_102' >> /etc/profile \ && echo 'export PATH=$JAVA_HOME/bin:$PATH' >> /etc/profile \ && echo 'export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar' >> /etc/profile \ && source /etc/profile # 設定環境變數 EXPOSE 22 80 3306 6379 CMD /usr/bin/supervisord -c /etc/supervisord.conf #CMD /usr/sbin/init
  • 6.program.conf
[program:mysql]
command=mysqld --user=mysql

[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]
tartsecs=50
command=/usr/local/bin/redis-server /etc/redis/redis.conf
  • 7.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
  • 8.supervisord.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 = relative/directory/*.ini

[include]
files = /etc/program.conf

注意:如果你的映象生成之後,使用 supervisorctl 出現如下錯誤:
supervisorctl unix ///tmp/supervisor.sock refused connection
請看這篇文章:
supervisorctl unix ///tmp/supervisor.sock refused connection