dockerfile note

本文详细介绍Dockerfile的使用方法及各个指令的作用,包括如何通过Dockerfile构建镜像、指令格式规范、变量替换等高级用法,并解释了关键指令如FROM、RUN、CMD等的具体用途。

dockerfile note
reference

summary

  1. defination
    docker can build images automatically by reading the instructions from a dockerfile. dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.

  2. usage
    the docker build command builds an image from a dockerfile and context. the build's context is the set of files at a specified location PATH or URL. warning: don't use PATH /, because it can transfer the entire contents of your hard drive to the docker daemon.
    docker build -f /home/vickey/dockerfile .

  3. format a. INSTRUCTION is not case-sensitive but convention is UPPERCASE. e.g: FROM nginx:1.13 b. must start with FROM.

    c. docker treat lines begin with # as a comment.

  4. parser directive
    reference a. parser directive is not case-sentive but convention is lowercase and must at the first line of dockerfile e.g: # directive=value then the next line is FROM nginx:1.13

    b. can't repeat

  5. escape
    in linux default is \, " ` " in windows

  6. variable replacement
    ${variable:-word} indicates that if variable is set then the result will be that value. If variable is not set then word will be the result.
    ${variable:+word} indicates that if variable is set then word will be the result, otherwise the result is the empty string.

  7. .dockerignore file
    Before the docker CLI sends the context to the docker daemon, it looks for a file named .dockerignore in the root directory of the context. If this file exists, the CLI modifies the context to exclude files and directories that match patterns in it

  8. FROM
    The tag or digest values are optional. If you omit either of them, the builder assumes a latest tag by default
    FROM buildpack-deps:jessie

  9. RUN
    RUN /bin/bash -c 'source $HOME/.bashrc; \ echo $HOME'
    equivalent to following
    RUN /bin/bash -c 'source $HOME/.bashrc; echo $HOME'

  10. CMD
    There can only be one CMD instruction in a Dockerfile. If you list more than one CMD then only the last CMD will take effect
    CMD echo "This is a test." | wc -

  11. LABEL
    A LABEL is a key-value pair.
    LABEL version="1.0"
    To view an image’s labels, use the docker inspect command
    docker inspect docker container name

  12. EXPOSE
    By default, EXPOSE assumes TCP. You can also specify UDP:
    EXPOSE 80/udp
    or publish port when run images
    docker run -p 80:80/tcp -p 80:80/udp image_name

  13. ENV
    The entire string after the first space will be treated as the - including whitespace characters. allows for multiple variables to be set at one time
    ENV myName John Doe ENV myDog Rex The Dog
    equivalent ti ENV myName = John Doe

  14. ADD
    The ADD instruction copies new files, directories or remote file URLs from and adds them to the filesystem of the image at the path

  15. COPY
    The COPY instruction copies new files or directories from <src> and adds them to the filesystem of the container at the path <dest>.
    COPY VS ADD

  16. VOLUME
    The VOLUME instruction creates a mount point with the specified name and marks it as holding externally mounted volumes from native host or other containers. The value can be a JSON array, VOLUME ["/var/log/"], or a plain string with multiple arguments, such as VOLUME /var/log orVOLUME /var/log/var/db

  17. WORKDIR
    The WORKDIR instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile
    WORKDIR /path/to/workdir RUN pwd

转载于:https://www.cnblogs.com/vickey-wu/p/8745953.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值