Use docker-compose to build the AspNetCore development environment,
Use docker-compose to build the AspNetCore Development Environment
1. Use docker-compose to build a Development Environment
Our goal is simple: Use docker-compose to combine several docker containers.
First, use Nginx to proxy all Web programs. In this way, you only need to listen to a port on the host, so as not to pollute the host. Then combine various Web programs, Redis/Memcached, and SqlServerOnLinux.
Create a directorySites, All cluster-related items are put here, And the directory structure is as follows:
Sites
Nginx
Sites-enabled
Default
Dockerfile
Redis
Dockerfile
Redis. conf
Docker-compose.yml
New docker-compose.yml
Version: "2" services: redis: build:./redis/restart: always container_name: mac-redis identity: build :~ /Identity/src/Web/# restart: always volumes :-~ /Identity/src/Web/bin/Debug/netcoreapp1.1/publish:/app # Add the compiled sequence set to the data volume links:-'ucenter: api.ucenter.com '# because the identity project depends on the ucenter project, the identity Project internally calls the ucenter webapi through url: api.ucenter.com. Therefore, we need to set an alias like ur here, in this way, access to 'api .ucenter.com 'from the identity project will be forwarded to the ucenter container. -Redis extra_hosts:-"dbserver: 192.168.199.143" # a SQL Server container was previously built, which is placed on a Linux machine separately, so it is called directly through the address here, enable remote access. Container_name: identity logging: driver: "json-file" options: max-size: "50 k" # As the program runs, logs increase, resulting in longer loading times, therefore, the log file size is specified to save time. max-file: "10" ucenter: build :~ /UCenter/src/WebAPI # restart: always volumes :-~ /UCenter/src/WebAPI/bin/Debug/netcoreapp1.1/publish:/app links:-redis extra_hosts:-"dbserver: 192.168.199.143" container_name: ucenter nginx-host: build :. /nginx # nginx Dockerfile directory restart: always ports:-"80: 80" # Listening to port 80 of the host, or other ports can be links: -identity-ucenter volumes :-. /nginx/sites-enabled:/etc/nginx/sites-enabled # Put the nginx configuration file in the data volume. You can restart nginx when you need to change it later, you do not need to re-build-/WebCommon:/www/data # static files shared by each project. Generally, the static files are stored in cdn. In the development environment, nginx proxy container_name: nginx-host logging: driver: "json-file" options: max-size: "50 k" max-file: "10"
Four services are configured in the cluster above. One nginx listens to port 80 of the host and forwards it to the corresponding service.
Identity is a project developed by me and depends on the ucenter project.
The project depends on the redis cache, and the database service dbserver resolved through the host.
2. Set up the nginx service first.
InSites-> nginxDirectory, createSites-enabledDirectory, and create a new configuration file named default.
Server {listen 80; server_name account.xxx.com; # location/{proxy_pass http: // identity; # Forward to the identity service to process proxy_set_header Host $ host; proxy_set_header X-Forwarded-For $ timeout; proxy_connect_timeout 150; proxy_send_timeout 100; Limit 100; proxy_buffers 4 32 k; limit 8 m; Limit 128 ;}} server {listen 80; server_name limit; # ucenter project domain name location/{proxy_pass http: // ucenter; # Forward to ucenter Service Processing proxy_set_header Host $ host; proxy_set_header X-Forwarded-For $ timeout; proxy_connect_timeout 150; proxy_send_timeout 100; proxy_read_timeout 100; proxy_buffers 4 32 k; client_max_body_size 8 m; Limit 128 ;}} server {listen 80; server_name cdn.xxx.com; # package static files into a service, replace cdn root/www/data; location /{}}
This is the nginx configuration file. It mainly configures the Nginx proxy mode. The following code generates the Nginx Dockerfile, and creates the Dockerfile under sites-> nginx.
FROM tutum/nginxVOLUME /etc/nginx/sites-enabled
You can use this Dockerfile to create Nginx,
3. Install redis
1) create a directory in sites: mkdir redis
2). vim Dockerfile
FROM redisCOPY redis.conf /usr/local/etc/redis/redis.confCMD [ "redis-server", "/usr/local/etc/redis/redis.conf" ]
3). vim redis. conf, copy and paste the redis configuration, which is the official default configuration of redis.
42.16.doc ker build-t mac-redis. I gave it the name mac-redis
5) after the image is created, first test whether it can be used. docker run-d -- name mac-redis. At this time, the mac-redis container has been running in the background. We use redis-cli to test the redis service, docker run-it -- link mac-redis: redis -- rm redis-cli-h redis-p 6379 use the -- rm parameter to clear the container when it is used up. The test is as follows:
redis:6379> set key1 value1OKredis:6379> get key1"value1"
It seems that the redis service is okay. exit and quit. Delete the redis service container used for testing docker rm-f mac-redis.
4. Run the aspnetcore program using docker
This is the easiest way to use Microsoft images. There are also a lot of tutorials in the garden.
Here I put them in the cluster, as long as you configure the path of the Dockerfile for each aspnetcore program in the docker-compose.yml, there are
Build :~ /Identity/src/Web/# directory of the Dockerfile of the identity Project
The directory where the source code of identity is located is ~ /Identity/, Dockerfile is in src/web, as follows:
FROM microsoft/aspnetcore:1.1.1LABEL Name=identity Version=0.0.1ENV ASPNETCORE_ENVIRONMENT DevelopmentENTRYPOINT ["dotnet", "Identity.Web.dll"]
If multiple dockerfiles exist, you can specify the Dockerfile name in yml.
Another ucenter Dockerfile is as follows:
FROM microsoft/aspnetcore:1.1.1LABEL Name=ucenter Version=0.0.1ENTRYPOINT ["dotnet", "UCenter.WebAPI.dll"]
After the code is modified, dotnet publis & docker restart xxx (xxx is the container name) takes effect.
Access through chrome
Finally, enter the sites directory and run docker-compose up to start the cluster. Because nginx listens to port 80 of the host, you need to point the bound url to port 80 of the host, one method is to directly add resolution in hosts, but this is not recommended because you need to change hosts if you want to access an online project. In order not to pollute the host environment, chrome browser is recommended. chrome has multiple users. Therefore, to create a new user, use the SwitchyOmega plug-in to proxy the required URl to port 80 of the host, in this way, the chrome user can be used in future development without affecting the host environment.
Debugging is essential in the development process, so the next article describes how to debug in Docker with VSCode.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.