Intro to Docker and Docker compose

we will use docker containers to manage the different services. Here is an example of a http server container and an api server container, that we will use as an example for two containers built to communicate with each other, open a port to the host and automate the start up including the build and shutdown processes.

what is docker anyway? Here is a list of basic commands to get started

For setting up our docker server container, using a http_server.Docker file:

now to automate the process of building and starting the server container with a shell script

don’t forget to make it executable

Now we will make the same for the node.js api server that the http container will communicate with, this is the api_server.Dockerfile:

then the deployment shell script

We will create a simple express.js function on the api server as a backend for the http server. For that we will need to install express, and initiate npm with our service:

then let’s create our service, e.g. api_server.js:

Now we can let docker compose take care of handling the set up of these containers and especially the communication between them. For that we will create our composer config file docker-compose.yml

once we run docker-compose, we will have the http server available on localhost:8080, the api server will not be reachable from outside the container, except for http server. Now let’s create the start.sh scripts:

and respectively the stop.sh script, don’t forget to make them executable!

Leave a comment

Your email address will not be published. Required fields are marked *