Hand in hand to teach you to write Dockerfile, with a gift of MongoDB and Nginx Dockerfile writing examples
The base image can be used to create Docker containers. The image can be very basic, only contains the operating system, can also be very rich, including smart application stack, ready to publish. When you use Docker to build images, each command forms a new layer based on the previous command. These base images can be used to create a new container. This article will walk you through the process of building a container from dockerfile, step-by-step, one-layer from the base image.
About Docker
The Docker project provides advanced tools that are built on top of the Linux kernel functionality. The goal is to help development and OPS people deliver applications and their dependencies more easily across systems across the system. Docker achieves this goal through Docker containers, a secure, lightweight container-based environment. These containers are created by mirrors, and images can be created manually from the command line or automatically through Dockerfile.
Dockerfiles
Dockerfiles is a script that consists of a series of commands and parameters that are applied to the underlying image and eventually create a new image. They simplify the process from beginning to end and greatly simplify deployment. Dockerfile starts with the FROM command, followed by various methods, commands, and parameters. The output is a new image that can be used to create a container.
Dockerfile syntax
Before we go into the dockerfile, let's get a quick look at Dockerfile's syntax and what they mean.
What is grammar?
Very simple, in programming, syntax means a call command, input parameters to let the application execute the grammar structure of the program. These grammars are bound by rules or by a clear or dark rule. Programmers follow the syntax specification to interact with the computer. If a 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 can be self-explanatory and support annotations.
Dockerfile Syntax Examples
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 the image, and these commands are briefly described later in this article.
ADD
The add command has two parameters, source and target. Its basic function is to copy files from the file system of the source system to the file system of the target container. If the source is a URL, the contents of the URL will be downloaded and copied into the container.
# Usage: ADD [source directory or URL] [destination directory]
ADD /my_app_folder /my_app_folder
Cmd
Similar to the Run command, CMD can be used to execute a specific command. Unlike run, these commands are not executed during the mirroring build, but are called after the container is built with the image.
# 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 just keep the parameters, and the parameters will be 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 with CMD can be overridden during *run*
CMD "Hello docker!"
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 provides great convenience for running applications in containers.
# Usage: ENV key value
ENV SERVER_WORKS 4
EXPOSE
The expose is used to specify ports so that applications within the container can interact with the outside world via ports.
# Usage: EXPOSE [port]
EXPOSE 8080
From
The FROM command is probably the most important dockerfile command. The change command defines which base image to use to start the build process. The underlying image can be any mirror. If the underlying image is not found, Docker will attempt to find the image 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 the from.
# Usage: MAINTAINER [name]
MAINTAINER authors_name
RUN
The Run command is the core part of the Dockerfile execution command. It takes the command as a parameter and is used to create the mirror. Unlike the cmd command, the Run command is used to create a mirror (forming a new layer 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 running 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 run directory for the command specified by CMD.
# Usage: WORKDIR /path
WORKDIR ~/
How to use Dockerfiles
It's as simple as running a command using Dockerfiles and manually using Docker daemon. After the script runs, the output is the new mirror ID
# Build an image using the Dockerfile at current location
# Example: sudo docker build -t [name] .
sudo docker build -t my_mongodb .
Dockerfile Example one: Creating a MongoDB image
In this section, we'll step through the creation of a dockfile, which can be used to build a mongodb image and then build a MongoDB container.
Create a Dockerfile
Using the Nano text editor, let's create a dockerfile
sudo nano Dockerfile
Define the file and its purpose
It is always necessary for the reader to be clear about the purpose of dockerfile. To do this, we usually start writing dockerfile from the comments.
############################################################
# Dockerfile to build MongoDB container images
# Based on Ubuntu
############################################################
Set up the base image
# Set the base image to Ubuntu
FROM ubuntu
Define author
# File Author / Maintainer
MAINTAINER Example McAuthor
setting commands and Parameters 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 #####################
Setting up a 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
Build image
Using the dockerfile above, we can already start building MongoDB mirrors
sudo docker build -t my_mongodb .
Dockerfile Example two: Creating an Nginx image Nginx description
Nginx is a high-performance HTTP and reverse proxy server. It is popular in the industry because of its lightweight, easy-to-use, easy-to-expand. Based on the excellent architecture design, it can handle more requests than the previous similar software. It can also be used to provide static file services, tablets, scripts and CSS.
As in the previous example, we start 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
Installing Nginx
# 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
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 80
# Set the default command to execute
# 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 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
Automatically build Nginx containers with Dockerfile
Because we command Docker to replace the default configuration file with the Nginx configuration file of the current directory, we want to ensure that this new configuration file exists. Under the directory where Dockerfile exists, create nginx.conf:
sudo nano nginx.conf
Then replace the existing content with the following:
worker_processes 1;
events { worker_connections 1024; }
http {
sendfile on;
server {
listen 80;
location / {
proxy_pass http://httpstat.us/;
proxy_set_header X-Real-IP $remote_addr;
}
}
}
Let's save nginx.conf. We can then use the Dockerfile and configuration files to build the image.
This article is from the "eyes engraved with your Smile" blog, please be sure to keep this source http://dengaosky.blog.51cto.com/9215128/1854897
Teach you to write Dockerfile, with MongoDB and Nginx Dockerfile to write an instance