docker 使用

瞭解docker已經有段時間了,準備用docker來替換掉以前的環境javascript

以前的環境用到了:jdk,tomcat,mysql,nginx ,一個nginx反向代理tomcat,tomcat會使用到mysql。css

nginx,mysql都好安裝,直接docker pull <鏡像>就能夠了java

jdk,tomcat是本身作的Dockerfilemysql

jdk的以下:linux

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到容器。nginx

docker裏面大部分都使用了debian,因此也跟風試了試,原本是準備作個base的Dockerfile,在裏面配置環境及安裝些工具(好比curl)的,主要是java開發,因此直接放在jdk的file裏了web

build下:docker build --tag=debian/jdk:8u121 --rm=true .sql

tomcat的以下:docker

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用來調試apache

也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起來,使之能夠通訊。

相關文章
相關標籤/搜索