Docker Basics Tutorial--dockerfile

Source: Internet
Author: User
Tags port number docker hub docker run

Original source: http://blog.csdn.net/anxpp/article/details/52495309

Dockerfile is a text-formatted configuration file that we can use to quickly create a custom image using the Dockerfile file.

Many of the instructions supported by Dockerfile are the main things to be covered in this article, followed by a basic example.

In general, Docker is divided into 4 parts: base image information, maintainer information, mirroring operation instructions, and execution commands at container startup.

and supports comment lines that begin with #. 1. Instruction

Half the format of the instruction is instruction arguments. (1) from

The format is from <image> or from <image>:<tag>.

The first line of the Dockerfile file must be a from directive, and multiple from directives (once per mirror) can be used if multiple mirrors are created in the same dockerfile. (2) Maintainer

Specify maintainer information in the format: Maintainer <name>. (3) RUN

The format is run <command> or run ["Executable", "param1", "param2"].

The former will run the command in the shell terminal, i.e./bin/sh-c, which is executed using EXEC.

The second way you can specify other terminal implementations, such as RUN ["/bin/bash", "-C", "echo Hello"].

Each day the Run command executes the specified command on the current mirror and submits it as a new mirror.

When the command is long, you can use \ to break the line. (4) cmd specifies the command to execute when the container is started, with only one CMD command per Dockerfile. If more than one command is specified, only the last bar is executed.

The command supports three formats:

cmd ["Executable", "param1", "param2"]: Execute cmd command with exec param1 param2: Execute in/bin/sh, provide application CMD ["param1", "param2" to interact) ]: Default parameters provided to EntryPoint

If you specify a command to run in the container, the cmd command is overwritten. (5) EXPOSE

Tell the Docker server container to expose the port number for use by the connected system.

Format: EXPOSE <port> [<port>..]

When the container starts, it is necessary to have the Docker host automatically assign a port to the specified port via-p and specify which local port will be mapped by using-p. (6) ENV

Specifies the environment variable that will be used by the post-stream Run command and maintained when the container is running.

Format: ENV <key> <value> (7) ADD

This command copies the file to the container.

Format: ADD <src> <dest>

The specified <src> will be copied to <dest>.

<src> is a relative path (file or directory) of the directory where the Dockerfile is located, or it can be a URL or a tar file (it will be automatically unzipped to a directory). (8) COPY

Format Copy <src> <dest>

The replication host's <src> (the relative path, file or directory of the directory where the Dockerfile resides) is the container's <dest>, and is created automatically when the destination path does not exist. (9) entrypoint

Configures the command that is executed after the container is started, and cannot be overridden by parameters provided by Docker run.

Format:

entrypoint ["Executable", "param1", "param2"] entrypoint command param1 Param2:shell Execute

Similarly, there can only be one directive in each dockerfile, specifying multiple when only the last one takes effect. (Ten) VOLUME

Create 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.

Format: VOLUME ["/data"]. (one) USER

Specifies the user name or UID when the container is run, and subsequent run uses the specified user.

Format: USER daemon (workdir)

Configure the working path for subsequent run, CMD, and entrypoint directives.

Format: Workdir/path/to/workdir.

You can use more than one directive, and subsequent directives, if they are relative paths, specify the path based on the previous command

Like what:

workdir/a
workdir B
RUN X
The final path is:/a/b.

(Onbuild)

Configures the action instruction that is being executed when the mirror is currently created as the underlying image of the other newly created mirror.

Format: onbuild [instruction].

For example, the following dockerfile:

...
Onbuild ADD  .   /APP/SRC
onbuild run/usr/local/...--dir/app/src ...
If you create a new image based on the image above, the new dockerfile uses the from to specify that the underlying image will automatically execute the onbuild instruction content, which is equivalent to adding two instructions later.

2. Create Image

After writing the dockerfile, you can create a mirror from the Docker build command.
Basic format: Docker build[] option path.

The change command reads the dockerfile under the specified path (including subdirectories) and sends all content under that path to the Docker server, with the server creating the mirror.

It is generally recommended to place the Dockerfile directory as an empty directory.

You can let Docker ignore directories and files under the path through the. dockerignore file (one matching pattern per line).

Use-T to specify the label information for the Mirror.

Like what:

Docker Build-t Image_name/src/dockerfile_path

3. Example

The MySQL image is dockerfile as follows:

#本文参考了 "tutum" Dockerfile from
sshd
maintainer waitfish <dwj_zz@163.com>
#安装软件 
ENV debian_ FRONTEND noninteractive
RUN apt-get update && \
  apt-get-yq install mysql-server-5.6 pwgen && \
  rm-rf/var/lib/apt/lists/*

# Delete pre-installed database files
RUN rm-rf/var/lib/mysql/*

# Add folder under MySQL config file
MY.CNF/ETC/MYSQL/CONF.D/MY.CNF add
mysqld_charset.cnf/etc/mysql/conf.d/mysqld_charset.cnf

# adding MySQL Script
ADD import_sql.sh/import_sql.sh
ADD run.sh/run.sh
run chmod 755/*.sh

# Set environment variables, username and secret
env mysql_user admin
Env mysql_pass **random * *

setting environment variables in master-slave copy mode
env replication_master **false**
env replication_slave **false**
env Replication_user replica
ENV replication_pass replica

# setting allows mounted volumes that can be used to back up databases and configuration files
VOLUME  ["/ Etc/mysql ","/var/lib/mysql "]

# Sets the port that can be mapped, if it is inherited from our sshd image, the default will also open 22 port
EXPOSE 3306
CMD ["/run.sh "]


Summary

The Docker hub and the Docker pool community provide a number of dockerfile examples for our reference.

Subsequent articles will give some popular images for use.

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.