Share
Explore

Creating a Docker Container for Your Node.js Application

The Dockerfile should be named `Dockerfile` and placed in the root of your project directory.
Here are the detailed steps:
### Step-by-Step Guide
#### 1. Create the Dockerfile
1. Open your project directory in Visual Studio Code or your preferred code editor.** 2. Create a new file named `Dockerfile` (no extension).** 3. Add the following content to the Dockerfile:**
# Use an official Node.js runtime as the base image FROM node:14
# Create and change to the app directory WORKDIR /usr/src/app
# Copy package.json and package-lock.json COPY package*.json ./
# Install app dependencies RUN npm install
# Copy the rest of the application code COPY . .
# Expose the port the app runs on EXPOSE 3000
# Define the command to run the app CMD [ "node", "dedup.js" ]

2. Build the Docker Image

1. Open a terminal (Command Prompt, PowerShell, or the integrated terminal in Visual Studio Code).**

2. **Navigate to your project directory.** 3. **Run the following command to build your Docker image:**
docker build -t yourusername/bookstore-app .
Replace `yourusername` with your Docker Hub username and `bookstore-app` with your preferred image name.
image.png
image.png

3. Run the Docker Container Locally

1. **Run the following command to start a container from your image:**

docker run -p 3000:3000 yourusername/bookstore-app ```
2. **Open your browser and navigate to `http://localhost:3000` to ensure your application is running correctly.**
#### 4. Push the Docker Image to Docker Hub
1. **Login to Docker Hub:**
```bash docker login ```
You will be prompted to enter your Docker Hub username and password.
2. **Push your Docker image to Docker Hub:**
```bash docker push yourusername/bookstore-app ```
### Optional: Using Docker Compose
If you want to simplify the process of running the application and database together, you can create a `docker-compose.yml` file:
1. **Create `docker-compose.yml` in your project directory:**
```yaml version: '3' services: app: image: yourusername/bookstore-app ports: - "3000:3000" depends_on: - db links: - db db: image: nouchka/sqlite3 volumes: - ./PeanutsBookstore.db:/data/PeanutsBookstore.db ```
2. **Run Docker Compose:**
```bash docker-compose up ```
### Create a `.dockerignore` file
1. **Create a `.dockerignore` file in your project directory:** 2. **Add the following content to the `.dockerignore` file:**
```dockerignore node_modules npm-debug.log ```
### Document the Process in a `README.md`
1. **Create a `README.md` file in your project directory:** 2. **Add the following content to the `README.md` file:**
```markdown # Bookstore Application
## Description
This is a Node.js application that uses SQLite to manage a database of books. The application can deduplicate records and export them with Markdown tagging.
## Docker Setup
### Build Docker Image
```bash docker build -t yourusername/bookstore-app . ```
### Run Docker Container
```bash docker run -p 3000:3000 yourusername/bookstore-app ```
### Push Docker Image to Docker Hub
```bash docker login docker push yourusername/bookstore-app ```
## Docker Compose (Optional)
To run the application and database together using Docker Compose:
### Create `docker-compose.yml`
```yaml version: '3' services: app: image: yourusername/bookstore-app ports: - "3000:3000" depends_on: - db links: - db db: image: nouchka/sqlite3 volumes: - ./PeanutsBookstore.db:/data/PeanutsBookstore.db ```
### Run Docker Compose
```bash docker-compose up ```
## Application Usage
Open your browser and navigate to `http://localhost:3000` to use the application. ```
By following these steps, you will be able to package your Node.js application with SQLite into a Docker container, run it locally, and upload it to Docker Hub. This makes it easy to share and showcase your work.


megaphone

Uploading your Docker Image:


Students need to follow these steps to download, install, and set up Docker, as well as to create an account on Docker Hub to upload their Docker container.

Step 1: Install Docker Desktop

1. **Download Docker Desktop:** - Go to the [Docker Desktop download page](https://www.docker.com/products/docker-desktop) and download the version suitable for your operating system (Windows, macOS, or Linux).
2. Install Docker Desktop: - Follow the installation instructions specific to your operating system. For Windows and macOS, this usually involves running the installer and following the on-screen instructions.
3. Start Docker Desktop: - After installation, start Docker Desktop. Ensure Docker is running by checking the Docker icon in the system tray (Windows) or menu bar (macOS).
### Step 2: Create a Docker Hub Account
1. **Sign Up for Docker Hub:** - Go to the [Docker Hub sign-up page](https://hub.docker.com/signup) and create a new account. Follow the instructions to complete the registration process.
2. **Verify Your Account:** - Check your email for a verification message from Docker and follow the instructions to verify your account.

Step 3: Build and Push the Docker Image

1. Open PowerShell Terminal as administrator: - Navigate to your project directory where the `Dockerfile` is located.
2. **Login to Docker Hub:** - Log in to your Docker Hub account from the terminal: ```bash docker login ``` - Enter your Docker Hub username and password when prompted.
3. **Build the Docker Image:** - Build your Docker image using the following command. Replace `yourusername` with your Docker Hub username and `bookstore-app` with your preferred image name. ```bash docker build -t yourusername/bookstore-app . ```
4. **Run the Docker Container Locally (Optional):** - Verify that the image works as expected by running it locally: ```bash docker run -p 3000:3000 yourusername/bookstore-app ```
5. **Push the Docker Image to Docker Hub:** - Push your Docker image to Docker Hub: ```bash docker push yourusername/bookstore-app ```
### Step 4: Share the Docker Hub Link
1. **Find Your Image on Docker Hub:** - Go to [Docker Hub](https://hub.docker.com/) and log in to your account. - Navigate to your profile and find the repository named `bookstore-app`.
2. **Copy the Repository Link:** - Copy the URL of your repository. It should look something like `https://hub.docker.com/r/yourusername/bookstore-app`.
3. **Send the Link:** - Send this link as proof of work.
### Example Email to Instructor:
``` Subject: Docker Container Upload - Bookstore Application
Dear [Instructor's Name],
I have successfully completed the Bookstore application project and uploaded the Docker container to Docker Hub. You can access the Docker container using the following link:
[https://hub.docker.com/r/yourusername/bookstore-app]
Thank you, [Your Name] ```
### Summary of Steps:
1. Download and install Docker Desktop. 2. Create a Docker Hub account. 3. Build the Docker image. 4. Push the image to Docker Hub. 5. Share the Docker Hub link as proof of work.
By following these steps, students will be able to create, upload, and share their Docker containers on Docker Hub, providing a clear demonstration of their work.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.