\chapter{Digital Ocean Docker Deployment} \ChapterLabel{DigitalOceanDockerDeployment} \index{digital ocean docker deployment} \section{Overview} In this chapter we deploy our Color Buttons web application, a small Node.js app with two buttons that change the page's background color, inside a Docker container on a DigitalOcean droplet. We then refactor the button logic into class-based JavaScript and redeploy it. The deployed website is available at: \url{http://147.182.178.136} \section{Creating the Droplet} \index{droplet} We created a droplet named \texttt{The-Ocean} from the DigitalOcean dashboard (Create $\rightarrow$ Droplets) running Ubuntu 24.04 LTS. After creation, the dashboard showed the droplet's public IPv4 address, \texttt{147.182.178.136}, which we used for SSH access and for the website. \section{Setting Up SSH Access} \index{SSH} We generated an SSH key pair on Windows using PowerShell. At the prompt for a file name, we named the key \texttt{digiOcean}, which produced a private key (\texttt{digiOcean}) and a public key (\texttt{digiOcean.pub}). \begin{minted}{powershell} ssh-keygen \end{minted} The public key was added to our DigitalOcean team. The private key never leaves the local machine. \subsection{Troubleshooting: Permission Denied} Our first connection attempt failed: \begin{minted}{text} root@147.182.178.136: Permission denied (publickey). \end{minted} We ran SSH in verbose mode to see which keys were being tried: \begin{minted}{powershell} ls ~\.ssh ssh -v root@147.182.178.136 \end{minted} The verbose output showed SSH only tries default key names such as \texttt{id\_rsa} and \texttt{id\_ed25519}. Our key had a custom name and had been saved one directory above \texttt{.ssh}, so SSH never found it. We found two problems and fixed them: \begin{enumerate} \item \textbf{The key was not on the droplet.} The droplet had been created before the SSH key was added to the team, so the key was never installed on it. We opened the droplet's web console (Access $\rightarrow$ Launch Droplet Console) and added the public key manually: \begin{minted}{bash} mkdir -p ~/.ssh nano ~/.ssh/authorized_keys # pasted the contents of digiOcean.pub chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys \end{minted} \item \textbf{SSH was not using our key.} We moved both key files into our \texttt{.ssh} folder (\verb|C:\Users\Billy\.ssh|) and told SSH which key to use with the \texttt{-i} flag. On Windows, \verb|~\.ssh\digiOcean| fails with a \texttt{tilde\_expand} error, so forward slashes are required: \begin{minted}{powershell} ssh -i ~/.ssh/digiOcean root@147.182.178.136 \end{minted} \end{enumerate} After these fixes, we were logged in to the droplet. \section{Installing Docker} \index{docker!installation} Docker was not preinstalled, so we installed it with Docker's official convenience script and confirmed the installation: \begin{minted}{bash} docker --version curl -fsSL https://get.docker.com | sh docker --version \end{minted} \section{Copying the Application to the Droplet} From a second PowerShell window on our local machine, we copied the application folder to the droplet with \texttt{scp}. Quotes are needed because the local path contains spaces. \begin{minted}[breaklines, breakanywhere]{powershell} scp -i C:\Users\Billy\.ssh\digiOcean -r ` "C:\Users\Billy\Documents\SSW 590 DevOps\color-buttons-app" ` root@147.182.178.136:~/ \end{minted} Long commands are split across lines for readability. In PowerShell, a backtick (\verb|`|) at the end of a line continues the command on the next line; in Bash, a backslash (\verb|\|) does the same. On the droplet, we inspected the project: \begin{minted}{bash} cd ~/color-buttons-app ls cat Dockerfile \end{minted} The project contains a \texttt{Dockerfile}, \texttt{package.json}, \texttt{server.js}, and a \texttt{public} folder with \texttt{index.html}. \section{The Dockerfile} The app's Dockerfile uses a lightweight Node.js 20 Alpine base image, installs production dependencies, copies the application code, and starts the server on port 3000: \begin{minted}{docker} FROM node:20-alpine WORKDIR /app # Install dependencies COPY package*.json ./ RUN npm install --omit=dev # Copy app code COPY . . # Expose port EXPOSE 3000 # Run app CMD ["npm", "start"] \end{minted} \section{Building and Running the Container} We built the image and ran it in the background, mapping the droplet's port 80 (the default HTTP port) to the container's port 3000. The \texttt{--restart unless-stopped} flag restarts the container automatically if it crashes or the droplet reboots. \begin{minted}{bash} docker build -t color-buttons-app . docker run -d -p 80:3000 --name color-buttons-app \ --restart unless-stopped color-buttons-app docker ps \end{minted} Visiting \url{http://147.182.178.136} in a browser showed the Color Buttons page, and both buttons changed the background color. Figure~\ref{fig:live-site} shows the live site after clicking each button. \begin{figure}[h] \centering \begin{minipage}{0.48\textwidth} \centering \includegraphics[width=\textwidth]{images/site-blue.png} \end{minipage}\hfill \begin{minipage}{0.48\textwidth} \centering \includegraphics[width=\textwidth]{images/site-red.png} \end{minipage} \caption{The deployed Color Buttons app at \texttt{147.182.178.136} after clicking Blue (left) and Red (right)} \label{fig:live-site} \end{figure} \section{Refactoring to Class-Based JavaScript} \subsection{Original Code} The original page attached a separate anonymous function to each button, repeating the same logic twice: \begin{minted}{javascript} document.getElementById("blueBtn").addEventListener("click", () => { document.body.style.backgroundColor = "blue"; }); document.getElementById("redBtn").addEventListener("click", () => { document.body.style.backgroundColor = "red"; }); \end{minted} \subsection{Refactored Code} We replaced this with two classes. \texttt{ColorButton} holds each button's state (its element, color, and target) and behavior (\texttt{attach} and \texttt{applyColor}). \texttt{ColorButtonApp} creates and manages the buttons. The color-changing logic is now written once and reused, and adding a new button takes a single \texttt{addButton} call. \begin{minted}{javascript} // Represents one button that sets a target's background color class ColorButton { constructor(buttonId, color, target) { this.element = document.getElementById(buttonId); this.color = color; this.target = target; } attach() { this.element.addEventListener("click", () => this.applyColor()); } applyColor() { this.target.style.backgroundColor = this.color; } } // Creates and manages all the color buttons on the page class ColorButtonApp { constructor(target) { this.target = target; this.buttons = []; } addButton(buttonId, color) { const button = new ColorButton(buttonId, color, this.target); button.attach(); this.buttons.push(button); return this; } } new ColorButtonApp(document.body) .addButton("blueBtn", "blue") .addButton("redBtn", "red"); \end{minted} \subsection{UML Class Diagram} \index{UML} Figure~\ref{fig:uml} shows the updated design. Each box lists a class's name, attributes, and methods. All members are public (\texttt{+}) since JavaScript class fields are public by default. The filled diamond shows composition: one \texttt{ColorButtonApp} creates and owns zero or more \texttt{ColorButton} objects. \begin{figure}[h] \centering \begin{tikzpicture}[ umlclass/.style={ rectangle split, rectangle split parts=3, draw, align=left, rectangle split part align={center, left, left}, font=\footnotesize\ttfamily, minimum width=7cm } ] \node[umlclass] (app) { \textbf{ColorButtonApp} \nodepart{second} + target : HTMLElement\\ + buttons : ColorButton[] \nodepart{third} + constructor(target : HTMLElement)\\ + addButton(buttonId : string, color : string) : ColorButtonApp }; \node[umlclass, below=1.8cm of app] (btn) { \textbf{ColorButton} \nodepart{second} + element : HTMLElement\\ + color : string\\ + target : HTMLElement \nodepart{third} + constructor(buttonId : string, color : string,\\ \phantom{+ constructor(}target : HTMLElement)\\ + attach() : void\\ + applyColor() : void }; \draw[{Diamond[length=10pt, width=6pt]}-] (app.south) -- (btn.north) node[pos=0.2, right] {1} node[pos=0.8, right] {0..*} node[midway, left] {creates}; \end{tikzpicture} \caption{UML class diagram of the refactored Color Buttons app} \label{fig:uml} \end{figure} \subsection{Redeploying the Updated Site} A Docker image is a snapshot of the files at build time, so editing \texttt{index.html} does not change the running container. To deploy the change, we copied the updated file to the droplet, rebuilt the image, and replaced the old container. From PowerShell on our local machine: \begin{minted}[breaklines, breakanywhere]{powershell} scp -i C:\Users\Billy\.ssh\digiOcean ` "C:\Users\Billy\Documents\SSW 590 DevOps\color-buttons-app\public\index.html" ` root@147.182.178.136:~/color-buttons-app/public/ \end{minted} On the droplet: \begin{minted}{bash} cd ~/color-buttons-app docker stop color-buttons-app docker rm color-buttons-app docker build -t color-buttons-app . docker run -d -p 80:3000 --name color-buttons-app \ --restart unless-stopped color-buttons-app \end{minted} \subsection{Verifying the Update} We confirmed the new code reached both the droplet and the running container by searching for the class definitions: \begin{minted}{bash} grep -n "class" ~/color-buttons-app/public/index.html docker exec color-buttons-app grep -n "class" /app/public/index.html \end{minted} Both commands returned: \begin{minted}{text} 27: class ColorButton { 44: class ColorButtonApp { \end{minted} Figure~\ref{fig:terminal} shows the rebuild, the verification inside the running container, and the full command history on the droplet. \begin{figure}[h] \centering \includegraphics[width=\textwidth]{images/terminal-history.png} \caption{Rebuilding the image, verifying the class-based code inside the container, and the droplet's command history} \label{fig:terminal} \end{figure} We also checked the page source in the browser (Ctrl+U) after a hard refresh (Ctrl+Shift+R) and confirmed the live site serves the class-based code. The page looks and behaves the same as before, as expected, since only the internal structure of the code changed. \section{Security Considerations} The droplet's IP address is public, as any website's must be. We rely on the following to keep the server safe: \begin{itemize} \item SSH accepts only key-based authentication, so password-guessing attacks cannot succeed. \item The private key stays on our local machine; only the public key is on the server. \item The site serves static content over HTTP with no logins or user data. HTTPS would be the next step for a production deployment. \end{itemize}