How to build a docker image using Dockerfile

Source: Internet
Author: User
Tags curl docker run
Build Docker Image

Simple recording of how to build Docker image

Build Docker image Docker build Usage dockerfile related instruction RUN CMD entrypoint SHELL CMD entrypoint building ACTIVEMQ Image Reference

The Docker build Usage dcoker Build command sends the built-in content to the Docker daemon, sequentially executing the commands in Dockerfile (each line of instruction commits a new layer). Successful image build when you build the Docker image, you should choose a directory that belongs to the build-related (the content needed to build the image), which will reduce unnecessary content to the Docker daemon to accelerate the build of the specified Dockerfile build, Docker Build-t 8lovelife/activemq:1.0-f/home/dmz/temp/dockerfile. related directives in Dockerfile RUN

Run is used to execute the required command on a new layer above the current mirror, and after execution the result is committed and can be used by subsequent directives Shellform:run <command>,linux is executed by default in/bin/sh-c, Windows defaults to CMD/ S/C Execform:run ["Executable", "param1", "Parma2"], this form does not execute the command shell. such as Run ["echo", "$DMZ"] does not output the parsed variable DMZ, and run ["sh", "-C", "Echo $DMZ"] can parse the variable DMZ (System variables in the final mirror) Execform will be parsed in JSON array form, So the parameters need double quotes and the relevant symbols need to be translated like backslashes, such as: RUN ["C:\\Windows\\System32"] CMD

CMD is used for the default behavior of the container execution (which also enables the container to run in an executable form) and must not exist. If the dockerfile appears multiple times, only the last time will be effective CMD ["executable", "param1", "param2"] execform CMD ["Params1", "param2"], as the default parameters of EntryPoint CMD command param1 param2 shellform entrypoint

EntryPoint enables the container to run in an executable form, only the last entrypoint will be valid. Use Docker run--entrypoint to specify overrides entrypoint ["Executable", "param1", "param2"] execform entrypoint command param1 param2 Shellform, the parameters in CMD will not be used. This becomes the/BIN/SH-C subcommand, and the executable command is no longer a pid=1 in the container, which causes the container to not be able to receive UNIX signals and cannot listen to Docker stop <container> SIGTERM when the Stop container times out , the Sigkill will be issued a forced stop container. Using exec will solve the problem. SHELL

The shell is used to change the shell used by default in Shellform, such as Windows using Powershell,shell ["PowerShell", "-command"], and multiple shell instructions will overwrite the previous shell instructions Cmd/entrypoint

Build ACTIVEMQ Image

Dockerfile:

From Openjdk:8-jre-alpine #Base Image OpenJDK8
ENV activemq_version 5.14.5 #环境变量
ENV ACTIVEMQ apache-activemq-$ACTIVEMQ _version
ENV activemq_tcp=61616 activemq_amqp=5672 activemq_stomp=61613 activemq_mqtt=1883 activemq_ws=61614 ACTIVEMQ_UI=8161
ENV ACTIVEMQ_HOME/OPT/ACTIVEMQ
RUN set-x && \ #设置脚本执行跟踪
Mkdir-p/opt && \ #建立目录/opt
APK--update Add--virtual build-dependencies curl && \ #添加curl命令到虚拟构建依赖组build-dependencies
Curl-s-S https://archive.apache.org/dist/activemq/$ACTIVEMQ _version/$ACTIVEMQ-bin.tar.gz | Tar xvz-c/opt && \ #下载activemq并解压到指定目录
Ln-s/opt/$ACTIVEMQ $ACTIVEMQ _home && \ #建立软连接
Addgroup-s activemq && adduser-s-h-g activemq-h $ACTIVEMQ _home activemq && \ #添加用户 & user groups
Chown-r activemq:activemq/opt/$ACTIVEMQ && \
Chown-h activemq:activemq $ACTIVEMQ _home && \
APK del build-dependencies && \ #删除虚拟构建依赖组
rm-rf/var/cache/apk/* #删除install缓存, narrowing image size, backslash wrapping (same command execution reduces result submission, reduces image layer)
User Activemq #用户切换 (after which the command belongs
Workdir $ACTIVEMQ _home
EXPOSE $ACTIVEMQ _tcp $ACTIVEMQ _amqp $ACTIVEMQ _stomp $ACTIVEMQ _mqtt $ACTIVEMQ _ws $ACTIVEMQ _ui #暴露端口号
CMD ["/bin/sh", "-C", "BIN/ACTIVEMQ console"] #容器启动后执行命令

Build
Docker build-t 8lovelife/activemq:1.0. Reference

Docker v17.6
ACTIVEMQ Dockerfile Source

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.