Frontend and API in different containers

View other answers to this thread
Start a personal dev blog on your domain for free and grow your readership.

3.4K+ developers have started their personal blogs on Hashnode in the last one month.

Write in Markdown · Publish articles on custom domain · Gain readership on day zero · Automatic GitHub backup and more

Anthony Lapenna's photo

As Dong Nguyen stated, you'll probably need a reverse-proxy to map your API container to the myapp.com/api location. I'd recommend using Nginx, have a look at Frank Rousseau's answer below for an example with Nginx.

In addition to that, I wanted to recommend you a few things:

Using the --link option to link container is deprecated since Docker 1.9 and will probably be removed in future versions: docs.docker.com/engine/userguide/networking..

What you should do instead is to create your own Docker network and start your containers inside that network. Containers started inside a user-defined network will be able to communicate by using their container names.

Here is a small example:

$ docker network create myapp
$ docker run -d -p 27017:27017 --name mongodb mongo
$ docker run -d -p 3000:3000 --name backend myapp/backend
$ docker run -d -p 80:80 --name frontend myapp/frontend

Give a try to docker-compose

Compose is tool for defining and running multi-container Docker application. You'll be able to define what services (mongodb, backend, frontend) that make up your app in a single YAML file and manage it using a single command-line. It's pretty handy ! Have a look here for more information: docs.docker.com/compose/overview

Riderman de Sousa Barbosa's photo

Thanks Anthony Lapenna nice tips.

I replied the Dong Nguyen comment using nginx proxy and works.

And yes, now I use network and compose in my code :), thanks for the tips.