In this assignment, you will work with Docker to containerize a simple Node.js web server. You will learn to:
- Build a Docker image
- Run a Docker container
- Expose ports for accessing the web server
- Persist logs from within the container
Ensure you have Docker Desktop installed on your machine. If you haven’t already installed it, follow the instructions on the Docker website to do so.
-
Fork the Repository:
- You should have received an invitation link to this assignment. Click the link and accept the assignment.
- After accepting, GitHub will create a personal repository based on the template. Clone your repository to your local machine:
git clone https://github.com/YOUR-GITHUB-USERNAME/docker-introduction-assignment.git cd docker-introduction-assignment
-
Explore the Project Structure:
- Inside the project, you’ll see the following structure:
docker-introduction-assignment/ ├── src/ │ └── server.js ├── logs/ ├── Dockerfile ├── package.json └── README.md
- The
src/directory contains the Node.js web server. - The
logs/directory is where the application will store logs when running. - The
Dockerfiledefines the instructions to build and run the Docker container.
- Inside the project, you’ll see the following structure:
-
Understand the Web Server:
- The web server in
src/server.jslistens on port3000and responds with "Welcome to Docker" when accessed via a browser or HTTP request. - The server also logs each request to the
logs/access.logfile inside the container.
- The web server in
-
Build the Docker Image:
- Now, you will build the Docker image for this project. Run the following command to build the image:
docker build -t simple-node-app . - This command uses the
Dockerfilein the root directory to build the image and tag it assimple-node-app.
- Now, you will build the Docker image for this project. Run the following command to build the image:
-
Run the Docker Container:
- After building the image, run the Docker container, mapping port
3000on the container to port3000on your local machine. Additionally, mount thelogs/directory to persist the logs on your host machine:docker run -d -p 3000:3000 -v $(pwd)/logs:/app/logs simple-node-app - This command:
- Runs the container in detached mode (
-d). - Maps port
3000from the container to port3000on your machine (-p 3000:3000). - Mounts the
logsdirectory from your machine into the container (-v $(pwd)/logs:/app/logs).
- Runs the container in detached mode (
- After building the image, run the Docker container, mapping port
-
Test the Web Server:
- Open your browser and navigate to
http://localhost:3000, or use the followingcurlcommand to test:curl http://localhost:3000
- You should see the response:
Welcome to Docker.
- Open your browser and navigate to
-
Check the Logs:
- After making a few requests, check the
logs/access.logfile in your project directory. You should see entries for each request made to the server, similar to:Request received at 2024-10-27T10:23:59.000Z
- After making a few requests, check the
-
Stop the Docker Container:
- To stop the container, find its
container IDby running:docker ps
- Then stop it using:
docker stop <container-id>
- To stop the container, find its
-
Submit Your Work:
- Once you’ve confirmed the container runs correctly and the logs are being written, commit and push your changes:
git add . git commit -m "Completed Docker assignment" git push origin main
- Your submission will be automatically graded based on the configuration set up for this assignment.
- Once you’ve confirmed the container runs correctly and the logs are being written, commit and push your changes:
- 1 point: Dockerfile correctly exposes port 3000.
- 1 point: The Docker image builds successfully.
- 1 point: The container runs successfully and is accessible on
localhost:3000. - 2 points: The HTTP response is correctly returned as
Welcome to Docker.