.netcore docker of items, and a communication between the docker

In short:

  The company recently completed the new system, the system uses a micro-service architecture, due to the leadership that you want to service of docker. I will share the following findings came out, as if the wrong place, also please big brother a lot of guidance.

table of Contents:   

  1. What is a docker?
  2. Use docker What are the benefits?
  3. Communication between the use and the docker docker

1. What is a docker?  

  Docker is an open source application container engine that lets developers can package their applications and dependencies into a portable mirror, and then publish to any of the popular Linux or Windows machine can be virtualized. The container is completely sandbox ( sandbox: a virtual system program ) mechanism, will not have any interface with each other. In other words, we can use docker our systems and systems required for environment and dependent software packaged into an installation package can then be installed on linux or windows.

  About docker three core concepts:

  1. Warehouse: The warehouse stored image
  2. Mirror: similar to the system image
  3. Container: Example mirroring

  I especially like the kid on the beach to the river to play, usually with a bag, which installed touch with many shapes. After the wet sand, take a mold press, a block shape there, remember it is a five-pointed star favorite childhood die. Occasional mischievous little friend would I do deceives scattered shape, then I will not cry do not get mad, just pick up quickly and then make a mold, the air, he did not say. Sometimes a whim, think of a new shape, and then put it out, I'll take it down, then flew home to get hold of the new shape of the mold out.

  Here, I docker analogy beach, but my bag is like a warehouse, and the mold is a mirror, container naturally is to take the shape of the mold made out of blocks. But the real application development, you can put a mirror for us to understand the installation package of the system, container understood to run after the system is installed. These are my own personal understanding of the docker, if chiefs who think it is reasonable, but also like him to exchange.

2. Use docker What are the benefits?

  Previous systems have developed such a problem on a computer developer development system, ran on the server or on someone else's computer, you need with the environment, install a lot of software. Different operating systems as well as the installation method may be different. Etc. After the installation finished, a start found a lot of error, and then run on your computer properly (and here I met). There is, it is possible the company's file servers more relaxed, afraid of running multiple systems interference (where you can use a virtual machine, but the performance is not very good).

  The use docker, can develop a good system and what the system depends, packaged into a mirror, and then you can publish to the platform support docker. And docker can ensure that each container has its own resources, and other containers are isolated. You can use a different container to run applications that use different stacks, etc. (docker benefit too much, I will not list them).

Communication between the use and docker 3.docker

  How to install docker, where to download, and so I do not speak, it is not programmed white.

  Step 1: Download dotnet core Mirror: Docker RUN -it in the Microsoft / DOTNET , this is the start command, if there is no image will be automatically downloaded.

  Step 2: Create a userservice and emailservice the web api core projects, namely to create user email controllers and controller, modifying the Program class file

        

 

      

 

    Here email service depends on user services. After written and then published separately under the item

  Step Three: Write Dockerfile

    Respectively publish a folder at the same level to build a Dockerfile

    First of writing dockerfile emialservic

      

1 FROM microsoft/dotnet     #本镜像继承自哪个镜像
2 COPY publish /publish      # 拷贝当前文件夹下的publish文件夹到镜像中的根目录下的publish文件夹
3 WORKDIR /publish        #设置镜像的工作目录
4 EXPOSE 4001/tcp         # 暴露 tcp连接的4001端口
5 CMD ["dotnet","EmailService.dll","--databaseurl=132456789"]    #设置命令,以及一些参数

 

    在编写userservice的dockerfile

      

FROM microsoft/dotnet  
COPY publish /publish   
WORKDIR /publish
EXPOSE 5000/tcp   
CMD ["dotnet","UserService.dll"]

 

    有关dockerfile里面的指令解释,请阅读:https://www.cnblogs.com/linjiqin/p/8735230.html

  第四步:分别在目录下运行cmd,然后输入:docker build -t dotnet/emailservice .  和 docker build -t dotnet/userservice .  (注意后面的 . 号),然后镜像就制作完成了。

  第五步:因为emailservice依赖于userservice,所以emailservice和userservice要进行通信。

      docker通信有三种方式:

          1.通过docker容器ip加端口号。因为docker容器重启之后ip地址会改变,不方便迁移。所以不推荐使用。(除非使用固定ip)

          2.通过映射到主机的端口号。这种方式可以,但是会占用主机端口号

          3.docker的link机制。docker的link机制可以通过一个name来和另一个容器通信,link机制方便了容器去发现其它的容器并且可以安全的传递一些连接信息给其它的容器。

      我采用link机制,首先编写一个docker-compose.yml文件

      

version: '3'  #采用哪个docker-compose的版本
services:    #服务集合
  user-service:    #服务名,name
    image: dotnet/userservice    #启动哪个镜像
    ports:        #端口映射设置,这里是主机的6001端口映射到docker里的5000端口
    - "6001:5000"
  email-service:    #服务名,name:
    image: dotnet/emailservice  #启动哪个镜像
    ports:          #端口映射设置,这里是主机的4001端口映射到docker的4001端口
    - "4001:4001"
    links:        #依赖哪个服务
    - "user-service"

  然后emailservice可以通过:http://user-service:5000端口来访问userservice服务 。我在前面的代码中已经设置好了,就不用了

   运行命令:docker-compose up,就行了

结果图如下

  

 

 

     

Guess you like

Origin www.cnblogs.com/norain/p/docker_comunication.html