Tutorial: Creating a WebSocket Chat Application, Dockerizing, and Deploying on EC2 with NGINX and SSL

Nick Jabs
2 min readDec 11, 2023

--

Photo by Cookie the Pom on Unsplash

Step 2 : Dockerfile to containerize your WebSocket chat application

# Use the Node.js image as the base image
FROM node:latest

# Set the working directory inside the container
WORKDIR /app

# Copy package.json and package-lock.json to the working directory
COPY package.json package-lock.json ./

# Install the project dependencies
RUN npm install

# Copy the entire application files to the working directory
COPY . .

# Expose port 8080 for the WebSocket server
EXPOSE 8080

# Command to run the WebSocket server when the container starts
CMD ["node", "websocket-server.js"]

Building the Docker Image:

Run the following command in your terminal or command prompt:

docker build -t my-websocket-app .

This command builds the Docker image based on the instructions provided in the Dockerfile and tags it with the name my-websocket-app.

Testing the Docker Image Locally:

After the build is successful, you can run the container using the following command:

docker run -d -p 8080:8080 --name my-websocket-app my-websocket-app

This command starts a container in detached mode (-d) and maps port 8080 of the host machine to port 8080 of the container. The --name flag assigns a name to the container.

Testing the WebSocket Chat Application Locally:

Open your web browser and navigate to http://localhost:8080/. This should display the WebSocket chat application. Open multiple tabs or windows and chat between them to verify the functionality.

[ back to Part-1 ]This completes Part 2 of the tutorial. In the next part, we’ll explore deploying the Dockerized WebSocket application to an EC2 instance and configuring NGINX for reverse proxy and SSL. [Step-3]

--

--