环境

参考: 离线安装docker及docker-compose

安装centos 7 镜像

通过可连网的Linux执行: docker pull centos:centos7 
当前centos镜像打jar包: docker save -o centos.tar centos:centos7
传入未联网linux中:docker load < centos.tar
查看镜像:docker images

自行构建jdk1.8 镜像

下载 jdk-8u161-linux-x64.tar
tar -zxvf jdk-8u161-linux-x64.tar 查看解压后目录 (jdk1.8.0_161)
mkdir /usr/local/jdk
mv jdk-8u161-linux-x64.tar  /usr/local/jdk
cd /usr/local/jdk
vim Dockerfile
FROM centos:centos7
MAINTAINER tom
RUN mkdir /usr/local/jdk (如果存在该目录不需执行此步)
WORKDIR /usr/local/jdk
ADD jdk-8u161-linux-x64.tar /usr/local/jdk

ENV JAVA_HOME /usr/local/jdk/jdk1.8.0_161
ENV JRE_HOME /usr/local/jdk/jdk1.8.0_161/jre
ENV PATH $JAVA_HOME/bin:$PATH
docker build -t jdk:1.8 .
docker images 查看是否构建成功

部署web项目

mkdir demo
导入demo.jar包
vim Dockerfile
FROM jdk:1.8
VOLUME /tmp
ADD portfolio-overview-1.1.18.jar /app.jar
ENTRYPOINT ["java","-jar","-server","-Xms1024m","-Xmx1024m","-XX:+HeapDumpOnOutOfMemoryError","-verbose:gc","-XX:+PrintGCDetails","-XX:+PrintGCDateStamps","-Xloggc:/var/log/chinaamc/gc-portfolio-overview.log","-Dspring.profiles.active=test","/app.jar"]

docker build -t demo .
docker images 查看是否构建成功
docker run -d -p8085:8085 demo
docker ps  查看是否启动成功

docker-compose部署zookeeper

拉取zookeeper镜像
vim docker-compose.yml
version: '2'
services:
    zoo1:
        image: zookeeper:3.4
        restart: always
        container_name: zoo1
        ports:
            - "2181:2181"
        environment:
            ZOO_MY_ID: 1
            ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888
 
    zoo2:
        image: zookeeper:3.4
        restart: always
        container_name: zoo2
        ports:
            - "2182:2181"
        environment:
            ZOO_MY_ID: 2
            ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888
 
    zoo3:
        image: zookeeper:3.4
        restart: always
        container_name: zoo3
        ports:
            - "2183:2181"
        environment:
            ZOO_MY_ID: 3
            ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888

docker-compose up -d  启动zookeeper集群
docker ps 

Tomcat部署war包

2.1:搜索tomcat镜像

docker search tomcat

2.2:根据搜索出来的列表下载tomcat

下载命令
docker pull tomcat

2.3 linux离线时无法使用docker pull 时
mkdir /usr/local/tomcat
需离线下载 apache-tomcat-8.5.69.tar.gz
需离线下载 jdk-8u161-linux-x64.tar.gz
tar -zxvf apache-tomcat-8.5.69.tar.gz
tar -zxvf jdk-8u161-linux-x64.tar.gz

mv apache-tomcat-8.5.69 tomcat8
mv jdk1.8.0_161 jdk18
vim Dockefile
FROM centos:centos7
MAINTAINER tom
#创建目录
RUN mkdir -p /docker_home/local
#把当前目录下的jdk文件夹添加到镜像 ADD可以自动解压tar文件,但是无法解压tar.gz,所以上面手动tar -zxvf 解压tar.gz文件
ADD tomcat8 /docker_home/local/tomcat8
ADD jdk18 /docker_home/local/jdk18
ENV JAVA_HOME /docker_home/local/jdk18/
ENV CATALINA_HOME /docker_home/local/tomcat8
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/bin
#暴露8082端口
EXPOSE 8082
#启动时运行tomcat
CMD ["/docker_home/local/tomcat8/bin/catalina.sh","run"]

2.4 构建tomcat镜像
docker build -t tomcat:8.0 .
2.5 启动tomcat
docker run -d -p 8099:8080 tomcat:8

此时通过页面访问localhost:8099 可以看到tomcat页面

2.6 启动war包
docker cp /usr/local/docker-tomcat 9fccf0236619:/usr/local/tomcat/webapps
重启tomcat
docker restart [container Id]

此时由于war包在tomcat容器内部,所以每次重启linux时会导致容器内部war包消失,所以最好采用外挂目录方式启动容器

2.7 外挂目录启动tomcat容器
docker run -d -p 8099:8080 --name tomcat -v /usr/local/docker-tomcat:/usr/local/tomcat/webapps --restart=always tomcat:8
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐