DockerFile介绍
dockerfile是用来构建docker镜像的文件!命令参数脚本!
构建步骤:
- 编写一个dockerfile文件
- docker build 构建称为一个镜像
- docker run运行镜像
- docker push发布镜像(DockerHub 、阿里云仓库)
DockerFile构建过程
基础知识:
- 每个保留关键字(指令)都必须是大写字母
- 执行从上到下顺序执行
- /# 表示注释
- 每一个指令都会创建提交一个新的镜像层,并提交!
步骤:开发,部署,运维
-
DockerFile : 构建文件,定义了一切的步骤,源代码
-
DockerImages : 通过DokerFile构建生成的镜像,是最终发布和运行的产品
-
Docker容器:容器就是镜像运行起来提供服务器
1、DockerFile的指令
FROM
MAINTAINER
RUN
ADD
WORKDIR
VOLUME
EXPOSE
CMD
ENTRYPOINT
ONBUILD
COPY
ENV
2、案例
创建自己的镜像实例
FROM centos
MAINTAINER dainel<dlgnjupt&#64;qq.com>
ENV MYPATH /usr/local
WORKDIR $MYPATH
RUN yum -y install vim
RUN yum -y install net-tools
EXPOSE 80
CMD echo $MYPATH
CMD echo "-----end----"
CMD /bin/bash
docker build -f mydockerfile-centos -t mycentos:0.1 .
Successfully built 4af56313b71a
Successfully tagged mycentos:0.1
CMD 和 ENTRYPOINT区别
测试cmd
vim dockerfile-test-cmd
FROM centos
CMD ["ls","-a"]
docker build -f dockerfile-cmd-test -t cmdtest .
docker run f6a946a8a24e
docker run f6a946a8a24e -l
测试ENTRYPOINT
vim dockerfile-test-entrypoint
FROM centos
ENTRYPOINT ["ls","-a"]
docker build -f dockerfile-cmd-entrypoint -t entrypoint .
docker run a8dedd41d3d0
docker run a8dedd41d3d0 -l
3、案例tomcat镜像
准备镜像文件 tomcat压缩包&#xff0c;jdk压缩包
root&#64;vm:/home/whq/build/tomcat
apache-tomcat-10.0.4.tar.gz jdk-8u291-linux-x64.tar.gz readme.txt
编写dockerfile文件&#xff0c;官方命名Dockerfile&#xff0c;build会自动寻找这个文件&#xff0c;就不需要-f指定了&#xff01;
vim Dockerfile
FROM centos
MAINTAINER whq<ahuwhq&#64;163.com>
COPY readme.txt /usr/local/readme.txt
ADD jdk-8u291-linux-x64.tar.gz /usr/local
ADD apache-tomcat-10.0.4.tar.gz /usr/local
RUN yum -y install vim
ENV MYPATH /usr/local
WORKDIR $MYPATH
ENV JAVA_HOME /usr/local/jdk1.8.0_11
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-10.0.4
ENV CATALINA_BASH /usr/local/apache-tomcat-10.0.4
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
EXPOSE 8080
CMD /usr/local/apache-tomcat-10.0.4/bin/startup.sh && tail -F /usr/local/apache-tomcat-10.0.4/bin/logs/catalina.out
FROM centos
MAINTAINER whq<ahuwhq&#64;163.com>
COPY readme.txt /usr/local/readme.txt
ADD jdk-8u291-linux-x64.tar.gz /usr/local
ADD apache-tomcat-10.0.4.tar.gz /usr/local
RUN yum -y install vim
ENV MYPATH /usr/local
WORKDIR $MYPATH
ENV JAVA_HOME /usr/local/jdk1.8.0_291
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-10.0.4
ENV CATALINA_BASH /usr/local/apache-tomcat-10.0.4
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
EXPOSE 8080
CMD /usr/local/apache-tomcat-10.0.4/bin/startup.sh && tail -F /usr/local/apache-tomcat-10.0.4/bin/logs/catalina.out
docker build -t diytomcat .
docker run -d -p 9099:8080 --name ahuwhq01tomcat -v /home/whq/build/tomcat/test:/usr/local/apache-tomcat-10.0.4/webapps/test -v /home/whq/build/tomcat/tomcatlogs/:/usr/local/apache-tomcat-10.0.4/logs diytomcat
访问测试
一些出错的点
- curl: (7) Failed to connect to localhost port 8080: Connection refused
这里发现使用curl localhost:9099 和容器外部使用curl localhost:8080访问不了
解决方法&#xff1a;
1、查看java -version 如果是openjdk请卸载干净
sudo apt-get remove openjdk*
source /etc/profile
2、对应查看安装java8
java -version
3、查看Dockerfile文件的环境变量
首先进入容器ls查看jdk1.8.0_291&#xff0c;则将ENV JAVA_HOME /usr/local/jdk1.8.0_291
修改文件。
4、卸载容器镜像&#xff0c;然后在重新安装一遍就可以解决了。
此时在外网就可以访问apache了。
发布项目
cd test/
mkdir WEB-INF
cd WEB-INF/
vim web.xml
<?xml version&#61;"1.0" encoding&#61;"UTF-8"?>
<web-app xmlns:xsi&#61;"http://www.w3.org/2001/XMLSchema-instance" xmlns&#61;"http://java.sun.com/xml/ns/javaee" xmlns:web&#61;"http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation&#61;"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id&#61;"WebApp_ID" version&#61;"2.5">
</web-app>
cd ..
vim index.jsp
<%&#64; page language&#61;"java" contentType&#61;"text/html; charset&#61;UTF-8"
pageEncoding&#61;"UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset&#61;"utf-8">
<title>hello !</title>
</head>
<body>
Hello World!<br/>
<%
out.println("你的项目部署成功 ");
%>
</body>
</html>
ip:9099/test
产看日志&#xff1a;
cd ../tomcatlogs/
ls
cat catalina.out
发现&#xff1a;项目部署成功&#xff0c;可以直接访问&#xff01;
发布自己的镜像Dockerhub
1、地址 https://hub.docker.com/
2、确定这个账号可以登录
3、登录
$ docker login --help
Usage: docker login [OPTIONS] [SERVER]
Log in to a Docker registry.
If no server is specified, the default is defined by the daemon.
Options:
-p, --password string Password
--password-stdin Take the password from stdin
-u, --username string Username
4、提交 push镜像
$ docker build -t chengcoder/mytomcat:0.1 .
$ docker tag 容器id chengcoder/mytomcat:1.0