Dockerfile制作镜像

1.下载基础镜像

比如我做的是一个普通的JavaWeb应用的镜像,需要在Tomcat上运行,这里就选用Tomcat作为基础镜像。

镜像源

  1. 官网源
  2. 阿里云镜像

还有很多国内镜像源,因为官网镜像源(*的)被那啥了

➜ ~ docker pull tomcat
Using default tag: latest
latest: Pulling from library/tomcat
ab1fc7e4bf91: Pull complete 
35fba333ff52: Pull complete 
f0cb1fa13079: Pull complete 
3d79c18d1bc0: Pull complete 
ff1d0ae4641b: Pull complete 
8883e662573f: Pull complete 
adab760d76bd: Pull complete 
86323b680e93: Pull complete 
14a2c1cdce1c: Pull complete 
ee59bf8c5470: Pull complete 
067f988306af: Pull complete 
Digest: sha256:a98a9e48ec35714ee990c4975711687cdda00ef0cec83ed6ecc4c77ae5e8b430
Status: Downloaded newer image for tomcat:latest

镜像是分层依赖的,逐层构建,比如肯定这个Tomcat镜像里面有个是JDK的。

➜ ~ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
tomcat              latest              7ee26c09afb3        4 hours ago         46

2. 编写Dockerfile构造文件

直接vim Dockerfile, 这个命名是固定的。

FROM tomcat
MAINTAINER chgl16
COPY taobao.war /usr/local/tomcat/webapps

1.FROM导入基础镜像
2.MAINTAINER是标注作者、维护者(可选)
3.COPY 这是复制准备好的JavaWeb项目进基本镜像,做成自己的镜像。
对应使用方法和路径端口可以看镜像介绍。

Dockerfile制作镜像

build创建

➜ ~ docker build -t taobao:latest .
Sending build context to Docker daemon  37.87MB
Step 1/3 : FROM tomcat
 ---> 7ee26c09afb3
Step 2/3 : MAINTAINER chgl16
 ---> Running in 9b8e2f46dc0f
Removing intermediate container 9b8e2f46dc0f
 ---> eeea8c70e9de
Step 3/3 : COPY taobao.war /usr/local/tomcat/webapps
 ---> bc1b01b2371e
Successfully built bc1b01b2371e
Successfully tagged taobao:latest

-t 是给创建的镜像添加名字和标签: (详细参数可见 docker build --help)
-t, --tag list Name and optionally a tag in the ‘name:tag’ format
最后的 . 是对应当前目录编写的Dockerfile

查看执行新镜像

➜ ~ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
taobao              latest              bc1b01b2371e        7 seconds ago       500MB
tomcat              latest              7ee26c09afb3        4 hours ago         462MB

➜ ~  docker run -d -p 8888:8080 taobao
97644b25ce8f951abce4f77740351be6f93462f6c2c20eacafacc15960291a5b

➜ ~  docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
97644b25ce8f        taobao              "catalina.sh run"   6 minutes ago       Up 6 minutes        0.0.0.0:8888->8080/tcp   agitated_brahmagupta

Tomcat容器的默认端口是8080,可以看镜像介绍。

执行 localhost:8888/taobao即可看到项目。

如果用的是本地数据库,也可以另外搞个数据库镜像容器。但是注意桥接带来的主机名问题。