"Linux" "Services" "Docker" Docker File

Source: Internet
Author: User
Tags docker run haproxy

Docker Images:
Docker commit
Dockerfile: Text file, image file build script;

Dockerfile: Consists of a series of specialized instruction sequences used to construct a new image file based on the underlying image;
Directives: Select the base image, install necessary programs, copy configuration files and data files, automatically run the service, and the ports to be exposed, etc.

Command: Docker build;

Syntax: command line, Comment line, and blank line;
Command line: Composed of instruction and instruction parameters;
directive: Its characters are not case-sensitive; It is customary to use all uppercase letters;
Comment lines: #开头的行, must be in one row alone;
Blank line: will be ignored;

Instructions:
From directive: Must be the first non-commented line, used to specify the base image used;

Syntax format:
from <image>[:<tag>] or
From <image>@<digest>

From Busybox:latest
From centos:6.9

Note: Try not to use multiple from directives in a dockerfile file;

Maintanier directive: The instruction used to provide information for the author to provide his own information, without limiting the location of its appearance, but the recommendation is immediately after the from;

Syntax format:
Maintanier <author ' s detail>

For example:
Maintanier mageedu Linux operation and maintance Institute <[email protected]>

Copy instruction: Used to copy files from the Docker host to the image file being created;

Syntax format:
COPY <src> ... <dest>
COPY ["<src>",... "<dest>" (Use this format when there are white space characters in the file name)

<src&gt: A wildcard character is supported for the source file or directory to be copied;
<dest>: Destination path, the file system path of the image file being created, or the absolute path established, otherwise, relative to Workdir;

The UID and GID of all new copy generated catalog files are 0;

For example:
COPY Server.xml/etc/tomcat/server.xml
COPY *.conf/etc/httpd/conf.d/

Attention:
<src> must be a path in the build context, so you cannot use a similar ". The path of the/some_dir/some_file "class;

<src> if it is a directory, recursive replication will be automatic, and if there are multiple <src&gt, including the use of wildcards on <src>, then <dest> must be a directory and must be/end;

<dest> If it does not exist beforehand, it will be created automatically, including its parent directory;

Add directive: Similar to copy instructions, additional support for copying tar files, and URL path;


Syntax format:
ADD <src> ... <dest>
ADD ["<src>",... "<dest>"]

Example:
ADD haproxy.cfg/etc/haproxy/haproxy.cfg
ADD logstash_*.cnf/etc/logstash/
ADD http://www.magedu.com/download/nginx/conf/nginx.conf/etc/nginx/

Note: The source file is specified in URL format, and the permissions of the destination file are 600 after the download is complete;

Attention:
<src> must be a path in the build context, so you cannot use a similar ". The path of the/some_dir/some_file "class;

If <src> is a URL and <dest> does not end with/, the <src> specified file will be downloaded and created directly as <dest>; if <dest> ends with/ The URL specified file will be downloaded to <dest>, and the original is retained;

If <src> is a tar-formatted file on a host local file system, it will be expanded to a directory that behaves like the tar-x command; However, if the file downloaded through the URL is in tar format, it will not be automatically expanded;

<src> if it is a directory, recursive replication will be automatic, and if there are multiple <src&gt, including the use of wildcards on <src>, then <dest> must be a directory and must be/end;

<dest> If it does not exist beforehand, it will be created automatically, including its parent directory;

ENV directive: Defines environment variables, which can be called by other directives in the current Dockerfile file, in the form of $variable_name or ${variable_name};

Grammar:
ENV <key> <value> Define one variable at a time
ENV <key>=<value> ... Multiple variables can be defined at a time, if there is a white space character in <value>, to be escaped or quoted with the \ character;

For example:
ENV myname= "Obama Clark" mydog=hello\ Dog
Mycat=garfield

Equivalent to:
ENV myName Obama Clark
ENV Mydog Hello Dog
ENV Mycat Garfield

env defines an environment variable that persists throughout the process of mirroring, so you can use the inspect command to view it, or even use the--ENV option to modify the value of the specified variable when the Docker run launches the image;

User directive: Specifies the username or UID when the image is run, or the program specified by any run/cmd/entrypoint instruction in the Dockerfile file is run;

Syntax format:
USER <UID>|<Username>

Note that the:<uid> should use the UID of the user that exists with the/etc/passwd file, otherwise Docker run may fail;

Workdir instruction: Used to specify the working directory for all Run/cmd/entrypoint/copy/add directives in the dockerfile;

Syntax format:
Workdir <dirpath>

Note: Wordir can occur more than once, or a relative path, which represents the path specified relative to the previous workdir instruction, and Workdir can also invoke the value of an environment variable defined by ENV;

For example:
Workdir/var/log
Workdir $STATEPATH

Volume directive: Used to create a mount point directory in the target image file for mounting volumes on the host or other containers;

Syntax format:
VOLUME <mountpoint>
VOLUME ["<mountpoint>", ...]

Attention:
If a file exists in advance of the Mountpoint path, the Docker Run command copies the previous file to the newly mounted volume after the volume is mounted;

Run instruction: Used to specify the command to run during the Docker build process, rather than the Docker run of the Dockerfile built as a mirror;

Syntax format:
RUN <command> or
RUN ["<executeable>", "<param1>", "<param2>", ...]

RUN ["/bin/bash", "-C", "<executeable>", "<param1>", "<param2>", ...]

For example:
RUN Yum install iproute nginx && yum Clean all

CMD directives: Similar to run commands for running programs, but they run at different points in time; cmd runs at Docker Run, not Docker build;
The primary purpose of the CMD instruction is to specify the default program to be run for the container to start, the end of the program run, and the end of the container, however, the program specified by the cmd instruction can be overridden by the program specified to run in the Docker Run command line parameter.

Syntax format:
CMD <command> or
CMD ["<executeable>", "<param1>", "<param2>", ...] or
CMD ["<param1>", "<param2>", ...]

The third is to provide default parameters for the program specified by the entrypoint directive;

Note: If there are multiple cmd directives in Dockerfile, only the last one takes effect;

CMD ["/usr/sbin/httpd", "-C", "/etc/httpd/conf/httpd.conf"]

entrypoint directive: Similar to the cmd instruction, but it is not overwritten by the instructions specified by the command line parameters of Docker run, and these command line parameters are given as parameters to the program specified by the entrypoint instruction;
However, if the--entrypoint option is used when running Docker run, the parameters of this option can be used as a program to be run to overwrite the program specified by the entrypoint directive;

Syntax format:
EntryPoint <command> or
entrypoint ["<executeable>", "<param1>", "<param2>", ...]

For example:
CMD ["-C"]
entrypoint ["Top", "-B"]

Expose directive: Used to specify the port to be exposed for the container;

Syntax format:
EXPOSE <port>[/<protocol>] [<port>[/<protocol>]] ...

<protocol> is either TCP or UDP, the default is TCP;

For example:
EXPOSE 11211/tcp 11211/UDP

Onbuild directives: defining triggers;
The operation specified by the onbuild instruction will be executed only when the mirror constructed by the current dockerfile is used as the base image to construct the other mirrors;

Syntax format:
Onbuild <INSTRUCTION>

For example:
Onbuild ADD my.cnf/etc/mysql/my.cnf

Note: Onbuild cannot nest itself, and does not trigger the from and maintainer instructions;

Example 1:
From Busybox:latest
Maintainer mageedu <[email protected]>

COPY index.html/web/html/index.html

EXPOSE 80/tcp

CMD ["httpd", "-F", "-H", "/web/html")


Workdir:/tmp/busybox-web
Index.html
Busybox-web.df

~]# Docker build-f/tmp/busybox-web/busybox-web.cf-t Busybox:web/tmp/busybox
~]# Docker Images







Example 2:HTTPD



Practice:
(1) Build a CentOS-based httpd image, the main directory path is/web/htdocs, and the home page exists, and run as Apache user, exposing 80 ports;
(2) Further, its paging file on the main machine on the volume;
(3) Further, HTTPD Support parsing PHP page;
(4) Build a MARIDB image based on CentOS, so that the container can communicate with each other;
(5) to deploy WordPress on httpd;


Container Import and Export:
Docker Export
Docker Import

Storage and loading of images:
Docker Save-o/path/to/somefile. TAR Name[:tag]

Docker Load-i/path/from/somefile. TAR

Review:
Dockerfile directive:
From,maintainer
Copy,add
Workdir, ENV
USER
VOLUME
EXPOSE
RUN
Cmd,entrypoint
Onbuild

Dockerfile (2)
Example 2:HTTPD

From Centos:latest
Maintainer mageedu "<[email protected]>"

RUN sed-i-E ' [email protected]^mirrorlist.*repo=os.*[email protected]=http://172.16.0.1/cobbler/ks_mirror/$ Releasever/@g '-e '/^mirrorlist.*repo=updates/a enabled=0 '-e '/^mirrorlist.*repo=extras/a enabled=0 '/etc/ Yum.repos.d/centos-base.repo && \
Yum-y Install httpd php php-mysql php-mbstring && \
Yum Clean all && \
Echo-e ' <?php\n\tphpinfo ();\n?> ' >/var/www/html/info.php

EXPOSE 80/tcp

CMD ["/usr/sbin/httpd", "-F", "/etc/httpd/conf/httpd.conf", "-dforeground"]

"Linux" "Services" "Docker" Docker File

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.