Docker introduction and practical explanation

This article is reproduced content, the original address is: https://blog.csdn.net/relax_hb/article/details/69668815


Briefly

Docker is an open source application container engine that allows developers to package their applications and dependencies into a portable container, which can then be distributed to any popular Linux machine, and can also be virtualized. Containers are completely sandboxed and do not have any interface with each other (similar to iPhone apps). There is almost no performance overhead and can be easily run in machines and data centers. Most importantly, they do not depend on any language, framework including system.

What is Docker? 

Docker is a cloud open source project based on the Go language. It was born in early 2013 and was originally initiated by dotCloud.

Docker achieves the "package once, run everywhere" at the application component level by managing the life cycle of application components such as packaging, distribution, deployment, and operation. The application component here can be a Web application, a set of database services, or even an operating system or a compiler.

Docker consists of three cores: Image, Container, and Repository.

Why use Docker?

Faster delivery and deployment (using docker, developers can use images to quickly build a standard development environment; after development is complete, test and operations personnel can directly use the same environment to deploy code.)

Easier migration and scaling (docker containers can run on almost any platform, including physical machines, virtual machines, public clouds, private clouds, PCs, servers, etc. Applications can be easily migrated across different platforms)

Simpler update management (using Dockerfile, you only need to modify a small configuration, you can replace a lot of update work in the past)

Compared with traditional virtual machines



Scenario Example - Traditional Development Process



Scenario example - docker environment development process



Install the docker program under Linux

Environmental preparation

  1. Linux7 or above or cent OS6 and above

  2. Kernel 3.1.0 or above

  3. 64 - bit operating system

install docker

  1. yum install  yum install   docker - engine

  2 , curl script installation  



Docker command usage → image

Create an image   

  1. Create a container based on an existing image

          docker commit [options] container[repository]

         option:-a, --author="" author information

  -m,--message="" submit message

  -p, --pause=true Pause the container on commit

  2. Import and create based on local template

          docker load < ***.tar -- local template file tar

  3. Build an image based on the Dockerfile file
          docker build –t image-name basedir





Docker command usage → delete image

delete mirror   

  dockerrmi image

  dockerrmi –f image –强制删除镜像

  其中image可以为标签或者ID

注意:

  用dockerrmi删除镜像时,首先删除容器,再删除镜  像。 




Docker命令用法→容器

创建 |启动容器

docker  create image 创建的容器是停止状态

docker  start/stopcontainer_id启动/停止容器

docker  run image创建并启动容器

备注:image-可以为镜像标签或Id

 container_id为容器id


Docker数据卷→介绍

数据卷是一个特别指定的目录,该目录利用容器的UFS文件系统可以为容器提供一些稳定的特性或者数据共享。数据卷可以在多个容器之间共享。

       创建数据卷,只要在dockerrun命令后面跟上-v参数即可创建一个数据卷,当然你也可以跟多个-v参数来创建多个数据卷,当创建好带有数据卷的容器后,你就可以在其他容器中通过--volumes-froms参数来挂载该数据卷了,而不管该容器是否运行。你也可以在Dockerfile中通过VOLUME指令来增加一个或者多个数据卷。

Docker基础命令用法总结



Docker-dockerfile构建镜像用法

介绍Dockerfile的基本语法结构Dockerfile有以下指令选项:

•FROM 

  FROM指定构建镜像的基础源镜像,如果本地没有基础镜像会从docker公共  库中下载镜像

•RUN

  RUN指令将在当前镜像基础上执行指定命令

•CMD

  CMD的目的是为了在启动容器时提供一个默认的命令执行选项。如果用户启  动容器时指定了运行的命令,则会覆盖掉CMD指定的命令

•EXPOSE

  Docker 服务端容器对外映射的本地端口,需要在dockerrun 的时候使用-p或  者-P选项生效

•ENV

  ENV指定一个环节变量,会被后续RUN指令使用,并在容器运行时保留

•ADD

  ADD复制本地主机文件、目录或者远程文件URLS从并且添加到容器指定路  径中,支持正则模糊匹配    

•COPY

   COPY复制新文件或者目录从并且添加到容器指定路径中 。用法同ADD,唯  一的不同是不能指定远程文件 URLS

•VOLUME

   VOLUME创建一个可以从本地主机或其他容器挂载的挂载点

•WORKDIR

   WORKDIR为后续的RUN、CMD、ENTRYPOINT指令配置工作目录。可以使  用多个WORKDIR指令,后续命令如果参数是相对路径,则会基于之前命令指  定的路径

DockerFile部署tomcat应用服务器





启动docker镜像

dockerrun –d –p 8080:8080 u-tomcat

-d --标示后台进程运行–p端口映射宿主8080→容器8080端口



出现上图所示表示启动成功,用http://ip:8080/访问,显示如图说明部署tomcat成功。



注意:

    1dockerfile构建镜像使用ADD指令复制文件到镜像    目录中 文件必须与dockerfile文件同目录。

    2dockerinspect -f “{{.Volumes}}”容器id 这里可   存放tomcat部署的war重启容器自动部署。

重启容  器命令:dockerrestart容器id


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325503557&siteId=291194637