Giter Site home page Giter Site logo

taboca / container-server-virtual-host-nodeapp Goto Github PK

View Code? Open in Web Editor NEW
0.0 3.0 0.0 25 KB

Technote / Docker / Using Docker compose with an Apache server and a virtual hosted node app in other container

Dockerfile 62.56% Shell 6.43% JavaScript 28.01% HTML 3.01%
tutorial example docker-compose

container-server-virtual-host-nodeapp's Introduction

Study notes - Docker container with a server and 2 NodeJS apps

Welcome to a tutorial and the opportunity to learn a bit about Docker. This is a simple case example intended to show you a Docker-based "app", a system with 3 containers, that shall be installed in a host system, a computer.

Such system is comprised of 3 services:

  • A Web Server, using Apache2
  • A NodeJS-based Web Server Simple app 1
  • Another NodeJS-based Web Server Simple app 2

The idea here is to put the Apache2 serving content from the other apps via port 80. The apache will connect the user to app 1 or app 2 based on virtual host configuration. So, if the user tries to access claiming a "www.myexamplesite.com", the proxy configuration in the Apache should connect with the service 1 / app 1. If instead the user tries to access the server claiming "www.myothersite.com", the proxy will use service 2 / app 2.

Let's first create the images that Docker compose will use to eventually launch the containers

  • Access "./image-webserver" and run "./makeImage.sh"
  • Access "./image-app-1" and run "./makeImage.sh"
  • Access "./image-app-2" and run "./makeImage.sh"

Now you can check the images that you have created:

sudo docker image ls

Let's check how to launch the services (and associated containers)

First, let's look at our docker-compose.yml:

version: "3"
services:
  service.web:
    image: image-webserver
    ports:
      - "80:80"
    restart: always

  service.app.1:
    image: image-app-1
    ports:
      - "8080:8080"
    restart: always

  service.app.2:
    image: image-app-2
    ports:
      - "8081:8081"
    restart: always

Notice that the above compose specification uses names, such as "service.web", "service.app.1", and "service.app.2" in association with the images. According to Networking in Compose Docker compose will create a common network so the containers (associated with these services) can reach each other.

Therefore, when using Docker compose, a network gets created and the containers can be reachable using the service names (e.g., service.web, service.app.1, service.app.2). Notice that these names were my own and I have decided to name them using the notation "something.dot.something" just so they could look like a domain.

Launching

When you launch the system, with "sudo docker-compose-up", keep in mind that Docker compose will launch one container for each service.

sudo docker-compose up

Notice: **In this example, we are launching "docker-compose" without the "-d" parameter so that we can see log outputs, eventually served from the NodeJS-based scripts. **

As you launch these 3 services, you shall verify that Docker Compose creates a networks and informs you about the name that is the name of the directory where this "docker-compose.yml" is installed.

In my case, this local root directory was named "compose" so Docker Compose created a network called "compose".

Testing

Before we attempt to use the browser, to load the apps, we need to tell your local computer and map the names (eg., www.myexamplesite.com, www.myothersite.com) as your localhost.

sudo vi /etc/hosts

And add the following lines:

127.0.0.1 www.myexamplesite.com
127.0.0.1 www.myothersite.com

Now you are good to go, you can open your best browser and try each domain. Notice the log outputs served in the shell where you launched via docker-compose.

Notice: I am using a Mac so I had a problem with the /etc/hosts file being ignored. I was able to fix due to this solution editing the /etc/resolv.conf.

Of course this is a controlled scenario that you are informing your local computer that the above domains are the 127.0.0.1. In the real world and production scenario you shall be using DNS servers with a real domain propagated. If you liked this tutorial please feedback me. Here is a video with my narrative

container-server-virtual-host-nodeapp's People

Contributors

taboca avatar

Watchers

 avatar  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.