Chuck's Academy

Docker

Practical Examples: Deploying Web Applications and APIs

To consolidate Docker knowledge, this chapter will explore practical examples of deploying web applications and APIs in containers. These examples will illustrate how to containerize and deploy modern services in Docker.

Example 1: Deploying a Web Application on Docker

Suppose we have a simple web application built with Nginx. Docker facilitates packaging the application into a production-ready image.

Step 1: Create the Dockerfile

Create a Dockerfile that uses Nginx as a web server:

dockerfile
"This Dockerfile uses Nginx on a lightweight alpine image, copies the HTML files to the Nginx server folder, and exposes port eighty to serve the application."

Step 2: Build and Run the Image

To build the application image, run the following command:

bash
"This command builds the my-web-app image and then runs it in a container, mapping port eighty of the container to port eighty eighty of the host."

Example 2: Deploying a Node.js API

Now let's consider an API built in Node.js. Docker allows packaging and consistently deploying the API in any environment.

Step 1: Create the Dockerfile for the API

The following Dockerfile creates an image for a Node.js API:

dockerfile
"This Dockerfile uses Node.js as a base, installs the API's dependencies, and exposes port three thousand to make the API accessible from outside the container."

Step 2: Build and Run the API

Build the image and run the API in a container:

bash
"This command builds the my-node-api image and then runs it, mapping port three thousand of the container to port three thousand of the host to access the API."

Example 3: Deploying Applications in Docker Compose

Docker Compose simplifies the configuration of multi-container applications. In this example, we will deploy a web application with a MySQL database.

Step 1: Create the docker-compose.yml File

This file defines an application composed of a web server and a database:

yaml
"This docker-compose dot y-m-l file defines two services: a web server in my-web-app and a MySQL database, where the database data is stored in a persistent volume."

Step 2: Bring Up the Application with Docker Compose

Run the application with the following command:

bash
"The docker-compose up dash d command brings up the services in the background, including the web server and the MySQL database defined in the docker-compose file."

Testing and Verification

Once the containers are running, verify the deployment by accessing the web server at port 8080 and using tools like curl or Postman to test the API at port 3000.

Conclusion

These practical examples demonstrate how to deploy web applications and APIs using Docker and Docker Compose, enabling easy and scalable management. With these concepts, it is possible to efficiently containerize and deploy applications in any environment. We hope these examples are useful for implementing your own projects and continuing to explore the possibilities.


Ask me anything