Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker: 4 Containers for Frontend, Backend, Database, Push-Server?

I am just getting started with docker and I am trying to figure out how to deploy our webapplication. We got:

  • ReactJS Webapplication & Workbench Frontend (Non-isomorph, Apache)
  • Java / Wildfly Server Backend
  • PostgreSQL Database
  • NodeJS Websocket Server for push-notifications

My first approach was to put it all inside one ubuntu-container and just deploy that. This lead into an awefully long dockerfile so I thought that cannot be the right way and I googled best practices and came to the point that each container should have only one concern.

Does that mean that I should use one container for each of the above mentioned processes and link them, which would result in 4 running containers (the database one having a volume)?

The advantages I see right now are:

  • scalability
  • maintainability
  • moving parts of the application to different servers / hosters is easier

The only two disadvantages I can think of are

  • might get complicated linking all containers together
  • (small) performance loss due to 4 containers running instead of 1 single one (and more to come for queueing, mail-server, ...)
like image 342
Felix Hagspiel Avatar asked Jul 13 '17 10:07

Felix Hagspiel


People also ask

Can Docker run multiple containers?

It's ok to have multiple processes, but to get the most benefit out of Docker, avoid one container being responsible for multiple aspects of your overall application. You can connect multiple containers using user-defined networks and shared volumes.

What is the way to easily link a back end service Docker container with data only Docker container?

Two options would be: Create a docker bridge network manually. Here are the docs for that. You'd just want to define a network with docker network create , add that network in your docker-compose, and make sure you put your frontend container in the same network when you docker run it by using the --network option.

Is Docker useful for front end?

Docker is a great tool that helps developers build, deploy, and run applications more efficiently in a standardized way. For frontend applications, we only need the Docker image for local development, because we deploy it to a static hosting provider.


1 Answers

You can use 4 different containers! With Docker you generally want to try to keep each container relatively simple but that doesn't mean you have to separate every component into different containers e.g. your web app from your server API (I have found it is useful to combine these in the same container becuase it makes communication easier) But it would be good practice to have your Database in a separate container. Docker has a tool called Docker Compose for launching container apps:

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a Compose file to configure your application’s services. Then, using a single command, you create and start all the services from your configuration. - Overview of Docker Compose

In order to use this tool you will:

  1. Create a project directory with folders for each separate container.

  2. Define your apps environment by creating a Dockerfile for each of your containers (Web App / Server API) (Database) in there sub folder.

Dockerfile for Server API( and Web App (angular):

# Create image based on the official Node 6 image from dockerhub
FROM node:6

# Create a directory where our app will be placed
RUN mkdir -p /usr/src/app/

# Change directory so that our commands run inside this new directory
WORKDIR /usr/src/app

# Get all the code needed to run the app
COPY . /usr/src/app

# Move into AngularApp directory
WORKDIR /usr/src/app/public
# Install dependencies for AngularApp
RUN npm install
# Changed to updated angular dependency
RUN ./node_modules/@angular/cli/bin/ng build
# Move into server directory
WORKDIR /usr/src/app/server
# Install dependencies for server
RUN npm install
# Return to top level directory of project
WORKDIR /usr/src/app

# Add API environment variables
ENV USERNAME="username"
ENV PASSWORD="password"

# Expose the port the app runs in
EXPOSE 4200
EXPOSE 80

WORKDIR /usr/src/app
  1. Define in the project folder a docker-compose.yml file. This file will define the services in your app so that they can be run together in an isolated environment. Compose file version 3 reference

A docker compose file follows this structure:

version: '3'
services:
  web:
    build: <directory with Dockerfile for service>
    ports:
      - "5000:5000"
    volumes:
      - .:/code
      - logvolume01:/var/log
    links:
      - redis
  database:
    image: mongo
  volumes:
    logvolume01: {}
  1. Lastly run docker-compose up and Compose will start to run you entire app!
like image 135
ob1 Avatar answered Oct 24 '22 16:10

ob1