This template provides a basic setup for building a Node.js web application using the Express.js framework, and containerized with Docker. It includes a Dockerfile for building a Docker image and a Docker Compose configuration for easy deployment.
- Node.js & Express.js: A minimal setup for creating a web server using Node.js with the Express.js framework.
- Dockerized: Utilizes Docker for containerization, enabling consistent and reproducible environments across different systems.
- Docker Compose: Includes a Docker Compose configuration for orchestrating multiple containers, simplifying the deployment process.
- Ensure Docker is installed.
- Confirm Docker Compose is installed.
- app/server.js: The central application file orchestrating the Express setup.
- app/db.js: The configuration hub for the database along with connection pool setup.
- Dockerfile: A tailored Dockerfile facilitating containerization.
- docker-compose.yml: Docker Compose configuration streamlining multi-container orchestration.
- demo.sql: Preconfigured SQL commands executed during boot-up for table and user creation.
Version: The Docker Compose file format version, set to "3."
Services:
-
Node Service (
node
):- Image: Docker image sourced from
rattymyles/docker-express-api
repository with thelatest
tag. - Container Name: Defined as
express-api
for the Node.js container. - Ports: Mapping host port 8080 to container port 8080.
- Environment: Configured environment variables, communicated to the Node.js application in
db.js
, defining MariaDB connection details. - Networks: Connection to a custom bridge network named
docker-service
. - Depends On: Ensures that the
node
service starts only after themariadb
service is up and running.
- Image: Docker image sourced from
-
MariaDB Service (
mariadb
):- Image: Utilizes the official MariaDB image from the repository with the
latest
tag. - Container Name: Set as
docker-mariadb
for the MariaDB container. - Ports: Maps host port 3306 to container port 3306.
- Environment: Configuration of the root password for MariaDB.
- Networks: Connection to the custom bridge network named
docker-service
. - Volumes: Mounts two volumes:
maria-data:/data/db
: Persists MariaDB data on the host machine../demo.sql:/docker-entrypoint-initdb.d/demo.sql
: Copies thedemo.sql
file to the MariaDB container, executing SQL commands during initialization.
- Image: Utilizes the official MariaDB image from the repository with the
Networks:
- Defines a custom bridge network named
docker-service
facilitating seamless communication between thenode
andmariadb
services.
Volumes:
maria-data:
Creates a named volume ensuring persistence for MariaDB data on the host machine.
This Docker Compose configuration seamlessly sets up a Node.js application, a MariaDB database, and all necessary connections and configurations for a cohesive and efficient runtime environment.
-
Clone this repository:
git clone https://github.com/RattyMyles/Node.js-and-MariaDB-Project-with-Docker.git cd Node.js-and-MariaDB-Project-with-Docker
-
Navigate to the
app
folder containing the Dockerfile. Build a container to package the Node.js files:docker build -t rattymyles/docker-express-api .
-
Once built, return to the parent directory and run the
docker-compose.yml
file:docker compose up
or run it in detached mode:
docker compose up -d
Detached Mode: Containers run in the background, and the terminal becomes available for further commands without being attached to the container logs.
Fetches all tasks from the database.
curl localhost:8080/tasks
Creates a new task. Requires a JSON payload with a description
property.
curl -X POST -d "description=HelloWorld" localhost:8080/tasks
Once you are finished, you can stop the containers:
docker compose down
If you wish to delete the volume data to start fresh:
docker compose down -v
Please do get in touch for any feedback or anything. I'll respond :)