FROM

Create a base image FROM ubuntu:14.14Copy the code

For security, try to use the official image as the base image

LABEL

The Metadata cannot little

LABEL Maintainer ="[email protected]" LABEL Version ="1.0" LABEL Description =" This is description"Copy the code

RUN

RUN yum update && yum install-y vim \ python-dev # backslash newlineCopy the code
RUN apt-get update && apt-get install-y perl\ pwgen--no-install-recommends &&rm-rf\ /var/lib/apt/lists/* #Copy the code
 RUN /bin/bash -c 'source $HOME/.bashrc; echo $HOME
Copy the code

Each run will generate a new 10% for image. For aesthetics, please use a backslash line break for complex run to avoid useless layering and merge multiple commands into one line

WORKDIR

Setting the current working directory is similar to CD changing the current directory

WORKDIR /root WORKDIR /test # Create test directory WORKDIR demo RUN PWD #Copy the code

Use WORKDIR instead of RUN CD and try to use absolute directories.

ADD and COPY

Add local files to docker image. Add can be unzipped

WORKDIR /root ADD hello test/ #/root/test/hello WORKDIR /root COPY hello test/Copy the code

In most cases, COPY is better than ADD! To ADD a remote file/directory, use curl or wget

ENV

RUN apt-get install-y mysql-server= "${MYSQLVERSION}" \ &&rm-rf /var/lib/apt/lists/* #Copy the code

Env is maintainable, so use env to set constants whenever possible

ARG

Build parameters, and ENV to. But the scope is different. ARG environment variables are only valid in Dockerfile, that is, only in the docker build process, the built image does not exist in this environment variable.

The build command docker build can be overridden with –build-arg < parameter name >=< value >.

ARG APP_PATH=/var/www/app
Copy the code

CMD ENTRYPOINT

CMD: sets the default commands and parameters to be executed after the container is started. ENTRYPOINT: sets the command to be RUN when the container is started

Shell format

RUN apt-get install-y vim
CMD echo "hello docker"
ENTRYPOINT echo "hello docker"
Copy the code

The Exec format

RUN [ "apt-get" , "install" , "-y", "vim" ]
CMD [ "/bin/echo", "hello docker"]
ENTRYPOINT ["/bin/echo", "hello docker"]
Copy the code

[“/bin/bash”, “-c”, “echo hello $name”]

CMD

  1. The command executed by default when the container is started
  2. If docker run specifies another command, CMD is ignored
  3. If more than one CMD is defined, only the last one will be executed
FROM centos
ENV name Docker
CMD echo "hello $name"
Copy the code

Hello docker docker run-it [image] /bin/bash // No output

ENTRYPOINT

  1. Let the container run as an application or service
  2. It’s not going to be ignored, it’s going to be executed
  3. Best practice: Write a SHel script as entryPoint
COPY docker-entrypoint.sh /usr/local/bin/ 
ENTRYPOINT ["docker-entrypoint.sh"]
EXPOSE 27017
CMD ["mongod"]
Copy the code

reference

Github.com/docker-libr…