COPY
COPY <源路径> <目标路径>
ADD
ADD <源路径> <目标路径> 同COPY带解压缩功能,不推荐使用
CMD
-
启动脚本 只允许使用一次 CMD ./startup.sh
-
只允许使用一次,多个CMD只执行最后一条
CMD ./startup.sh
CMD systemctl start mysql
ENTRYPOINT
<ENTRYPOINT> "<CMD>"
- 只允许使用一次
CMD ./startup.sh
ENTRYPOINT serviceup.sh
systemctl start mysql
systemctl start redis
ENV
ENV <key> <value>
ENV MYSQL_VERSION 5.7.22
EXPOSE
端口暴露
FROM tomcat
MySQL 3306
EXPOSE 8080 3306
WORKDIR
像cd
FROM tomcat
# 执行删除命令
RUN rm -rf /usr/local/tomcat/webapps/ROOT/*
# 复制到指定目录
COPY myshop.tar.gz /usr/local/tomcat/webapps/ROOT/
# 指定工作目录
WORKDIR /usr/local/tomcat/webapps/ROOT/
# 解压并删除
RUN tar -zxvf myshop.tar.gz
&& rm -rf myshop.tar.gz
# 暴露端口
EXPOSE 8080
创建容器镜像
[root@Proxy myshop]# docker build -t myshop .
Sending build context to Docker daemon 3.072kB
Step 1/6 : FROM tomcat
---> 31a47677561a
Step 2/6 : RUN rm -rf /usr/local/tomcat/webapps/ROOT/*
---> Running in 9e79795e9561
Removing intermediate container 9e79795e9561
---> 8f8e7a6f0311
Step 3/6 : COPY myshop.tar.gz /usr/local/tomcat/webapps/ROOT/
---> 698409b80c1c
Step 4/6 : WORKDIR /usr/local/tomcat/webapps/ROOT/
---> Running in cdd76a252311
Removing intermediate container cdd76a252311
---> 36de8e85e9f7
Step 5/6 : RUN tar -zxvf myshop.tar.gz && rm -rf myshop.tar.gz
---> Running in f938c18130e7
index.jsp
Removing intermediate container f938c18130e7
---> 32412a868e90
Step 6/6 : EXPOSE 8080
---> Running in b15b8be8779b
Removing intermediate container b15b8be8779b
---> efd695e46c39
Successfully built efd695e46c39
Successfully tagged myshop:latest
运行容器
[root@Proxy myshop]# docker run -p 8080:8080 --name myshop -d myshop
4531078fec2e6991dea847bc8ff8166cefc7a0ce76c0304aff8668c6fd474312
WORKDIR工作目录
[root@Proxy myshop]# docker exec -it myshop /bin/bash
root@4531078fec2e:/usr/local/tomcat/webapps/ROOT#