Build and run docker containers using docker-compose3 min read

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

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');
}).listen(8081);

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

Step 3: Create dockerfile

vi Dockerfile

 

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

Step 4: create docker-compose file

version: "3.3"
services:
  web:
    build: .
    ports:
      - "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 http://127.0.0.1:8081/

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

 

© 2020, Techrunnr. All rights reserved.

#1
#2
#3
Questions Answered
Articles Written
Overall Points

Related posts

Leave a Reply