Running related containers together – Docker Compose
In our last 2 blogs we executed both, a Dockerized B/E App and FE Application in isolated mode. In real scenario multiple applications work together and have dependency on each other, especially in n-tier & microservices architectures.
In this blog we’ll create a docker compose file to run both the dockers together. UI app will be powered with the APIs exposed in B/E docker.
- Pre-requisites from previous 2 blogs
- UI and B/E dockerized applications from previous 2 blogs
- docker-compose installed, this is usually pre-installed if docker desktop is installed
First, we’ll update the angular app to get & update count values in B/E. We need to angular service that interacts with backend API. The change looks like –
CORS has to be enabled in B/E application so that it can accept requests from angular application. The change looks like
Finally, we need to create a docker-compose yaml file that configures and runs both the docker containers in expected order. Contents of docker compose yaml are –
The above docker compose defines 2 services
- counterbe : B/E service using demo/samplespringboot as image and maps host 8099 port to container 8099 port
- counterfe: F/E service using demo/counterappui as image and maps hots 4201 port to container 4201 port. Also, depends_on specifies that counterfe service depends on the counterbe container.
Bringing docker compose up: Use the below command to start the instances from docker compose file
‘docker-compose -f docker-compose-counter.yaml up -d‘
The above command runs the container in detached mode. Use the ‘docker ps’ command to check if the dockers are up and running.
To verify if app is up and running, access “http://localhost:4201/counter/” URL in browser and check if below UI comes up
Stopping the containers: Run the below command to stop running containers
‘docker-compose -f docker-compose-counter.yaml down’
This article just scratches the surface of docker-compose, it is very useful in enterprise applications or product solutions. The complete documentation of docker compose can be found at https://docs.docker.com/compose/
The next articles/blogs would address the container orchestration and related concepts – Kubernetes basics, hands-on with Kubernetes configuration on minikube.