GeoJSON
Spring
Hibernate
Liquid
Karma
Deploy
SASS
REST
Upgrade
Boot
Spring
Consume
Visualize
React
Angular

Down by the Ubuntu Docks

Down by the Ubuntu Docks

Developing apps with Docker is pretty much the standard these days.

Docker is an open-source platform that enables developers to automate the deployment, scaling, and management of applications using containerization. Containers are lightweight, portable, and self-sufficient units that encapsulate an application and its dependencies, allowing it to run consistently across different environments, from development to production.

Let's write a Dockerfile to spin up a Node App in Ubuntu.

# Use the official Ubuntu as the base image
FROM ubuntu:latest

# Set environment variables for non-interactive installation
ENV DEBIAN_FRONTEND=noninteractive

# Update package repositories and install Node.js and npm
RUN apt-get update && \
apt-get install -y curl && \
curl -fsSL https://deb.nodesource.com/setup_14.x | bash - && \
apt-get install -y nodejs && \
apt-get clean

# Display installed Node.js and npm versions
RUN node -v && npm -v

# Create and set the working directory
WORKDIR /app

# Copy your application files into the container
COPY . .

# Install application dependencies
RUN npm install

# Expose a port (if your application needs it)
EXPOSE 3000

# Start your application
CMD ["npm", "start"]

In this Dockerfile:

We use the official Ubuntu image as the base image.

We update the package repositories and install curl to fetch the Node.js installation script.

We use curl to download and run the Node.js setup script, which adds the official Node.js repository.

We install Node.js and npm using apt-get.

We set the working directory to /app in the container.

We copy your application files into the container.

We run npm install to install your application's dependencies.

We expose port 3000 (you can adjust this as needed based on your application).

We set the default command to start your application using npm start.

To build and run the container:

Create a directory containing your application files and a package.json file.

Place the Dockerfile in the same directory as your application files.

Open a terminal and navigate to the directory.

Build the Docker image using the command: docker build -t my-node-app . (replace my-node-app with your desired image name).

Run the Docker container using the command: docker run -p 3000:3000 my-node-app (replace my-node-app with the image name).

This Dockerfile sets up a basic Node.js environment in an Ubuntu-based container. You can customize it further based on your specific requirements and application setup.

Now let's write a Docker Compose file to run the Dockerfile. Docker Compose is an open-source tool that allows you to define and manage multi-container applications using a simple YAML file. It helps simplify the process of orchestrating the deployment and management of multiple Docker containers as a single application stack.

Docker Compose provides a way to:

Define Services: You can define each component of your application as a service in a docker-compose.yml file. A service is a containerized component, such as a web server, database, or application backend.

Specify Configuration: Docker Compose lets you specify various configuration options for each service, including the base image, environment variables, networking, ports, volumes, and more.

Link Containers: You can define dependencies between services and establish communication between them, such as linking a web server container with a database container.

Orchestrate Containers: With a single command (docker-compose up), Docker Compose can create and start all the necessary containers in the defined services, ensuring they run together as a cohesive application.

Scale Services: Docker Compose allows you to scale services up or down by specifying the desired number of instances for each service.

Network Isolation: Containers created by Docker Compose are isolated within a private network, which helps prevent conflicts with other containers running on the same host.

Here is our Docker Compose file.

version: '3'
services:
my-node-app:
image: my-node-app:latest
ports:
- "3000:3000"

In this docker-compose.yml file:

version: '3' specifies the version of the Docker Compose file format.

services: defines a list of services to be created.

my-node-app: is the name of the service.

image: my-node-app:latest specifies the Docker image to use for the service. Replace my-node-app with the name you used when building the Docker image. latest is the tag of the image.

ports: maps the port 3000 from the host to port 3000 in the container.

To use this docker-compose.yml file:

Save the file in the same directory as your Dockerfile.

Open a terminal and navigate to the directory containing the docker-compose.yml file.

Run the command docker-compose up to start the application. Docker Compose will build the image if it doesn't exist and then create and run the container.

Make sure you have Docker and Docker Compose installed on your system before using this docker-compose.yml file. Additionally, replace my-node-app with the actual name of your Docker image if it's different. Adjust the ports or other configuration settings as needed based on your application's requirements.