Developing and Orchestrating Microservices with Docker and Kubernetes - Day 3

Welcome to Day 3 of learning microservices! Today, we'll focus on containerizing microservices using Docker.

Day 1: https://shorturl.at/qsCG0

Day 2: https://www.garudax.id/pulse/developing-orchestrating-microservices-docker-day-2-hossain


1. Dockerizing Microservices:

Containerization is a key aspect of microservices architecture, and Docker provides an excellent solution for creating and managing containers. By containerizing microservices, we can ensure consistent and reliable deployment across different environments.


2. Dockerfile:

To containerize a microservice, we use a Dockerfile. It is a text file that contains a set of instructions for building a Docker image. These instructions define the base image, dependencies, and configurations required to run the microservice.


3. Dockerfile Instructions:

Common Dockerfile instructions include:

- `FROM`: Specifies the base image to build upon.

- `COPY` or `ADD`: Copies files from the local machine to the image.

- `RUN`: Executes commands within the image during the build process.

- `EXPOSE`: Specifies the ports on which the microservice listens.

- `CMD` or `ENTRYPOINT`: Defines the command to run when the container starts.


4. Building Docker Images:

To build a Docker image from a Dockerfile, use the `docker build` command followed by the location of the Dockerfile and an optional tag. For example:

docker build -t my-microservice:1.0 .        

This command builds the Docker image using the Dockerfile located in the current directory (`.`) and tags it as `my-microservice:1.0`.


5. Running Docker Containers:

Once the Docker image is built, we can create and run Docker containers based on that image using the `docker run` command. For example:

docker run -p 8080:80 my-microservice:1.0        

This command creates a container from the `my-microservice:1.0` image and maps port 8080 of the host machine to port 80 of the container.


6. Testing the Microservice:

With the container running, you can test the microservice by accessing it through the mapped port on your local machine (e.g., `http://localhost:8080`).


7. Managing Containers:

You can use various Docker commands to manage containers, such as `docker ps` to list running containers, `docker stop` to stop a running container, and `docker rm` to remove a stopped container.

8. A simple node js microservice in docker that prints hello world

  • Create a new directory for your project and navigate into it:

mkdir my-microservic

cd my-microservicee        

  • Create a new file named `app.js` and open it in a text editor:

touch app.js        

  • Add the following code to the `app.js` file:

const http = require('http')

const server = http.createServer((req, res) => {

 res.writeHead(200, { 'Content-Type': 'text/plain' });

 res.end('Hello, World!\n');

});

const port = 8080;

server.listen(port, () => {

 console.log(`Server running on port ${port}`);

});;        

  • Create a new file named `Dockerfile` and open it in a text editor:

touch Dockerfile        

  • Add the following code to the `Dockerfile`:

FROM node:14-alpin
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD [ "node", "app.js" ]        

  • Save the `app.js` and `Dockerfile` files.
  • Build the Docker image using the following command:

docker build -t my-microservice .        

This command builds the Docker image and tags it as `my-microservice`.

  • Once the image is built, run a container from the image:

docker run -p 8080:8080 my-microservice        

This command maps port 8080 of the host machine to port 8080 of the container.

  • Access the microservice by opening a web browser and navigating to `http://localhost:8080`. You should see the message "Hello, World!" displayed.

That's it! You now have a simple Node.js microservice Dockerized and running in a container.


Today's focus on Dockerizing microservices sets the foundation for deploying and scaling your microservices architecture. Tomorrow, we'll explore Docker Compose, which helps manage multi-container applications.

Feel free to experiment with Dockerizing your microservices, and let me know if you have any questions or need further guidance.

To view or add a comment, sign in

More articles by Md Razon Hossain

Others also viewed

Explore content categories