Dockerfile How to use

Source: Internet
Author: User
Tags require centos port number docker run x11vnc

Dockerfile is a text-formatted configuration file that allows users to quickly create custom images using Dockerfile.

I. BASIC structure of dockerfile
Dockerfile consists of a row of line command statements and supports comment lines that begin with #.
In general, Dockerfile is divided into four parts base image information Maintainer information mirror operation instruction container start execution instruction

(1) Example of Dockerfile Nginx

Install Nginx, apache2, openssh-server on the basis of the CentOS parent image

# Nginx

# The first line must be specified based on the mirroring base from
CentOS

# Maintainer information
maintainer Hongxue hongxue@xxxx.com

# Mirror operation Instructions
RUN yum-y install Nginx apache2 openssh-server

(2) Dockerfile Firefox and VNC examples

On the basis of the CentOS parent image, install Firefox and VNC software, after launch, the user can use the 5900 port via VNC mode Firefox

# Firefox over VNC out of

CentOS

# Install VNC,XVFB in order to create a ' fake ' display and Firefox
RUN yum-y i Nstall X11vnc xvfb Firefox
run mkdir/.vnc

# Setup a password
run x11vnc-storepasswd hahaha ~/.vnc/passwd
  
    #Autostart Firefox (might not being the best to, but it does the trick)
RUN bash-c ' Echo ' Firefox >>/.BASHRC ' C8/>expose 5900
CMD ["X11vnc", "-forever", "-USEPW", "-create"]
  

Ii. directives

The general format of the instruction is instruction arguments, which includes the from, maintainer, run, etc.

(1) from

Format: from
<image> or from <image>:<tag>

The first instruction must be a from instruction. Also, if multiple mirrors are created in the same dockerfile to use multiple from directives (once per mirror)

(2) Maintainer

Format:
maintainer <name>

Specifies that the information is maintained

(3) RUN

Format:
RUN <command> 

The former will run the command in the shell terminal, that is/bin/sh-c, while the latter is executed using EXEC. Specifies that the use of other terminals can be implemented in a second way, such as Run ["/bin/bash", "-C", "echo Hello"]
Each run instruction executes the specified command on the current mirror and submits it as a new mirror.

(4) CMD

Supports three formats:
CMD ["Executable", "param1", "param2"] using exec execution, recommended way.
CMD command param1 Prarm2 executed in/bin/sh for applications
that require interaction CMD ["Prarm1", "param2"] provides default parameters for EntryPoint

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.
If the user specifies a command to run when the container is started, the command specified by CMD is overwritten.

(5) EXPOSE

Format:
EXPOSE <port> [<port> ...]
Example:
EXPOSE 20 80 8443

Tell the Docker server container the port number exposed for use by the connected system. When you start the container, you need to automatically assign a port to the specified port via the-p,docker host, and use-p to specify which local port to map over.

(6) ENV

The format is:
ENV <key> <value>
Specifies an environment variable that will be used by subsequent run instructions and maintained while the container is running

(7) ADD

Format:
ADD <src> <dest>

The command copies the ' Dest ' in the container to the specified ' src '. Where ' src ' can be a relative path (file or directory) of the directory where the dockerfile resides; it can also be a URL; it can also be a tar file

(8) COPY

Format:
COPY <src> <dest>

The ' src ' of the local host is copied as ' Dest ' in the container and is created automatically when the destination path does not exist.
When using a local directory as the source directory, it is recommended to use the copy

(9) EntryPoint

There are two formats:
entrypoint ["Executable", "param1", "param2"]
entrypoing command param1 param2 (executed in shell)

Configures the commands that are executed after the container is started, and cannot be overridden by parameters provided by Docker run.
There can be only one entrypoint in each dockerfile, when more than one entrypoint is specified, only the last one takes effect.

(Ten) VOLUME

Format:
VOLUME ["/data"]

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.

(one) USER

Format is:
USER daemon

Specifies the user name or UID when the container is run, and subsequent run uses the specified user
When the service does not require administrator rights, you can specify the running user by this command. And you can create the required users before, for example:
RUN groupadd-r postgres && useradd-r-G postgres postgres

(Workdir)

Format is:
Workdir/path/to/workdir

Configure the working directory for subsequent run, CMD, entrypoint directives
Multiple Workdir directives can be used, and subsequent commands, if the parameters are relative paths, are based on the path specified by the previous command.

(Onbuild)

Format:
onbuild [instruction]

Configure the action instructions that are executed when the mirror being created is the base image of the other newly created image

Third, create the image
After writing the completed Dockerfile, you can create the image through the Docker build command.
The basic format is the Docker build [options] path, which reads the dockerfile under the specified path and sends all content under that path to the Docker server, which is created by the server side to create the mirror. Therefore, it is generally recommended to place the Dockerfile directory as an empty directory

(1) See if the Dockerfile exists in the directory
[root@docker ~] #cd/dockerfile/
[root@docker Dockerfile] #ls
Dockerfile

(2) Execute the command to build
[Root@docker Dockerfile] #docker build-t nginx_image.
The build for the previous command builds the image, and the parameter T specifies the mirror name. The path to Dockerfile is shown
below to see build success

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.