Dockerfile command summary and parsing

Source: Internet
Author: User
Tags dockerfile example

Address: http://www.maoyupeng.com/dockerfile-command-introduction.html

What is dockerfile?

Dockerfile is a script composed of a series of commands and parameters. These commands are applied to the basic image and finally create a new image. They simplify the process from start to end and greatly simplify the deployment. Dockerfile starts from the from command, followed by various methods, commands, and parameters of the followers. The output is a new image that can be used to create a container.

When you use docker to build an image, each command forms a new layer based on the previous command. These basic images can be used to create new containers. This article will teach you how to build a container from dockerfile step by step from the basic image.

Dockerfile example
# Version 1.0FORM ubuntu:14.04MAINTAINER Mao "[email protected]"RUN apt-get update && apt-get install -y nginxRUN echo ‘Hello,I am work‘ > /usr/share/nginx/html/index.htmlEXPOSE 80 80
Docker execution step analysis

AboveDockerfileIn this example, each command creates a new image layer and submits the image. docker executes the general process of dockerfile:

  1. Docker runs a container from the basic image;

  2. Execute a command to modify the container;

  3. Execute similardocker commitTo submit a new image layer;

  4. Docker then runs a new container Based on the submitted image;

  5. RunDockerfileUntil all commands are executed;

Example
  1. From: The first command of each dockerfile is the from. From command to specify an existing image, which means that subsequent commands of from are based on this image (ubuntu14.04.

  2. Maintainer: This Command tells docker the author and email address.

  3. Run: In layman's terms,RUNCommand will use the command package in Shell/bin/sh -cRun the Run Command in Exec format if it is run on a platform that does not support shell.RUN ["apt-get","install","-y","nginx"]

  4. Expose: Public ports

Dockerfile command summary and parsing maintainer

I suggest placing this command in the starting part of dockerfile, although theoretically it can be placed anywhere in dockerfile. This command is used to declare the author and should be placed behind the from.

# MAINTAINER [name] [email]MAINTAINER authors_name "[email protected]"
From

The from command may be the most important dockerfile command. The command defines which basic image is used to start the building process. The basic image can be any image. If the basic image is not found, docker will try to find the image from the docker image index. The from command must be the first command of dockerfile.

# FROM [image name]FROM ubuntu 
Add

ADDThe command has two parameters: source and target. Its basic function is to copy files from the source system file system to the target container file system. If the source is a URL, the URL content will be downloaded and copied to the container.

# ADD [source directory or URL] [destination directory]ADD /my_app_folder /my_app_folder 
Run

RUNCommand isDockerfileThe core part of the command execution. It accepts commands as parameters and is used to create images. UnlikeCMDCommand,RUNCommand is used to create an image (a new layer is formed above the previous commit Layer ).

# RUN [command]RUN apt-get update
CMD

AndRUNSimilar commands,CMDIt can be used to execute specific commands. AndRUNThe difference is that these commands are not executed during the image building process, but are called after the container is built using the image.

# CMD application "argument", "argument", ..CMD "echo" "Hello Mao!"
Entrypoint

ENTRYPOINTIt helps you configure a container to make it executable.CMDCommands andENTRYPOINTCommand, you canCMDCommand to remove the "application" and only retain the parameter. The parameter will be passedENTRYPOINTCommand.

# Usage: ENTRYPOINT application "argument", "argument", ..# Remember: arguments are optional. They can be provided by CMD# or during the creation of a container.ENTRYPOINT echo# Usage example with CMD:# Arguments set with CMD can be overridden during *run*CMD "Hello docker!"ENTRYPOINT echo
Env

ENVCommand is used to set environment variables. These variables exist in the form of "Key = value" and can be called by scripts or programs in the container. This mechanism brings great convenience to running applications in containers.

# ENV key valueENV SERVER_WORKS 4
User

The USER command is used to set the UID of the running container.

# USER [UID]USER 751
Volume

The volume command is used to allow your container to access the directory on the host.

# VOLUME ["/dir_1", "/dir_2" ..]VOLUME ["/my_files"]
Workdir

The workdir command is used to set the running directory of the command specified by CMD.

# WORKDIR /pathWORKDIR ~/
Expose

EXPOSECommand is used to tell docker which ports the container will listen for at runtime. docker uses this information when connecting to different containers (using the-Link parameter;
The two core concepts of docker are repeatable and portable. The image should be able to run on any host and run as many times as possible. In dockerfile, you have the ability to map private and public ports, but you never need to map public ports through dockerfile. By ing public ports to the host, you can run only one containerized application instance. (Note: Running multiple ports does not conflict)

# EXPOSE [port]# private and public mappingEXPOSE 80:8080# private onlyEXPOSE 80

Dockerfile command summary and parsing

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.