How to get Docker mirror image of the target software

Copyright: Huawei cloud All rights reserved Please indicate the source https://blog.csdn.net/devcloud/article/details/91491173

Abstract guidance Docker beginners, how to get the software you want to mirror the Docker. First find, can not find it yourself. Produce their own divided: a simple manner and DockerFile way. Docker started going to the pit to facilitate the students to use.

1, the service related terms 

2, Docker container

Docker appear before you to install a software, you need to download the software package, download the software library files, compile, install, configure, and ultimately have the program up and running. Once the intermediate steps have an error, the need to start all over again. Originally want to engage in business, but had to face a variety of environmental installation debugging, pain points can be imagined.

And after using Docker, you just need to pull a mirror, run a container, your software run up. All depends, are you ready to help in the mirror, you absolutely do not care how this software installed, only will be used on the line.

image.png                                               

Docker containers such technology application can run in the same way almost anywhere. Developers create and test a good container on their laptop, without any modification to run on virtual machines, physical servers or public cloud host production systems.

It can solve the problem of sequencing run various versions of the software in a variety of environments. For example, on the same VM GATK run two different versions of the software simultaneously. This traditional VM model is not available, which build complex business scenarios to gene sequencing, provides convenience. Because the environment if damaged by container technology, easy recovery.

2.1, Docker container mirror

Docker Docker container and mirrored relationship, the relationship is like software and processes. Run up called container, not run the file called mirroring.

A mirror Docker all run a minimum set of dependent software required. OS does not require the entire system into a mirror, so the mirror compared to Docker VM image will be much smaller.

2.2 How to obtain a corresponding software Docker image

To obtain a corresponding software Docker image and does not necessarily need to produce their own. There are many ways to get a Docker image:

  1. First is to search the warehouse from DockerHub

  2. Google search software can then name + docker keywords, there may be included Github repository

  3. Or else you can make your own, make your own mirror is divided into two sects. Snapshot faction faction vs DockerFile

  4. The final step is to help Huawei container mirroring team to help to help make friends.

Here we introduce you to each method.

3, the search from DockerHub

As the world's largest Docker image center, DockerHub website provides public Docker image download 400,000 + in various types of software, and the rate of weekly 5K continues to grow. So in addition to your own developed software, you can generally be found in the corresponding mirrored version here. https://hub.docker.com/

3e.jpg

For example, we need bwa software mirroring, you can get search:

2e.jpg

Select the desired version of the software on it.

PS: There are the following types of software, we recommend getting them from DockerHub, rather than produce their own.

3.1 basis running OS environment

Like a mirror Ubuntu, Suse, Centos and other such basic OS class, we recommend getting the official certification version directly from DockerHub.

image.png

3.2 basic programming language class

Like Mirror Java, Python, R Language, Golang and other such basic programming language class, we also recommend getting the official certification version directly from DockerHub.

image.png

3.3 basis of generic class software

But this kind of universal common software like Tomcat, Mysql, Ngnix etc. We also recommend obtaining official certification version directly from DockerHub.

image.png

4, from Google search

Although DockerHub base running OS environment can solve the problem 80% of the search software. However, some third-party software if located in a warehouse in the mirror, we can find the relevant image through Google search. Just need the software key name with docker can. For example :

image.png

See generally ranked first of which is DockerHub warehouse.

5, to produce their own Docker image

Docker produce their own image, there are two:

  1. Looking for a clean base image, such as Ubuntu, then landed inside the system to install software. Then make a whole snapshot, mirroring want to have a Docker software. Just as the Ghost CD.

  2. 第二种就是把软件安装的流程写成DockerFile,使用Docker build构建。

两个的构建原理是一样的,可以理解为第二种是对第一种的自动化。

5.1 打快照方式获得镜像(偶尔制作的镜像)

 image.png

示例图如上,具体操作: 

  1. 找一台主机,安装好Docker

  2. 启动一个空白的基础容器,并进入容器。比如启动一个 CentOS的容器。docker run -it centos.

  3. 执行安装任务。敲你平常敲的那些

  4. 退出容器。exit:退出容器

  5. 给刚才的容器,打个快照。docker commit -m "xx" -a "tsj" container-id tsj/image:tag

  • -a : 提交的镜像作者;

  • container-id : 刚才跑命令的容器id。可以使用 docker ps -a 查询得到;

  • -m : 提交时的说明文字;

  • tsj/image:tag : 仓库名/镜像名:TAG名

最后通过 docker images 查看得到的Docker镜像。

5.2      Dockerfile方式构建(经常更新的镜像)

上一步做镜像的方法,如果后续镜像都不变化,还行。如果经常要换(比如某个软件更新版本),那么你打镜像得累死。因为每变一次你就得重新执行那么多命令。

这时就需要使用自动化打镜像的方法。

image.png

就是把第一种打镜像的方法,用文件写出来(这个文件叫做DockerFile)。 然后执行:

docker build -t tsj/image:tag

命令,它就自动完成镜像制作了。

简单的DockerFile举例:

#Version 1.0.1

FROM centos:latest

 

MAINTAINER ***u "***[email protected]"

 

#设置root用户为后续命令的执行者

USER root

 

#执行操作

RUN yum update -y

RUN yum install -y java

 

#使用&&拼接命令

RUN touch test.txt && echo "abc" >>abc.txt

 

#对外暴露端口

EXPOSE 80 8080 1038

 

#添加文件

ADD abc.txt /opt/

 

#添加文件夹

ADD /webapp /opt/webapp

 

#添加网络文件

ADD https://www.baidu.com/img/bd_logo1.png /opt/

 

#设置环境变量

ENV WEBAPP_PORT=9090

 

#设置工作目录

WORKDIR /opt/

 

#设置启动命令

ENTRYPOINT ["ls"]

 

#设置启动参数

CMD ["-a", "-l"]

 

#设置卷

VOLUME ["/data", "/var/www"]

 

#设置子镜像的触发操作

ONBUILD ADD . /app/src

ONBUILD RUN echo "on build excuted" >> onbuild.txt

详细的可以参考:https://docs.docker.com/engine/reference/builder/

或者《华为云容器仓库SWR 最佳实践》

5.3      DockerFile基本语法

这里给出一些基础的信息

FROM:

指定待扩展的父级镜像(基础镜像)。除了注释以外,在文件开头必须是一个FROM指令,接下来的指令便在这个父级镜像的环境中运行,直到遇到下一个FROM指令。通过添加多个FROM命令,可以在同一个Dockerefile文件中创建多个镜像。

MAINTAINER:

声明创建的镜像的作者信息。用户名、邮箱。非必须。

RUN:

用来修改镜像的命令,常用来安装库、程序以及配置程序。一条RUN指令执行完毕后,会在当前镜像上创建一个新的镜像层,接下来对的指令会在新的镜像上继续执行。RUN 语句有两种形式:

  • RUN yum update:是在/bin/sh环境中执行的指令的命令

  • RUN ["yum", "update"]:直接使用系统调用exec来执行行。

  • RUN yum update && yum install nginx:使用&&符号将多条命令连接在同一条RUN语句中。

EXPOSE:

用来指明容器内进程对外开放的端口,多个端口之间使用空格隔开。运行容器时,通过参数-P(大写)即可将EXPOSE里所指定的端口映射到主机上另外的随机端口,其他容器或主机就可以通过映射后的端口与此容器通信。同时,我们也可以通过-p(小写)参数将Dockerfile中EXPOSE中没有列出的端口设置成公开的。

ADD:

向新镜像中添加文件,这个文件可以是一个主机文件,也可以是一个网络文件,也可以使一个文件夹。

 •第一个参数:源文件(夹)。如果是相对路径,它必须是相对于Dockerfile所在目录的相对路径。如果是URL,会先下载下来,再添加到镜像里去。

 •第二个参数:目标路径。如果源文件是主机上zip或者tar形式的压缩文件,Docker会先解压缩,然后将文件添加到镜像的指定位置。如果源文件是一个通过URL指定的网络压缩文件,则不会解压。

VOLUME:

在镜像里创建一个指定路径(文件或文件夹)的挂载点,这个容器可以来自主机或者其它容器。多个容器可以通过同一个挂载点共享数据,即便其中一个容器已经停止,挂载点也仍热可以访问。

WORKDIR:

为接下来执行的指令指定一个新的工作目录,这个目录可以使绝对目录,也可以是相对目录。根据需要,WORKDIR可以被多次指定。当启动一个容器时,最后一条WORKDIR指令所指的目录将作为容器运行的当前工作目录。

ENV:

设置容器运行的环境变量。在运行容器的时候,通过-e参数可以修改这个环境变量值,也可以添加新的环境变量:

 docker run -e WEBAPP_PORT=8000 -e WEBAPP_HOST=www.example.com ...

CMD:

用来设置启动容器时默认运行的命令。

ENTRYPOINT:

与CMD类似,也是用来指定容器启动时的默认运行的命令。区别在于:运行容器时添加在镜像之后的参数,对ENTRYPOINT是拼接,CMD是覆盖。

ENTRYPOINT [ "ls", "-l"]

docker run centos ==> docker run centos ls -l

docker run centos -a ==> docker run centos ls -l -a

• When we run the container can --entrypoint to cover a given Dockerfile in: Docker RUN gutianlangyu / the Test --entrypoint echo "the Hello world"

USER:

UID specified user operation or the next operation of the container and RUN, CMD, ENTRYPOINT instruction is like.

ONBUILD:

Trigger command. When constructing a mirror, the mirror Docker builders will all ONBUILD specified in the instruction command to save the metadata mirror in the command does not execute the process of constructing the current mirror. Only the heart of a mirror FROM instruction specify the parent when the mirror is a mirror, it will trigger the execution.

• Use this Dockerfile FROM to build a mirror image of the parent Mirror, Mirror constructs when:

•   . ONBUILD the ADD / App / src : automatic execution A DD / App / src.

5.4 Huawei cloud uploaded to the local mirror mirror warehouse

Huawei cloud services Docker image warehouse SWR, in its interface you have tips on how to submit a local mirror to the cloud.

 image.png

6 Huawei cloud container production team for help

If the customer already exists a certain volume of business, or start POC, can provide appropriate free production services.

If the amount of image production needs, providing training courses on how to make Docker image. And charge a reasonable fee.

Source: Huawei cloud community  Author: tsjsdbd

Guess you like

Origin blog.csdn.net/devcloud/article/details/91491173