Code Monkey home page Code Monkey logo

dockerize-vps's Introduction

How to deploy multiple independent websites with docker

Introduction

It is common for individual developers and small businesses to run multiple independent websites on a single VPS. However, if they need to listen on the same port, such as 80, we had to merge their configuration files together, which is not clean.

The main idea comes from How to set your VPS with docker containers for multiple websites and corresponding repo rokerkony/dockerize-vps. However, his method doesn't support communication between containers. To solve this problem, I make some modifications based on his repo.

jwilder/nginx-proxy is a automated nginx reverse proxy for docker.

nginx-proxy sets up a container running nginx and docker-gen. docker-gen generates reverse proxy configs for nginx and reloads nginx when containers are started and stopped.

We can use it as a unified frontend to listen on 80 and 443 port, and then dispatch request to different websites according to the url.

Usage

Clone this repo

git clone [email protected]:Yriuns/dockerize-vps.git

Start the nginx-proxy

cd dockerize-vps/nginx_proxy
docker-compose up -d

Integrate into your project

You need to follow the structure and files in /template directory.

  1. If you have already run nginx in a docker container, just wrap it in a /nginx folder.
  2. Otherwise, create a /nginx forlder, write Dockerfile and default.conf like /template do.
  3. Copy or edit your project docker-compose.yml file.
version: "2.0"
services:
  nginx:
    build: ./nginx
    ports:
      # any port that has not been used
      - "8081:80"
    links:
      - backend
    environment:
      # your domain
      - VIRTUAL_HOST=example.com
    networks:
      - default
      # connect to nginx-proxy network
      - nginx_proxy_default
  backend:
    build: ./backend
    volumes:
      - ./backend:/opt/workspace

# connect to nginx-proxy network
networks:
  nginx_proxy_default:
    external: true
  1. Run
docker-compose up -d

Example

Start nginx_proxy

cd nginx_proxy
docker-compose up -d

Start site1

cd example/site1
docker-compose up -d

Start site2

cd example/site2
docker-compose up -d

Test sie1

curl -H "Host: site1.com" localhost
<html>
<head>
    <meta charset="utf-8">
    <title>Site 1</title>
</head>

<body>
    <h1>Hello, World!</h1>
</body>
</html>

Test site2

curl -H "Host: site2.com" localhost
<html>
<head>
    <meta charset="utf-8">
    <title>Site 2</title>
</head>

<body>
    <h1>Hello, World!</h1>
</body>
</html>

dockerize-vps's People

Contributors

whaodar avatar yriuns avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.