Create a docker image using Dockerfile

Source: Internet
Author: User
Tags bind file url mkdir relative centos tomcat docker run
The command used in Dockerfile has a base image specified from the from from, and in general a usable dockerfile must be from for the first instruction.
    As for the image, it can be any image image that is reasonably present.
    The from must be the first non-annotated instruction Dockerfile.
    The from can appear multiple times in a Dockerfile to facilitate the creation of a mixed images.
If no tag is specified, latest will be specified as the underlying image version to be used. Maintainer here is the information for specifying the image producer The Run Run command executes any legitimate command in the current image and submits the result of the execution.
    After the command executes the commit, the next instruction in the Dockerfile is automatically executed. Tier RUN directives and build submissions are consistent with the Docker core philosophy.
    It allows a custom build of the image image at any point, like version control. The RUN instruction cache is not automatically invalidated when the next command executes. For example, the cache of RUN Apt-get dist-upgrade-y may be used for the next instruction.
The--no-cache flag can be used to force cancellation of cache usage. The Env ENV directive can be used to set environment variables for the Docker container environment variable env settings, which can be viewed using the Docker inspect command.
You can also use Docker run--env <key>=<value> to modify environment variables. The user user is used to switch between running master identities.
Docker uses root by default, but if not, it is recommended to switch the user identity, after all, root permissions are too large, the use of security risks. Workdir Workdir is used to switch the working directory. Docker default working directory is/, only run can execute CD command to switch directories, but also only in the next run, that is, each run is independent. If you want other instructions to execute under a specified directory, you have to rely on Workdir.
The directory change of the Workdir action is persistent, and is used once workdir before each instruction.
    Copy copy adds files from path <src> copy to container internal path <dest>. <src> must be a file or directory for the source folder, or it can be a remote urL,<dest> is the absolute path in the target container. All new files and folders will create UID and GID.
In fact, if <src> is a remote file URL, the target file's permissions will be 600.
    Add add adds files from path <src> copy to container internal path <dest>. <src> must be a file or directory for the source folder, or it can be a remote URL.
    <dest> is the absolute path in the target container. All new files and folders will create UID and GID.
In fact, if <src> is a remote file URL, the target file's permissions will be 600.
VOLUME creates a mount point that can be mounted from a local host or other container, typically for storing databases and data that needs to be persisted.

The EXPOSE EXPOSE directive specifies the port to be forwarded when Docker allows it. CMD Dockerfile can only have one cmd instruction in it.
    If you specify more than one, then the last cmd command is in effect. The main function of the cmd instruction is to provide the default execution container.
    These default values can include executables, or they can omit executable files.
When you use the shell or exec format, CMD automatically executes the command.
    The function of the Onbuild onbuild is to delay the execution of the instruction and defer to the next use from the Dockerfile when the image is created, only one delay.
The Onbuild usage scenario is to obtain the latest source code (with RUN) and a qualified system framework when mirroring is established.
    Arg arg is a newly added directive for the Docker1.9 version. The variable defined by ARG is valid only when an image is created, the variable disappears after the build is complete, the label defines an image tag Owner, and assigns a value whose value is the value of the variable Name.

(LABEL owner= $Name)
    EntryPoint is the command or file to execute when you specify Docker image to run as instance (that is, Docker container).
 The contents of my own Dockerfile file are as follows:
From Docker.io/centos
maintainer the CentOS Test images-test
run mkdir-p/usr/app
run LS
run pwd
CO py/jdk/usr/app/jdk/
Add tomcat//usr/app/tomcat/
add hadoop//usr/app/hadoop/
ENV java_home/usr/app/jdk
ENV PATH $JAVA _home/bin: $PATH
#ADD/soft/jdk/
#ADD/soft/tomcat/
#ADD/soft/hadoop/
# Volumes for Systemd
# VOLUME ["/run", "/tmp"]
# Environment for SYSTEMD
# ENV Container=docker
# for S YSTEMD usage This changes to/usr/sbin/init
# Keeping it as/bin/bash in compatibility with previous
#CMD ["/bi N/bash "]

First look at my directory structure:

[Root@localhost docker]# pwd
/soft/docker
[root@localhost docker]# ll Total
4
-rw-r--r--. 1 root root 541 11:20 Dockerfile
[root@localhost docker]# cd
. [Root@localhost soft]# pwd
/soft
[root@localhost soft]# ll Total
393332
-rw-r--r--. 1 root  root< c11/>9271609 17:23 apache-tomcat-8.5.4.tar.gz
drwxr-xr-x. 2 root root:         11:20 Docker
Drwxr-xr-x. 9 10011 10011      4096 Jan
-rw-r--r--. 1 root  root  212046774  8 18:01 Hadoop-2.7.2.tar.gz
drwxr-xr-x. 8   143      4096 June 09:56 jdk-rw-r--r
--. 1 root  root< c28/>181435897  8 17:23 jdk-8u102-linux-x64.tar.gz
drwxr-xr-x. 9 root  root       4096 10 17:24 Tomcat
  The Dockerfile file is created so that you can create a Docker image from the Docker build.
  look at the Help files for the Docker build:
[Root@localhost soft]# Docker build--help usage:docker build [OPTIONS] PATH | URL |                    -Build an image from a Dockerfile--build-arg=[] Set build-time variables--cpu-shares CPU shares (relative weight)--cgroup-parent Optional parent Cgroup for the container--cpu-pe Riod Limit the CPU CFS (Completely Fair Scheduler) period--cpu-quota Limit the C PU CFS (Completely Fair Scheduler) quota--cpuset-cpus CPUs in which to allow execution (0-3, 0,1)- -cpuset-mems MEMS in which to allow execution (0-3, 0,1)--disable-content-trust=true Skip image ve                      Rification-f,--file Name of the Dockerfile (Default is ' path/dockerfile ')--force-rm                     Always remove intermediate containers--help Print usage--isolation           Container isolation level-m,--memory         Memory limit--memory-swap swap limit equal to memory plus swap: '-1 ' to enable unlimited SWA P--no-cache do not use cache when building the image--pull always Atte  MPT to pulling a newer version of the IMAGE-Q,--quiet suppress the build output and print image ID on                      Success--rm=true Remove Intermediate containers after a successful build--shm-size Size Of/dev/shm, default value is 64mb-t,--tag=[] Name and optionally a tag in the ' Nam ' E:tag ' format--ulimit=[] ulimit options-v,--volume=[] Set build-time bind Mount S
 Generate Image: 
[Root@localhost soft]# Docker build-t centos:base-f/soft/docker/dockerfile/soft Sending build context to Docker daemon 1.118 GB Step 1:from docker.io/centos Trying to pull repository Docker.io/library/centos ... latest:pulling from dock Er.io/library/centos 3d8673bd162a:pull Complete digest:sha256: A66FFCB73930584413DE83311CA11A4CB4938C9B2521D331026DAD970C19ADF4 status:downloaded newer image for Docker.io/centos  : Latest---> 970633036444 Step 2:maintainer the CentOS Test Images <liuqi@rzport.com>-Liuqi---> Running In 69FD729187F2---> 7f61239ff233 removing Intermediate container 69fd729187f2 Step 3:run mkdir-p/usr/app--- ; Running in fb5e95a82274---> 32826b551857 removing Intermediate container fb5e95a82274 Step 4:run ls---> Runnin 
G in 4cbe815e848f Anaconda-post.log bin Dev etc home Lib lib64 lost+found media mnt opt proc root run sbin SRV sys tmp usr var---> 8c3b8f7ebc57 removing Intermediate container 4cbe815e848f Step 5:runPWD---> Running in f226b1caf3e0/---> 19379045c11d removing Intermediate container f226b1caf3e0 Step 6:copy/j
 dk/usr/app/jdk/---> 3137c3ee72dc removing Intermediate container 6c1e513e964c Step 7:add tomcat//usr/app/tomcat/ ---> 8c6d7a52769a removing Intermediate container 98d8178ef560 Step 8:add hadoop//usr/app/hadoop/---> 34a8ea48 3a7e removing intermediate container ee7fdc55dbc9 Step 9:env java_home/usr/app/jdk---> Running in 0a4f8f242ede-- a297fbddb78a removing intermediate container 0a4f8f242ede Step 10:env PATH $JAVA _home/bin: $PATH---> Running i N 368103F758DD---> 9d20362732d7 removing Intermediate container 368103F758DD successfully built 9D20362732D7 [root@l              Ocalhost soft]# Docker images REPOSITORY TAG IMAGE ID CREATED SIZE CentOS              Base 9d20362732d7 4 minutes ago 904.8 MB Docker.io/centos Latest 970633036444 2 WeEks ago 196.7 MB
 
  Through-f to specify the location of the Dockerfile file, the following/soft and its directory must be able to find the Dockerfile file otherwise it will report the context of the error, Mv,copy,add file location is relative/soft.
[Root@localhost soft]# Docker build-t centos:bases-f/soft/docker/dockerfile/usr
Unable to prepare context:the Doc Kerfile (/soft/docker/dockerfile) must be within the build context (/usr)
  This resolves an issue where the Dockerfile file is not in the same directory as the file that needs to be copied, such as the No such files or Directory,forbidden path outside the build using the add,copy directive Context:.. /jdk/() and other similar errors.
  Once the image is created, you can launch the Docker run to start the image, and when the image is started, a container is created, and we can simply mirror the image like a class, the container is an instance of this class, and the image can be understood as a system image. Container is a state of image at run time. If you take a virtual machine as a metaphor, the image is the disk file in the shutdown state, container is the disk file, including memory data, when the VM is running.
   where the-t option allows Docker to assign a pseudo-terminal (Pseudo-tty) and bind to the container's standard input,-I keeps the container's standard input open. When
   you use Docker run to create a container, the standard operations that Docker runs in the background include
   checking that there is a specified image locally, not having the image created from the public repository,
   and starting a container
   to assign a file system, and mount a layer of read-write layers out of the mirrored mirror layer
   bridge a virtual interface from the host host's configuration to the container to
   configure an IP address from the address pool to
   the container execution the container is terminated after the user-specified application
   finishes executing

Reference: HTTPS://WWW.DWHD.ORG/20151202_113538.HTML?LAN=CN&LAN=CN
http://blog.chinaunix.net/ uid-10915175-id-4442826.html
http://blog.csdn.net/zssureqh/article/details/52009043
Http://blog.csdn.net /we_shell/article/details/38445979
http://my.oschina.net/renguijiayi/blog/353835
http://blog.csdn.net/ wsscy2004/article/details/25878363
http://www.simapple.com/364.html
http://pdfwork.cn/blog/2015/docker/

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.