Build and run docker containers using docker-compose3 min read

Docker-compose is an official tool by docker to manage and deploy multi-container applications. Docker-compose uses a YAML file where application services are defined.

If you have any confusion between docker-compose and docker stack check out this link.
Using the docker-compose file you can build docker images and run the docker containers in a single step.
Here we will show you how to do this using a sample nodejs application

Check this link to docker-compose installation guideĀ How to install docker-compose in Linux

Step 1: Create a folder for your application

mkdir nodejs-app

Step 2: create your sample application
vi app.js

var http = require("http");

http.createServer(function (request, response) {
   // Send the HTTP header 
   // HTTP Status: 200 : OK
   // Content Type: text/plain
   response.writeHead(200, {'Content-Type': 'text/plain'});
   // Send the response body as "Hello World"
   response.end('Hello World\n');

// Console will print the message
console.log('Server running at');

Step 3: Create dockerfile

vi Dockerfile


FROM node:8.11.3-alpine
WORKDIR /usr/src/app
COPY . .
ENTRYPOINT [ "node", "app.js" ]

Step 4: create docker-compose file

version: "3.3"
    build: .
      - "8081:8081"

Above is a simple docker-compose file that has a service called the web, docker-compose will start creating the docker image once it reaches the build section of docker-compose.yml. ‘.’ means the dockerfile present in the current path. Make sure that you maintain the DOckerfile name in the same format.

By default docker-compose file picks up Dockerfile has the dockerfile name.


Step 5: Now let’s deploy

sudo docker-compose up

Building web
Step 1/5 : FROM node:8.11.3-alpine
---> ca148a52ea10
Step 2/5 : WORKDIR /usr/src/app
---> Using cache
---> a80676df53f3
Step 3/5 : COPY . .
---> 0df3d8382467
Step 4/5 : EXPOSE 8081
---> Running in a09d6cb1eeb4
Removing intermediate container a09d6cb1eeb4
---> 2bee29a216a3
Step 5/5 : ENTRYPOINT [ "node", "app.js" ]
---> Running in 43f7be6f2449
Removing intermediate container 43f7be6f2449
---> 871d8b9b42b2

Successfully built 871d8b9b42b2
Successfully tagged node_web:latest
WARNING: Image for service web was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating node_web_1 ... done
Attaching to node_web_1
web_1 | Server running at

Now you can see docker-compose started building the docker image and after that, it started the docker container with the same images. Note that all build and deploy made using a single file.

Now check the browser to verify the apps is running

Step 6: To stop the service

sudo docker-compose -d


Prabhin Prabharkaran Administrator
DevOps Engineer

He is a Technical professional. He is a person who loves to share tricks and tips on the Internet. He Posts what he does!

follow me
We need your support!!
Other Amount:
Questions Answered
Articles Written
Overall Points

Prabhin Prabharkaran

He is Technical professional. He is a person who loves to share tricks and tips on the Internet. He Posts what he does!!

You may also like...

Leave a Reply