Customize your own Docker image

A good script is the starting point of a good movie. This time we will be a screenwriter. -Three minutes of programming "

Overview

Cast: Nginxcontainer, one who would say hello worldthe htmlfile.

Script: A Dockerfilefile named , which records the entire process of modification, installation, construction, and operation. We will use this script to build and customize the image.

Scene: dockerenvironment.

list of actors

The number one actor index.htmlcame out and said something to everyoneHello World

<h1>Hello World</h1>

The second actor nginx:alpine, he is a ready-made docker image. We are familiar with nginx:latesthis little brother. He is better than his brother in three places:

  1. The latest version of nginx mirror is used, and the function is nginx:latestexactly the same.

  2. alpineThe mirror image uses the Alpine Linuxkernel, which is much smaller than the ubuntukernel.

  3. nginx:alpineSupported by default http2.

script

The main reason for the success of a movie is to have a good story. Let's take a look at the script.

Creating a Dockerfile file is generally divided into four parts: basic image information, maintainer information, image operation instructions and instructions to be executed when the container is started. The content is as follows

# 基础镜像信息

FROM nginx:alpine

#
 维护者信息

MAINTAINER docker_user [email protected]

#
 镜像操作指令

COPY . /usr/share/nginx/html

RUN echo hello
  • FROMnginx:alpineCreate a layer from the Docker image.

  • MAINTAINERMark the information of the mirror maintainer.

  • Mirror operation instructions:

  1. COPY Copy the files in the current directory to /usr/share/nginx/html, as to why this directory is because the official website mirror uses this directory as the static website deployment directory.

  2. Format RUN or RUN ["executable", "param1", "param2"]; the former will run the command in the shell terminal, that is /bin/sh -c, the latter will use the execexecution, which is recommended.

  • Executing instructions when the container starts:  CMDCommand, the usage is the RUNsame, it is used to specify the operation command when running the container, because we are based on the nginx image to make our image, which is equivalent to adding a shell, where the nginx image has been written by ourselves Startup method, so we don't have to write it again.

Filming

After the script is written, execute the command: in the folder path where the Dockerfile is located docker build --tag name:tag ., and you can build a mirror as described. name is the name of the mirror, tag is the version or tag number of the mirror, if not written, it means last. Note that there is a space and dot after it.

# 用法

docker build [选项]  路径

Mirroring

$ docker build -t webserver-image:v1 .

Sending build context to Docker daemon 3.072kB

Step 1/4 : FROM nginx:alpine

 ---> bfba26ca350c

Step 2/4 : MAINTAINER docker_user [email protected]

 ---> Using cache

 ---> a8b84ca6d5a1

Step 3/4 : COPY . /usr/share/nginx/html

 ---> b25123c06e56

Step 4/4 : RUN echo hello

 ---> Running in 7c86a7bca405

hello

Removing intermediate container 7c86a7bca405

 ---> 55bcba5b3ab9

Successfully built 55bcba5b3ab9

Successfully tagged webserver-image:v1

Use docker imagescommand to view mirror

$ docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

webserver-image v1 55bcba5b3ab9 44 seconds ago 20.5MB

nginx alpine bfba26ca350c 2 weeks ago 20.5MB

Specify the webserver-image:v1mirror, -drun the container in the background, and map the mirrored port 80 to the local port 80.

$ docker run -d -p 80:80 webserver-image:v1

fe5dd3369f7766cf078c4049cad1c99fc444cac56752d2a1393b64fd46e43c4d

Check the running effect

$ curl 0.0.0.0:80

<h1>Hello World</h1>


image

other

.gitignoreAnyone who has learned git knows that files can be ignored for tracking, and docker also has similar files named .dockerignore, for example, write the following ignored files and folder contents in this file

.git/
logs


Guess you like

Origin blog.51cto.com/15076235/2608329