The creation of Docker image and private warehouse

1, How to create docker image

1. Create based on existing image

2. Create based on local template

3. Create based on dockerfile

4. Building private warehouse and its usage

1, How to create docker image

Docker image is not only the core technology of docker, but also the standard format of application publishing. A complete docker image can support the operation of a docker container. In the whole process of using docker, you can operate in a container after entering a finalized container. The most common operation is to install application services in the container. If you want to migrate the installed services, you need to generate a new image of the environment and built services.

There are three ways to create an image: Based on an existing image, based on a local template, and based on a dockerfile. Next, I will write down these methods in turn.

1. Create based on existing image

Based on the existing image creation is mainly to use the docker commit command. In essence, it is to package the program running in a container and the running environment of the program to generate a new image.

The command format is as follows:

Docker commit [options] container ID / name warehouse Name: [label]

Common options are as follows:

-m: Description information;

-a: Author information;

-p: Stop container during build

To create a new image based on an existing image:

1) Start an image, make changes in the container, and then submit the modified container as a new image. Remember the ID number of the container, as follows:

[root@centos01 ~]# Docker images <! -- view local image -- >
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
hub.c.163.com/public/centos   6.7-tools           b2ab0ed558bb        3 years ago         602 MB
[root@centos01 ~]# docker run -it -d --name centos6.7 hub.c.163.com/public/centos:6.7-tools  
     <!--be based on docker Host image runs a container-->
e70418f794378109bb583e2d9171b55d7fd747460cff2819f1dffb9df04dac4c
[root@centos01 ~]# Docker ps <! -- to view the running containers plus - a parameter is to view all containers -- >
CONTAINER ID        IMAGE                                   COMMAND                  CREATED              STATUS              PORTS               NAMES
e70418f79437        hub.c.163.com/public/centos:6.7-tools   "/usr/bin/supervisord"   About a minute ago   Up About a minute   22/tcp              centos6.7
[root@centos01 ~]# Docker exec - it centos6.7 / bin / bash <! -- log in to the newly created container -- >
[root@e70418f79437 /]# Echo "lyx" > / etc / lyx. TXT <! -- write test data -- >
[root@e70418f79437 /]# Exit <! -- exit container -- >
exit
[root@centos01 ~]# docker commit -m "newnamed" -a "lyx" centos6.7 centos6.701 
               <!--use docker commit Command to create a new image-->
sha256:305f99b50a1e8844ea21a2411649108f662ad5d42c35fa9ff2cf3608172d1590
[root@centos01 ~]# Docker images <! -- check whether the image just created is generated -- >
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
centos6.701                   latest              305f99b50a1e        32 seconds ago      602 MB
hub.c.163.com/public/centos   6.7-tools           b2ab0ed558bb        3 years ago         602 MB
[root@centos01 ~]# Docker create - it centos6.701 / bin / bash <! -- create a container based on the newly created image -- >
eb6522ec046e9b3c7bfcb1edb5c84545aa8264f27075c019ee89da9150039e2f
[root@centos01 ~]# Docker ps <! -- view all containers, get the container id just created -- >
[root@centos01 ~]# docker ps -a
CONTAINER ID        IMAGE                                   COMMAND                  CREATED             STATUS              PORTS               NAMES
eb6522ec046e        centos6.701                             "/bin/bash"              55 seconds ago      Created                                 condescending_darwin
e70418f79437        hub.c.163.com/public/centos:6.7-tools   "/usr/bin/supervisord"   9 minutes ago       Up 9 minutes        22/tcp              centos6.7
[root@centos01 ~]# Docker start eb6522ec046e <! -- start container -- >
eb6522ec046e
[root@centos01 ~]# Docker exec - it EB6 / bin / bash <! -- log in to the newly created container -- >
[root@eb6522ec046e /]# Cat / etc / lyx. TXT <! -- verify whether the data written by the previous container is still -- >
lyx
[root@eb6522ec046e /]# Exit <! -- exit container -- >
exit

2. Create based on local template

The image can be generated by importing the operating system template file, and the template can be imported from the OPENVZ open source project , or https://wiki.openvz.org/Download/template/precreated , the link of OPENVZ open source project is preferred;

Create an example based on the local template:

1) Download the mini system template of centos 7, and use the docker Import command to import it as a local image:

[root@centos01 ~]# wget https://download.openvz.org/template/precreated/centos-7-x86_64-minimal.tar.gz   
................<!--Omit part of the content-->
[root@centos01 ~]# Ls | grep CentOS <! -- confirm that the template compression package of centos7 has been downloaded -- >
centos-7-x86_64-minimal.tar.gz
[root@centos01 ~]# cat centos-7-x86_64-minimal.tar.gz |docker import - docker:new 
             <!--use docker Import command import as local mirror-->
sha256:6d662f735ee8efbd53f23e017fdb5c777303aaab93a085ee6c10d9fcb71b4ba8
[root@centos01 ~]# Docker images | grep new <! -- View imported images -- >
docker                        new                 6d662f735ee8        27 seconds ago      435 MB
<!--At this point, you can use this image to create a container and deploy the required functions-->
[root@centos01 ~]# Docker run - it - D docker: new / bin / bash <! -- run a container with a new image -- >
0c10ee4230983b020a0268f40d4523685226ee570271e71a5b5d24ec57e8f46a
[root@centos01 ~]# Docker PS | grep new <! -- view the container id just running -- >
0c10ee423098        docker:new                              "/bin/bash"              About a minute ago   Up About a minute                       nostalgic_wescoff
[root@centos01 ~]# Docker exec - it 0c1 / bin / bash <! -- use the first three digits of ID to log in to the running container -- >
[root@0c10ee423098 /]# ls 
bin  boot  dev  etc  fastboot  home  lib  lib64  ......  <!--see-->
[root@0c10ee423098 /]# Exit <! -- exit container -- >
exit

3. Create based on dockerfile

Dockerfile is a file composed of a set of instructions. Each instruction corresponds to a command in Linux. The docker program will read the instructions in the dockerfile to generate the specified image.

The dockerfile structure is roughly divided into four parts:

Basic image information;

Maintainer information;

Image operation instruction;

Execute the command when the container starts;

Each line of dockerfile supports one instruction, each instruction can carry multiple parameters, and comments beginning with "#" are supported;

Introduction to configuration items in dockerfile:

[root@centos01 ~]# docker tag docker:new centos7.lyx
[root@centos01 ~]# docker images | grep lyx
centos7.lyx                   latest              6d662f735ee8        9 minutes ago       435 MB
[root@centos01 ~]# vim Dockerfile
FROM centos           <!--The first line must indicate the underlying mirror based on which it must exist-->
MAINTAINER The centos project <lyx@centos.org>    <!--Maintain the user information of the image-->
<!--Here are the operation instructions of the image-->
RUN yum -y update
RUN yum -y install openssh-server
RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config
RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
EXPOSE 22       <!--Open port 22-->
CMD ["/usr/sbin/sshd","-D"]     <!--Execute command when starting container-->

When writing a dockerfile, there is a strict format to follow: the first line must use the FROM instruction to indicate the name of the image on which it is based; then use the MAINTAINER instruction to maintain the user information of the image; then the mirror operation instructions, such as the RUN instruction, will add a new layer to the basic image for each instruction; finally, use the CMD instruction to specify the boot container Command operation to RUN when.

dockerfile has more than ten commands that can be used to build images. The common instructions are as follows:

An example of using dockerfile -- using dockerfile to create an apache image and run it in a container

1) Create a mirror and load it into a container to run:

[root@centos01 ~]# MKDIR Apache <! -- create working directory -- >
[root@centos01 ~]# CD Apache / <! -- switch to the new directory -- >
[root@centos01 apache]# VIM dockerfile <! -- write dockerfile -- >

FROM centos                <!--Base image based on centos-->
MAINTAINER the centos project <lyx@163.com>     <!--Maintain the user information of the image-->
RUN yum -y update            <!--use yum Update-->
RUN yum -y install httpd        <!--Image operation instruction installation apache software package-->
EXPOSE 80                         <!--Open port 80-->
ADD index.html /var/www/html/index.html <!--Local home page file index.html Copy to mirror-->
ADD run.sh /run.sh      <!--Run script locally run.sh Copy to mirror-->
RUN chmod 775 /run.sh       <!--Give script execution permission-->
RUN systemctl disable httpd             <!--set up apache Service does not start on its own-->
CMD ["/run.sh"]             <!--Execute script when starting container-->
<!--After entering the above information, save and exit-->
[root@centos01 apache]# docker images | grep centos     
<!--Ensure local centos Because of the dockerfile The image is specified in the file-->
centos7                       system              c065d5c0571d        About an hour ago   435 MB
[root@centos01 apache]# VIM run.sh <! -- write execution script content -- >
#!/bin/bash
rm -rf /run/httpd/*              <!--clear httpd Cache for-->
exec /usr/sbin/apachectl -D FOREGROUND           <!--start-up apache service-->
[root@centos01 apache]# Echo "dockerfile test" > index. HTML <! -- write a homepage file -- >
[root@centos01 apache]# Ls <! -- make sure there are three files in the current directory -- >
Dockerfile  index.html  run.sh
<!--When the above preparation is completed, you can use docker  build Command to create a mirror, as follows:-->
[root@centos01 apache]# docker build -t httpd:centos .     
<!--Notice that there is a“.",Indicates the current path. If it is not added, an error will be reported.-->
<!--Among them“-t"Option to specify the label information for the mirror-->
Sending build context to Docker daemon 4.096 kB
Step 1/10 : FROM centos
Trying to pull repository docker.io/library/centos ... 
latest: Pulling from docker.io/library/centos
  .................. <!--Omit part of the content. It needs to wait for a few minutes. At this time, the system is executing dockerfile Instructions in file-->
    <!--If there is red information in the display information, as long as the configuration file is correct, it is normal (yes yum Information for)-->
Removing intermediate container 81a3d6c9d3db
Step 10/10 : CMD /run.sh
 ---> Running in 5cdc467fd874
 ---> 5d56b826432d
Removing intermediate container 5cdc467fd874
Successfully built 5d56b826432d
<!--When the above prompt appears, the new image has been created successfully-->
<!--Throughout the creation process, you can see each run dockerfile The instructions in add a new layer to the initial image-->
[root@centos01 apache]# docker run -d -p 81:80 httpd:centos  
<!--Load the newly generated image into the container to run-->
<!--Among them“-p"Option to map local port 81 to port 80 in the container-->
192cd783028dcb3013ebb40b65ba8450e695e424e700a13cb8a44bb84af3e71a
[root@centos01 apache]# Docker PS - a <! -- query whether the container is running -- >
CONTAINER ID        IMAGE                      COMMAND                  CREATED              STATUS              PORTS                       NAMES
192cd783028d        httpd:centos               "/run.sh"                About a minute ago   Up About a minute   0.0.0.0:81->80/tcp          gallant_khorana
                  <!--Omit part of the content-->

At this point, you can see that the newly generated image has been loaded and run in the container. The IP address of the local machine (the host, not the docker container) is 192.168.1.1 At this time, the client accesses 192.168.1.1 Port 81 of is equivalent to port 80 of the container just running, as follows:

4. Building private warehouse and its usage

As the number of key creation images increases, there needs to be a place to save the image, which is the warehouse. At present, there are two kinds of warehouses: public warehouse and private warehouse. The most convenient is to use public warehouse to upload and download. Downloading the image of public warehouse does not need to be registered, but when uploading, it needs to be registered. Here's how to create a private warehouse.

The method is as follows:

[root@centos01 ~]# docker pull  registry:2   <! -- download the image of registry: 2 -- >
[root@centos01 ~]# docker run -itd --name registry --restart=always  -p 5000:5000 -v /registry:/var/lib/registry registry:2
<!--p: Port mapping (host port in front: container exposed port in back)-->
<!---v: Mount directory (the directory of the host before: the directory of the container after) automatically creates the directory of the host-->
[root@centos01 ~]# docker tag centos:nginx 192.168.1.1:5000/centos:nginx
[root@centos01 ~]# vim /usr/lib/systemd/system/docker.service 
ExecStart=/usr/bin/dockerd --insecure-registry 192.168.1.1:5000
<!--Modify the original configuration file to add an insecure warehouse. The address of the host is IP Address and 5000 port-->
[root@centos01 ~]# systemctl daemon-reload 
[root@centos01 ~]# Systemctl restart docker <! -- restart docker Service -- >
[root@centos01 ~]# Docker ps <! -- make sure the registry container is working properly -- >
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
2645c31b2306        registry:2          "/entrypoint.sh /etc..."   21 minutes ago      Up 42 seconds       0.0.0.0:5000->5000/tcp   registry
​
[root@centos01 ~]# docker push  192.168.1.1:5000/centos : nginx <! -- upload image to private warehouse -- >

Private warehouse building completed, verification successful! Open another docker host to download for testing:

[root@centos01 ~]# vim /usr/lib/systemd/system/docker.service 
ExecStart=/usr/bin/dockerd --insecure-registry 192.168.1.1:5000
<!--Modify the original configuration file to add an unsafe warehouse. The address is registry Host's IP Address and 5000 port-->
[root@centos01 ~]# systemctl daemon-reload 
[root@centos01 ~]# Systemctl restart docker <! -- restart docker Service -- >
[root@centos01 ~]# docker run -itd --name nginx 192.168.1.1:5000/centos:nginx nginx -g "daemon off;"
<!--Create container based on image-->
[root@centos01 ~]# Docker inspect nginx <! -- View container details -- >
[root@centos01 ~]# curl  172.17.0.3           <! -- Test Access -- >

Private warehouse is the completion of building! Test complete!

————————Thank you for reading————————

Tags: Linux Docker CentOS Apache Nginx

Posted on Thu, 14 May 2020 05:26:42 -0400 by Tom_LR