在百度上试很多文章都不行,只有这篇可以。
宿主机为:centos64位
//安装docker
1:yum install docker
//启动docker
2:systemctl start docker.service
3:新建web目录
mkdir test/web
4:上传tomcat和jdk到该目录(Web)下。
5:配置Dockerfile :vim Dockerfile,然后复制下面的代码到Dockerfile中,保存。记得相应修改tomcat和jdk名
#pull down centos image FROM centos MAINTAINER test@test.com #copy jdk and tomcat into image ADD ./apache-tomcat-7.0.70.tar.gz /root ADD ./jdk-7u80-linux-x64.tar.gz /root #set environment variable ENV JAVA_HOME /root/jdk1.7.0_80 ENV PATH $JAVA_HOME/bin:$PATH #define entry point which will be run first when the container starts up ENTRYPOINT /root/apache-tomcat-7.0.70/bin/startup.sh && tail -F /root/apache-tomcat-7.0.70/logs/catalina.out
6构建镜像
[root@localhost web]# docker build -t keven/centos:tomcat-centos --rm=true . Sending build context to Docker daemon 507.1 MB Step 1 : FROM centos Trying to pull repository docker.io/library/centos ... latest: Pulling from docker.io/library/centos 785fe1d06b2d: Pull complete Digest: sha256:be5b4a93f116a57ab3fd454ada72421eac892a3a4925627ac9a44f65fcd69cf8 ---> 98d35105a391 Step 2 : MAINTAINER 27381011@qq.com ---> Running in 6c49ddc87de9 ---> 717ea8232a68 Removing intermediate container 6c49ddc87de9 Step 3 : ADD ./apache-tomcat-8.5.11.tar.gz /root ---> 79dee4e0968b Removing intermediate container b7fd20fb27d4 Step 4 : ADD ./jdk-8u11-linux-x64.tar.gz /root ---> f9e00a11709f Removing intermediate container 3e1d1cd9705f Step 5 : ENV JAVA_HOME /root/jdk1.8.0_11 ---> Running in b2296ac1b856 ---> 67ea9916ce46 Removing intermediate container b2296ac1b856 Step 6 : ENV PATH $JAVA_HOME/bin:$PATH ---> Running in 904ef55db8dc ---> a32596709d8e Removing intermediate container 904ef55db8dc Step 7 : ENTRYPOINT /root/apache-tomcat-8.5.11/bin/startup.sh && tail -F /root/apache-tomcat-8.5.11/logs/catalina.out ---> Running in 959ccd7993b0 ---> 41ab74750331 Removing intermediate container 959ccd7993b0 Successfully built 41ab74750331
7查看新产生的镜像
[root@localhost web]# docker images keven/centos REPOSITORY TAG IMAGE ID CREATED SIZE keven/centos tomcat-centos 41ab74750331 5 minutes ago 529.6 MB
8运行镜像
docker run -d -p 8090:8080 5207
-p指定主机80端口与容器8080端口进行绑定
-d 指定容器运行后与当前tty分离,后台运行
5207是镜像的ID前4位。
通过 http://宿主机IP:8090,即可看见我们熟悉的tomcat首页了。