Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create a Docker image

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Docker image creation method

Docker image creation method there are three ways to create an image, namely, based on the existing image, based on local template, and based on Dockerfile. The following focuses on these three methods.

First, create based on the existing image

First of all, load the image into the container, package the programs and running environment running in the container to generate a new image, and remember the ID number of the container.

Command format: docker commit [options] Container ID/ name Warehouse name: [label]

Common options:

-m: description information  

-a: author information  

-p: stop the container during the build process

Example:

Docker create-it nginx / bin/bash # launch Container

Docker ps-a # View containers

Docker commit-m "new"-a "zhao" 6f69698fc083 nginx:test # create a new image

Docker images # View the image

Second, create based on the local template

Images can be generated by importing system template files, and templates can be downloaded from OPENVZ open source projects

Download address: http://download.openvz.org/template/precreated

Example:

The following is an example of importing the debian template package as a local image.

Wget http://download.openvz.org/template/precreated/debian-7.0-x86-minimal.tar.gz

Cat debian-7.0-x86-minimal.tar.gz | docker import-nginx:new

Third, create based on Dockerfile (key point)

Dockerfile is a file composed of a set of instructions, each instruction corresponds to a command in linux, and the Docker program will read the instructions in Dockerfile to generate a specified image.

The general structure of Dockerfile: basic image information, maintainer information, mirror operation instructions, instructions to execute when the container starts. Each line supports one instruction, each instruction can carry multiple parameters, and supports comments that begin with #.

Dockerfile operation instructions:

FROM image: specify the image on which the new image is based

MAINTAINER name: indicates the maintainer of the new image

RUN command: execute the command on the image on which it is based and submit it to the new image

CMD ["Program to run", "Parameter 1", "Parameter 2"]: specifies the command or script to run when the container is started

EXPOSE port number: specifies the port to be opened when a new image is loaded into Docker

ENV environment variable value: set the value of an environment variable, which will be used by the following RUN

ADD source file / directory destination file / directory: copy the source file to the target file, where the source file and Dockerfile are located in the same directory

COPY source file / directory destination file / directory: copy the source file of the local host to the destination directory, where the source file and Dockerfile are located in the same directory

VOLUME ["directory"]: create a mount point in the container

USER username / UID: specifies the user when the container is running

WORKDIR path: specify a working directory for subsequent RUN, CMD, and ENTRYPOINT

ONBUILD command: specifies the command to run when the generated image is used as a base image.

Dockerfile example:

Mkdir httpd create directory

Cd httpd # switch directories

Vim Dockerfile # write dockerfile

# based on basic image FROM centos# user information MAINTAINER this is httpd image # add environment package RUN yum install-y httpd# specify http port EXPOSE 8 add host run.sh to container ADD run.sh / run.shRUN chmod 755 / run.shCMD ["/ run.sh"]

Vim run.sh # write httpd startup script

#! / bin/bashrm-rf / run/httpd/*exec / usr/sbin/apachectl-D FOREGROUND

Docker build-t httpd:new. # create a new image (don't forget the one at the end)

Docker run-d-p 80:80 httpd:new # start the image

Docker ps # View

Web page verification

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report