Run the ASPDOTNETCOREMVC program on Docker-part5: Using Docker-compose

Source: Internet
Author: User
Tags dotnet docker ps

In the previous part, " Run the ASPDOTNETCOREMVC program on Docker-part4: Load Balancing ", We have several more complex steps on the Docker platform to achieve the load balance of the website program, configuration steps are more. If the actual site is less, the overall architecture is relatively simple case, this does not have much problem, if the application of more time, will be prone to error. At this point we may think of writing scripts to automate, of course, this is possible. Docker, however, has been good for us and has provided us with docker-compose capabilities that enable us to manage complex applications, including containers, networks, volume, and so on.

Preparatory work

Remove the previous part of the container, the network, and the volume that we created (don't feel pity, docker-compose can be easily implemented later)

Docker Rm-f $ (Docker Ps-aq)
Docker Network RM $ (Docker network Ls-q)
Docker Volume RM $ (Docker volume ls-q)

Installing Docker-compose

On the Linux platform, you need to manually install the following commands:

sudo curl-l https://github.com/docker/compose/releases/download/1.17.1/docker-compose-' uname -S '-' uname-m '-o/usr/local/bin/docker-compose

sudo chmod +x/usr/local/bin/docker-compose

(the latest version can be from here https://github.com/docker/compose/releases learned)

Run docker-compose–version Check for success after installation

Preparing the MVC Site program

Can be downloaded from here

Https://github.com/shenba2014/AspDotNetCoreMvcDocker/tree/docker-compose

And then execute

dotnet Restore

Bower Install

One will use this site program to create a mirror and the corresponding container

Create a Docker-compose configuration file

New Docker-compose.yml file (can be created directly on the host server where Docker resides)

Version: "3"

Volumes

Productdata:

Networks

Frontend

Backend:

Services

Mysql:

Image: "mysql:8.0.0"

Volumes

-Productdata:/var/lib/mysql

Networks

-Backend

Environment:

-Mysql_root_password=password

-bind-address=0.0.0.0

This file is self-descriptive enough to define what we used in the previous section, or simply to explain

Version: The revision number, currently the latest is 3.0

volumes: the previous " Run the ASPDOTNETCOREMVC program on Docker-PART3: Use a separate storage container "has been introduced for storing data outside the Docker container .

Networks: As stated in the previous part, two networks were defined: Frontend and backend

Services: Defines the container that will be used, where only the MySQL container is defined, which is consistent with the parameters that create the container using the Docker create command. Refer to the previous part, " to run the ASPDOTNETCOREMVC program on Docker-part4: Load Balancing ."

Docker-compose Performing builds

Execute the following build command

Docker-compose–f DOCKER-COMPOSE.YML Build

Output results

Warning:some networks were defined but is not used by any service:frontend, backend

MySQL uses an image, skipping

The warning message here means that the two networks we have defined have not yet been used by any of the service components.

Running this command is just a definition, and does not really create a volume, network, or container.

Next run the Docker-compose.yml defined app to actually create the content

Docker-compose-f docker-compose.yml up

Can be understood as starting the component that we just defined, and then there will be a bunch of output content

Warning:some networks were defined but is not used by any service:frontend
Creating Network "Dockertemp_backend" with the default driver
Creating Dockertemp_mysql_1 ...
Creating Dockertemp_mysql_1 ... done
Attaching to Dockertemp_mysql_1

It is obvious that Docker-compose is creating a network, a container instance in turn.

The name looks strange, with the DOCKERTEMP_ prefix. Because the directory command that DOCKER-COMPOSE.YML is in is Dockertemp, the name that is provided in the end has this prefix and avoids the naming conflict.

And why is the Mysql_1, with the _1 suffix, the following will be used, this is to be used for scale-out to do the cluster.

Further verify that the volume, network, and container are created successfully by using the following command

Docker Volume LS

Docker netowrk ls

Docker Ps–a

Use docker-compose–f docker-compose.yml down-v to remove Network, container, and volume, fully automated (data saved in volume will also be deleted, If you want to keep volume Remove-V).

Now that only one service is defined in the Docker-compose file, the next step is to add the MVC site container and the Load Balancer container.

Add MVC site container and load Balancer container

First download the code from the following path

Https://github.com/shenba2014/AspDotNetCoreMvcDocker/tree/docker-compose

This branch is different from the previous one, adding some changes to support Docker-compose.

After the code is pulled down, execute the following command

dotnet Restore

dotnet Publish--framework netcoreapp2.0--configuration Release--output Dist

CD Dist | NPM Install

The contents of the Dist folder are the MVC site content we are going to use later.

View full Docker and docker-compose.yml files from source code

See what's new with Docker files first

From microsoft/aspnetcore:2.0.0

COPY Dist/app

COPY dist/node_modules/wait-for-it.sh/bin/wait-for-it/app/wait-for-it.sh

RUN chmod +x/app/wait-for-it.sh

Workdir/app

EXPOSE 80/tcp

ENV Waithost=mysql waitport=3306

entrypoint./wait-for-it.sh $WAITHOST: $WAITPORT--timeout=0 && exec dotnet AspDotNetCoreMvcDocker.dll

The red part is the new content, where a wait-for-it script is copied to the inside of the container, and the MVC container is set up in entrypoint waiting for MySQL to start.

Simply explained, because after using the Docker-compose boot container, we cannot ensure that the MySQL container service has started properly, and if MySQL does not start, then the site is not available. So the container must be executed after the MySQL container is started, and here is the introduction of a NPM package:wait-for-it, which can only be used under the Linux platform.

Next, open the full docker-compose.yml file, listing only the new service (the full file content is not listed, you can view the code)

Dbinit
Build
Context:.
Dockerfile:dockerfile
Networks
-Backend
Environment:
-Initdb=true
-Dbhost=mysql
-Dbpassword=password
DEPENDS_ON:
-MySQL
Mvc:
Build
Context:.
Dockerfile:dockerfile
Networks
-Backend
-Frontend
Environment:
-Dbhost=mysql
-Dbpassword=password
DEPENDS_ON:
-MySQL

LoadBalancer:
Image:dockercloud/haproxy
Ports
-3,000:80
Links
-MVC
Volumes
-/var/run/docker.sock:/var/run/docker.sock
Networks
-Frontend

Here only the original based on the addition of DBINIT,MVC and loadbalancer three service, respectively described as follows

Dbinit: Mainly used to initialize the database, this is to use it as a container to execute, actually after the completion of the initialization of the database will be exited.

MVC: This is the container for the MVC site, similar to our previous part defining MVC parameters. The main increase is the depends_on parameter, which indicates that the MVC container relies on the MySQL container.

LoadBalancer: As the name implies, is the Load Balancer server container, the site through the links parameter points to its agent is the MVC container, and through the volumes parameter configuration/var/run/ The Docker.sock file is mapped to a host file, and the specific function is not described, primarily in order to notify the Load Balancer server at the time of the expansion.

Start a site

Copy the previous step of the Dist folder Docker, docker-compose.yml files to the Docker server (if the developer is the Docker server can ignore), and then execute the following command in the directory where the files are located

Docker-compose-f DOCKER-COMPOSE.YML Build

This command has been mentioned before, similar to compiling, checking for errors and not actually creating containers

Then run

Docker-compose-f docker-compose.yml up Dbinit

Open the Dbinit container, as we mentioned before, this dbinit is also an MVC container, but only responsible for database initialization, the completion will be automatically closed, which is the output of this command

Creating Network "Dockertemp_frontend" with the default driver
Creating Network "Dockertemp_backend" with the default driver
Creating volume "Dockertemp_productdata" with default driver
Creating Dockertemp_mysql_1 ...
Creating Dockertemp_mysql_1 ... done
Creating dockertemp_dbinit_1 ...
Creating dockertemp_dbinit_1 ... done
Attaching to Dockertemp_dbinit_1
dbinit_1 | wait-for-it.sh:waiting for mysql:3306 without a timeout
dbinit_1 | wait-for-it.sh:mysql:3306 is available after seconds
dbinit_1 | WARN:MICROSOFT.ASPNETCORE.DATAPROTECTION.KEYMANAGEMENT.XMLKEYMANAGER[35]
dbinit_1 | No XML encryptor configured. Key {1B1E827D-D1D0-4D0C-A92D-8632EE0791EF} May is persisted to storage in unencrypted form.
dbinit_1 | Preparing Database ...
dbinit_1 | Applying migrations ...
dbinit_1 | Creating Seed Data ...
dbinit_1 | Database Preparation Complete
Dockertemp_dbinit_1 exited with code 0

From output you can see that there is a step waiting for MySQL to start, waiting for 16 seconds. It then performs database initialization, creates tables, and inserts some seed data. The last line output indicates that the container exits automatically.

Okay, OK. After the database is ready, you can start the MVC and load balancer containers, or through the Docker-compose command

Docker-compose-f docker-compose.yml up MVC LoadBalancer

After the execution is complete, we can access our MVC site, where we only have an MVC site, try to open http://192.168.115.136:3000/from the browser, everything is normal to see the website and the data

So many steps are in order to achieve load balancing, the next line of command let him immediately expand to 4 MVC sites

Docker-compose-f docker-compose.yml Scale mvc=4

second-level expansion is this simple, do not believe the words run Docker PS See below is not there are 4 MVC site container, still do not believe the words of manual multi-brush several times http://192.168.115.136:3000/ , you will notice that the value of the from server is changed.

Well, what if we're going to do a graceful downgrade, no problem, or a line of command

Docker-compose-f docker-compose.yml Scale Mvc=1

The system automatically removes three MVC site containers , and of course the second level.

The last command to close all services is

Docker-compose-f docker-compose.yml Stop

Write so much is to describe the use of docker-compose, compared to the previous part, is actually to put the previous work in a batch file execution, all services as a whole to manage, but also provides the way to traverse the site expansion and demotion. Seems to be very convenient, of course, not finished, follow-up and big strokes.





Run the ASPDOTNETCOREMVC program on Docker-part5: Using Docker-compose

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.