hesyar
hesyar

Reputation: 465

Best practice using Dockerfile with docker-compose and vcs

I have a question regarding best practice.

Lets say I have a webfrontend developed in angularjs and a API to get data from. I put those two in separate repositories. Now I want to dockerize the whole thing. My idea was to put a Dockerfile in each project which specifies their environment. Until now its fine but what when I also have a docker-compose file which starts those two services simultanously? First of in which repo should I put this file and secondly how can I assure, that the images are always up-to-date?

Upvotes: 0

Views: 843

Answers (1)

lvthillo
lvthillo

Reputation: 30733

You can use a structure like this:

project/
 - project/docker-compose.yaml
 - project/frontend/ (contains Dockerfile + necessary files)
 - project/api/ (contains Dockerfile + necessary files)

In your docker-compose.yaml you can write something like this for each image:

 frontend:  
  build: ./frontend #folder in which Dockerfile of frontend is saved (to build the image)
  image: my-frontend:1.0 #give a name to your image
container_name: frontend-container #containername when a container instance of your image is running

To start docker-compose you can run docker-compose up --build. The --build tag will recreate your images when there are changes so you can keep your compose up to date when there are changes in the dockerfile (image).

Docker-compose is 'reading' from up to bottom. So the service you describe first in your docker-compose.yaml will be created first. I would think it's your API because that can probably exist on its own and your frontend needs to connect with it. Sometimes your first service is started too slow, which means the second service is already up but can not find the first service (to connect with) and it crashes. This can be solved by using a wait-for-it.sh script. Your second service will use the script and check when the first service is up. When it's up, it will start its own service.

Upvotes: 2

Related Questions