Use Docker-compose to build a scalable Web application

Source: Internet
Author: User
Tags docker run

Use Docker-compose to build a scalable Web application

    • Use Docker-compose to build a scalable Web application
      • Build Web Application
        • To build a simple base image
        • Write app
        • New Startsh File
        • New Build File Dockerfile
        • Test Container
      • Use Docker-compose to orchestrate projects
      • Start Project
      • Additional Information
      • Summarize

This is a scale-out Web application "architecture" built using Docker-compose. said that the structure is a bit large, but the overall idea can be used as a frame selection of reference value.
The technology stacks I use in this practice are:
1. Sinatra: A very simple ruby web framework
2. Docker
3. Docker-compose

Go directly to the body

Build a Web application to build a simple base image

Why build a simple base image? This makes it easy for us to unify the environment when we build the project. This practice builds the base image very simple. The code is as follows

FROM ruby:2.3.0gem install sinatra

Build command: docker build -t xudonghe/sinatra:v1 .
After the build is complete, we can run the docker images view results

Write app

The first step is to create a simple app.rb file
The second part, enter the following code

require‘sinatra‘set‘0.0.0.0‘get‘/‘do  ‘Hello world!‘end

One notable point here is that you need to set the server to bind to the 0.0.0.0 address, otherwise the container will not be accessible outside.

New start.sh File

This file is primarily used to start the Web application

#!/bin/bashruby -rubygems app.rb
New Build File Dockerfile
FROM4567COPY . /appWORKDIR /appRUN chmod a+x ./start.shCMD ["bash""start.sh"]

Why inherit xudonghe/sinatra:v2 ? Because I've built a new version locally, it's really simple. is to open the 4567 port and copy the file to the container. In the new build file, I give the start.sh executable permission and run the file when the container starts.

Test Container

Run the following test command

-t.---ti-P-v-rubygems app.rb

And then access the local

curl http://$(docker-machine ip):4567
Use Docker-compose to orchestrate projects

Throughout the system, there are two components: Web and Nginx-proxy. I chose the jwilder/nginx-proxy. The use is relatively simple. First download the image to Local: docker pull jwilder/nginx-proxy . And then run docker run -d -p 80:80 -v /var/run/docker.sock:/tmp/docker.sock:ro jwilder/nginx-proxy . Of course, if you need to use docker-compose, we don't need to start it so manually. Here is just a simple introduction. If you need to keep going, you need to close this container.

Here's my docker-compose.yml file.

web:  build: .  volumes:    - .:/app  expose:    - 4567  environment:    - VIRTUAL_HOST=yourwebsiteurlnginx:  image: jwilder/nginx-proxy:latest  links:    - web  volumes:    - /var/run/docker.sock:/tmp/docker.sock:ro  ports:    - 80:80    - 443:443

There's a key place here
1. Environment variables should be defined wellenvironment

  environment:    - VIRTUAL_HOST=yourwebsiteurl
    1. Nginx needs to use volumes
      /var/run/docker.sock:/tmp/docker.sock:ro
Start Project
    1. Rundocker-compose up -d
    2. Scale Projectdocker-compose scale web=5
    3. viewing system Informationdocker-compose ps
    4. Browser accesshttp://yourwebsite
Additional Information

If you want to follow the above code practice, generally will not succeed. Because of the lack of the necessary conditions: The local nginx do reverse proxy and modify the local/etc/hosts file, the Web address to the Docker. This is not explained in detail here. The specific architecture diagram is as follows:

Which nginx is on the host machine, in nginx-proxy Docker.

Summarize

The advantage of this scheme is that it is very easy to expand the project horizontally, the disadvantage is that the advantages are complex.

Use Docker-compose to build a scalable Web application

Related Article

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.