The underlying mirror can be used to create Docker containers. Mirroring can be very basic, contains only the operating system, and can be very rich, including smart application stacks, ready to release. When you build a mirror using Docker, each command will form a new layer based on the previous command. These base mirrors can be used to create a new container. This article will take you step-by-step through the process of building a container from the Dockerfile, step-by-step, from the base image.
Docker Introduction
The Docker project provides advanced tools for building on top of the Linux kernel functionality and working together. The goal is to help development and operational personnel more easily deliver applications and their dependencies across systems across the host. Docker achieves this through the Docker container, a secure, lightweight container based environment. These containers are created by mirrors, and mirrors can be created manually by the command line or automatically by Dockerfile.
Dockerfiles
Dockerfiles is a script consisting of a series of commands and parameters that apply to the underlying mirror and eventually create a new mirror. They simplify the process from beginning to end and greatly simplify the deployment effort. The dockerfile begins with the from command, followed by the various methods, commands, and parameters of the follower. The output is a new mirror that can be used to create the container.
Dockerfile syntax
Before we dive into the dockerfile, let's take a quick look at Dockerfile's syntax and what they mean. What is grammar.
Very simply, in programming, syntax means a call to command, input parameters to allow the application to execute the program's grammar structure. These grammars are bound by rules, either clear or dark. Programmers follow the syntax code to interact with the computer. If a piece of program syntax is incorrect, the computer will not be recognized. Dockerfile uses simple, clear, and clean grammatical structures that are extremely easy to use. These grammars are self explanatory and support annotations. Dockerfile Syntax Example
Dockerfile syntax consists of two parts, comments and commands + parameters
# line blocks used for commenting
command argument argument.
A simple example:
# Print "Hello docker!"
RUN echo "Hello docker!"
Dockerfile command
Dockerfile has more than 10 commands that you can use to build mirrors, which are described briefly in the following sections.
ADD
The add command has two parameters, source, and destination. Its basic role is to copy files from the file system on the source system to the target container's file system. If the source is a URL, the contents of the URL are downloaded and copied to the container.
# Usage:add [Source directory or URL] [destination directory]
Add/my_app_folder/my_app_folder
Cmd
Like the Run command, CMD can be used to execute a specific command. Unlike run, these commands are not executed while mirroring is being built, but are invoked after the container is built with mirrors.
# Usage 1:cmd Application "argument", "argument", ...
CMD "echo" "Hello docker!"
EntryPoint
EntryPoint helps you configure a container to be executable, and if you combine the cmd command with the entrypoint command, you can remove "application" from the cmd command and simply reserve the parameters, which are passed to the entrypoint command.
# 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 and cmd can be overridden during *run*
CMD "Hello D ocker! "
EntryPoint Echo
Env
The env command is used to set environment variables. These variables exist in the form of "Key=value" and can be called by scripts or programs within the container. This mechanism has brought great convenience to the operation of the container.
# usage:env Key value
ENV Server_works 4
Expose
Expose is used to specify ports so that applications within the container can interact with the outside through the port.
# Usage:expose [port]
expose 8080
From
The from command may be the most important dockerfile command. The change command defines which underlying image is used to start the build process. The underlying mirror can be any mirror. If the underlying mirror is not found, Docker will attempt to find the mirror from Docker image index. The from command must be the first command of Dockerfile.
# Usage:from [image name] from
Ubuntu
Maintainer
I suggest that this command be placed at the beginning of the dockerfile, although theoretically it can be placed anywhere in the dockerfile. This command is used to declare the author and should be placed behind from.
# Usage:maintainer [name]
maintainer Authors_name
RUN
The Run command is a central part of the Dockerfile execution command. It takes a command as an argument and is used to create a mirror. Unlike the cmd command, the Run command is used to create a mirror (a new layer is formed above the previous commit layer).
# usage:run [command]
RUN aptitude install-y Riak
USER
The user command is used to set the UID of the run container.
# Usage:user [UID]
USER 751
VOLUME
The volume command allows your container to access the directory on the host.
# Usage:volume ["/dir_1", "/dir_2" ...]
VOLUME ["/my_files"]
Workdir
The Workdir command is used to set the running directory for the command specified by CMD.
# Usage:workdir/path
Workdir ~/
How to use Dockerfiles
Using Dockerfiles and manually using Docker daemon is as simple as running a command. Output as new mirror ID after script runs
# Build a image using the Dockerfile at current location
# Example:sudo Docker build-t [name].
sudo docker build-t My_mongodb.
Dockerfile Example One: Create a MongoDB mirror
In this section, we'll step through the creation of a dockfile, which can be used to build MongoDB mirrors and build mongodb containers.
Create a Dockerfile
Using the Nano text editor, let's create Dockerfile
sudo nano dockerfile
Define the file and its purpose
It is always necessary for the reader to make clear the purpose of dockerfile. To do this, we usually start writing dockerfile from the comments.
############################################################
# Dockerfile to build MongoDB container images
# Based on Ubuntu
############################################################
Setting up the underlying mirror
# Set The base image to Ubuntu from
Ubuntu
Define author
# File Author/maintainer
maintainer Example mcauthor
Set command and parameter download MongoDB
################## begin installation ###################### # Install MongoDB Following the instructions at mongodb docs # ref: http://docs.mongodb.org/ manual/tutorial/install-mongodb-on-ubuntu/# add the package verification key RUN APT-KEY ADV --KEYSERVER HKP://KEYSERVER.UBUNTU.COM:80 --RECV 7F0CEB10 # Add Mongodb to the repository sources list run echo ' deb http:// Downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen ' | tee /etc/apt/
Sources.list.d/mongodb.list # update the repository sources list once more Run apt-get update # install mongodb package (. Deb) Run apt-get install -y mongodb-10gen # create the default data directory RUN mkdir -p /data/db ##################### installation end #####################
Set MongoDB port
# expose the default port
expose 27017
# default port to execute the entrypoint (MongoDB)
CMD ["--port 27017"]
# Set default container command</span>
EntryPoint Usr/bin/mongod
Save Dockerfile, the following code is the full version of Dockerfile
############################################################ # dockerfile to build mongodb container images # based on ubuntu ################################################### ######### # set the base image to ubuntu From ubuntu # File Author / maintainer Maintainer example mcauthor # update the repository Sources list run apt-get update ################## begin installation ############
########## # install mongodb following the instructions at mongodb docs # ref: http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/# Add the Package verification key run apt-key adv --keyserver hkp://keyserver.ubuntu.com
: 80 --recv 7f0ceb10 # add mongodb to the repository sources list run echo ' Deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen ' | Tee /etc/apt/sources.list.d/mongodb.list # update the repository sources list once more run apt-get update # install mongodb package (. Deb) RUN Apt-get install -y mongodb-10gen # create the default data directory RUN mkdir -p /data/db ##################### installation end ##################### # expose the default port expose 27017 # default port to execute the entrypoint (MongoDB) cmd ["--port 27017"] # set default container command Entrypoint usr/bin/mongod
Building Mirrors
Using the above dockerfile, we are ready to start building MongoDB mirroring
sudo docker build-t My_mongodb.
Dockerfile Example Two: Create a nginx mirror Nginx brief
Nginx is a high-performance HTTP and reverse proxy server. It is popular in the industry because it is lightweight, easy to use, and easy to expand. Based on good architectural design, it can handle more requests than previous similar software. It can also be used to provide static file services, such as images, scripts, and CSS.
As with the previous example, we started with the base image, using the from command and the maintainer command
############################################################
# Dockerfile to build Nginx installed Containers
# Based on Ubuntu
############################################################
# Set the base image to Ubuntu from
Ubuntu
# File Author/maintainer
maintainer Maintaner Name
Install Nginx
# Install Nginx
# ADD application repository URL to
the ' default sources RUN Echo ' Deb Http://archive.ubuntu.com/ub Untu/raring Main Universe ">>/etc/apt/sources.list
# Update the repository
RUN apt-get update
# Insta ll necessary tools
RUN apt-get install-y Nano wget Dialog net-tools
# Download and install Nginx
RUN apt-get Install-y Nginx
Bootstrapping
After installing Nginx, we need to configure Nginx and replace the default configuration file
# Remove The default Nginx configuration file
RUN rm-v/etc/nginx/nginx.conf
# Copy A configuration file from the Current directory
ADD nginx.conf/etc/nginx/
# Append ' daemon off; ' To the beginning of the configuration
RUN echo "daemon off;" >>/etc/nginx/nginx.conf
# Expose Ports
expose
# Set ' The default command to Execu Te
# When creating a new container
CMD service Nginx start
The last Dockerfile
############################################################ # dockerfile to build nginx installed containers # based on ubuntu ############################################### ############# # set the base image to ubuntu From ubuntu # File Author / maintainer maintainer maintaner name # install nginx # Add Application repository url to the default sources RUN echo "deb Http://archive.ubuntu.com/ubuntu/ raring main universe " >> /etc/apt/ Sources.list # update the repository run apt-get update # Install necessary tools Run apt-get install -y nano wget dialog net-tools # Download and install nginx Run apt-get install -y nginx # Remove the Default nginx&nbsP;configuration file run rm -v /etc/nginx/nginx.conf # copy a configuration file from the current directory add nginx.conf /etc/nginx/# Append "daemon off;" to the beginning of the configuration run echo "daemon off;" >> /etc/nginx/nginx.conf # expose ports expose 80 # Set the Default command to execute # when creating a new container CMD Service nginx start
Using Dockerfile to automatically build nginx containers
Because we command Docker to replace the default profile with the Nginx profile of the current directory, we want to make sure that this new profile exists. In the directory where Dockerfile exists, create the nginx.conf:
sudo nano nginx.conf
Then replace the original content with the following:
Worker_processes 1;
Events {worker_connections 1024;}
HTTP {
sendfile on;
server {
listen;
Location/{
Proxy_pass http://httpstat.us/;
Proxy_set_header x-real-ip $remote _addr;
}
}
}
Let's save nginx.conf. We can then use Dockerfile and configuration files to build mirrors.
Via:digitalocean this site in addition to the note reproduced, are original site or compiled. Welcome to any form of reprint, but please be sure to indicate the source, respect for others to work together to create open source community. Reprint Please specify: The article reprinted from the Open source China Community [Http://www.oschina.net] This article title: Dockerfile Simple Edition encyclopedia, the bonus writes the example this article address: https://www.oschina.net/news/64396/ Dockerfile-instructions