Jeanluca Scaljeri
Jeanluca Scaljeri

Reputation: 29129

How to expose files from a docker container through a webserver

I have this website which uses angular for the frontend and has a NodeJs backend. The backend serves the angular files and handles client calls.

As it is now, they are both packages and deployed as one docker image. Meaning, if I change the frontend, I also need to build the backend in order to create a new image. So it makes sense to seperate them.

But if I create an image for the backend and frontend, how can the backend serve files from the frontend container?

Is this the right approach?

I think I would like to have the frontend inside a docker image, so I can do stuff like rollback easily (which is not possible with docker volumes for example)!

Upvotes: 0

Views: 514

Answers (2)

mixth
mixth

Reputation: 636

Yes! Containerize them to have their own containers is the way to go! This make us deploy/deliver faster and also separate build pipelines to make steps clearer to everyone involved.

I won't bother having backend serving frontend files. I usually create my frontend image with a webserver (eg nginx:alpine), since frontend and backend can be separately deployed to different machines or systems. And don't forget to use multi-stage builds to minimize image size.

But if you must do that, I guess you can use docker-compose to have them in one network, and then, forward requests of those static files from backend to the frontend webserver. (Just some hacks, there must be a better way to handle this from more advanced people here :P)

Upvotes: 1

camba1
camba1

Reputation: 1820

I have something similar, an Emberjs running in one docker container that connects to nodejs that is running in its own container (not to mention the DB that runs on a third container). It all works rather well.

I would recommend that you create your containers using docker-compose which will automatically create the network so that both containers can talk to each other using :.

Also I set it up so that the code is mapped from a folder in my machine to a folder in the container. This allows me to easily change stuff, work with Git , etc...

Here is a snippet of my docker-compose file as an example:

version: "3"

services:
....
  ember_gui:
    image: danlynn/ember-cli
    container_name: ember_dev
    depends_on:
      - node_server
    volumes:
      - ./Ember:/myapp
    command: ember server
    ports:
      - "4200:4200"
      - "7020:7020"
      - "7357:7357"

Here I create an ember_gui service which creates a container named ember_dev based on an existing image from docker hub. Then it tells docker that this container is dependent on another container that needs to be compiled first and which I do not show in the snippet but that is defined in the same docker-compose file (node_server). After that, I map the ./Ember directory to the /myapp folder in the container so that I can share the code. Finally I start the ember server and open some ports

Upvotes: 1

Related Questions