了解docker已经有段时间了,准备用docker来替换掉之前的环境
之前的环境用到了:jdk,tomcat,mysql,nginx ,一个nginx反向代理tomcat,tomcat会使用到mysql。
nginx,mysql都好安装,直接docker pull <镜像>就可以了
jdk,tomcat是自己做的Dockerfile
jdk的如下:
FROM debian
MAINTAINER h2g
COPY ./jdk-8u121-linux-x64.tar.gz $HOME/
RUN cat /etc/debian_version
#更新apt-get源 使用163的源
RUN mv /etc/apt/sources.list /etc/apt/sources.list.bak && \
echo "deb http://mirrors.163.com/debian/ jessie main non-free contrib" >/etc/apt/sources.list && \
echo "deb http://mirrors.163.com/debian/ jessie-proposed-updates main non-free contrib" >>/etc/apt/sources.list && \
echo "deb-src http://mirrors.163.com/debian/ jessie main non-free contrib" >>/etc/apt/sources.list && \
echo "deb-src http://mirrors.163.com/debian/ jessie-proposed-updates main non-free contrib" >>/etc/apt/sources.list
RUN apt-get update \
&& apt-get install -y curl
RUN find / -name jdk-8u121-linux-x64.tar.gz
RUN mkdir -p /usr/lib/jvm
RUN tar -zxf /jdk-8u121-linux-x64.tar.gz -C /usr/lib/jvm
ENV JAVA_HOME /usr/lib/jvm/jdk1.8.0_121
ENV PATH=$PATH:$JAVA_HOME/bin/:
RUN java -version
自己下载jdk-8u121-linux-x64.tar.gz,然后copy到容器。
docker里面大部分都使用了debian,所以也跟风试了试,本来是准备做个base的Dockerfile,在里面配置环境及安装些工具(比如curl)的,主要是java开发,所以直接放在jdk的file里了
build下:docker build --tag=debian/jdk:8u121 --rm=true .
tomcat的如下:
FROM debian/jdk:8u121
# Set the WILDFLY_VERSION env variable
ENV TOMCAT_VERSION 8.5.12
ENV TOMCAT_SHA1 acf6d327504e5d1b89678b25a061acd7b5c6dac3
ENV TOMCAT_HOME /opt/tomcat
USER root
RUN cd $HOME \
&& mkdir -p /opt/ \
&& mkdir -p /llogs/ \
&& curl -O http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v$TOMCAT_VERSION/bin/apache-tomcat-$TOMCAT_VERSION.tar.gz \
&& sha1sum apache-tomcat-$TOMCAT_VERSION.tar.gz | grep $TOMCAT_SHA1
RUN tar -xzf $HOME/apache-tomcat-$TOMCAT_VERSION.tar.gz
RUN rm $HOME/apache-tomcat-$TOMCAT_VERSION.tar.gz
RUN mv apache-tomcat-$TOMCAT_VERSION $TOMCAT_HOME
RUN find / -name catalina.sh
RUN java -version
RUN chmod -R 755 $TOMCAT_HOME/bin/*.sh
EXPOSE 8080
VOLUME ["/opt/tomcat/logs","/llogs"]
CMD ["/opt/tomcat/bin/catalina.sh", "run"]
编辑Dockerfile最大的问题,就是总是找不到文件,所以加上了find / -name xxx用来调试
也build下:docker build --tag=debian/jdk/tomcat:8.5.12 --rm=true .
工程部署,我是使用了重新写个Dockerfile,如下:
FROM debian/jdk/tomcat:8.5.12
ADD hello.war /opt/tomcat/webapps
直接把war工程部署到tomcat目录下
也build下:docker build --tag=tomcat-hello .
运行这个docker
docker run -d --name=tomcat-1 tomcat-hello
通过这个命令:docker inspect --format '{{ .NetworkSettings.IPAddress }}' <容器ID>,可以查看容器的ip,但容器IP是分配的,可能会有改变,所以还是使用--link参数。
nginx也是这样配置,新建个Dockerfile
FROM nginx
RUN mkdir -p /etc/nginx/logs
COPY nginx.conf /etc/nginx/nginx.conf
VOLUME ["/etc/nginx/logs"]
# PORT
EXPOSE 15001
nginx.conf如下:
user root;
worker_processes 16;
worker_rlimit_nofile 100000;
error_log logs/error.log debug;
pid logs/nginx.pid;
events {
use epoll;
worker_connections 65535;
}
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"';
access_log logs/access.log main;
sendfile on;
keepalive_timeout 65;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
upstream test1 {
server tomcat-1:8080;
}
server {
listen 15001;
server_name 113.x.x.x;
charset UTF-8;
access_log logs/test1.access.log main;
client_header_buffer_size 128k;
large_client_header_buffers 4 128k;
location / {
proxy_pass http://test1/;
proxy_redirect off;
proxy_set_header Host $host:$server_port;
proxy_set_header real_ip_header X-Real-IP;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Refer $http_referer;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
}
}
打包并且运行下:
docker build --tag=my-ngx .
docker run --name=ngx --link=tomcat-1:tomcat-1 -p=15001:15001 -d my-ngx
使用--link来关联其他容器,-p来开启宿主机的端口。
就可以直接访问了。
但还有个疑问,通过 firewall-cmd --list-all命令,发现端口并没有开放,但是外网可以访问,难道是用的iptables?
我们一般nginx都是方向代理多个tomcat,现在run个tomcat
docker run -d --name=tomcat-2 tomcat-hello
nginx的Dockerfile下要增加个端口
# PORT
EXPOSE 15001
EXPOSE 15002
nginx.conf也要增加点配置
...
upstream test1 {
server tomcat-1:8080;
}
upstream test2 {
server tomcat-2:8080;
}
...
server {
listen 15002;
server_name 113.x.x.x;
charset UTF-8;
access_log logs/test2.access.log main;
client_header_buffer_size 128k;
large_client_header_buffers 4 128k;
location / {
proxy_pass http://test2/;
proxy_redirect off;
proxy_set_header Host $host:$server_port;
proxy_set_header real_ip_header X-Real-IP;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Refer $http_referer;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
}
}
重新打包,并且run下
docker run --name=ngx --link=tomcat-1:tomcat-1 --link=tomcat-2:tomcat-2 -p=15001:15001 -p=15002:15002 -d my-ngx
使用多个--link,-p来做关联。
如果还要反向代理更多的tomcat,这样写其不会疯掉,所以准备用docker-compose来管理下
可以使用external_links来连接不再管理范围内的容器
先做个docker-compose.yml
version: '2'
services:
ngx:
image: my-ngx
ports:
- "15001:15001"
- "15002:15002"
networks:
- hhhh
external_links:
- tomcat-1
- tomcat-2
networks:
hhhh:
driver: bridge
[root@localhost nginx]# docker-compose up -d
Creating network "nginx_hhhh" with driver "bridge"
会创建一个nginx_hhhh的网,然后我们修改下之前的tomcat
docker run -d --name=tomcat-1 --net=nginx_hhhh tomcat-demo-1
这样在同一网络内,就能与外部创建的容器link起来,使之可以通信。