DEV Community

Hari Krishnan
Hari Krishnan

Posted on

FSD - 1.1.4 - Setting up Docker

What is Docker ?

Docker is an open-source platform that allows developers to automate the deployment of applications within lightweight, portable containers. These containers bundle the application code with all its dependencies, libraries, and configurations, ensuring it runs consistently across various environments.

Why do we need Docker ?

  1. Portability: Docker containers run the same way on any system—whether it's your laptop, a testing server, or a production cloud instance.

  2. Consistency: Eliminates the "works on my machine" problem by ensuring uniform environments.

  3. Efficiency: Containers are lightweight and use fewer resources than traditional virtual machines.

  4. Rapid Deployment: Docker simplifies and speeds up the deployment process.

  5. Isolation: Each container is isolated, reducing conflicts between dependencies and improving security.

Using Docker for Development

1. Set Up a Local Development Environment

The Problem Without Docker:

  • You need to install the right version of Node.js, databases (like MongoDB), or other tools manually.
  • Different versions or setups can cause "it works on my computer but not on yours" issues.

How Docker Helps:

  • You use Docker images (pre-configured setups) for Node.js and any other tools you need.
  • Your team gets the same environment on their machines with just one file

2. Collaborate Easily with Your Team

The Problem Without Docker:

  1. One teammate might have Node.js 18, another has Node.js 16, and things break.
  2. Setting up tools for each new project takes time.

How Docker Helps:

  1. Share the docker-compose.yml file with your team.
  2. Everyone runs docker-compose up and gets the exact same environment, regardless of their computer.

Why this is helpful:

  1. No "works on my machine" issues.
  2. New team members can start coding immediately without setup headaches.

3. Test Different Setups Easily

Example

  • Your app works with Node.js 18, but you want to test it with Node.js 16.
  • Instead of reinstalling Node.js, just switch the version in your Docker setup and restart

Using Docker for Production

App Deployments

Think of Docker as a way to pack your app into a "box" (called a container) that includes everything it needs to run. This makes sure your app behaves the same, no matter where it's running.

Deploy Your Node.js App with Docker

Imagine you're building a Node.js app (e.g., a website or API) and want to run it on a server for users to access. Instead of setting up Node.js manually on the server, you package your app with Docker.

Steps:

Create a Docker Image:

This is like making a blueprint for your app that includes:

  1. Your app code.
  2. Node.js (the version your app needs).
  3. Any libraries your app uses.

Push the Image to a Registry:

Think of this as uploading your "box" to a storage area (e.g., Docker Hub or AWS).

Run the App on a Server:

Use the image to start a container on a server.

Example:

You have a Node.js app that runs on port 3000. When you run the container, your app is ready to serve requests on that port.

Why this is helpful:

  • No worries about whether the server has the right Node.js version or dependencies.
  • If something works on your computer, it'll work on the server too.

SETTING UP DOCKER

  1. Setting Up Docker on Ubuntu

Step 1: Update Your System

Run the following commands to update your system packages:

sudo apt update
sudo apt upgrade -y
Enter fullscreen mode Exit fullscreen mode

Step 2: Install Docker

1. Uninstall Old Versions (if any):

sudo apt remove docker docker-engine docker.io containerd runc
Enter fullscreen mode Exit fullscreen mode

2. Install Docker Using the Official Repository:

sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io
Enter fullscreen mode Exit fullscreen mode

3. Verify Docker Installation:

docker --version
Enter fullscreen mode Exit fullscreen mode

Step 3: Allow Docker Without sudo

  1. Add your user to the docker group:
sudo usermod -aG docker $USER
Enter fullscreen mode Exit fullscreen mode
  1. Log out and log back in for the changes to take effect.

Step 4: Install Docker Compose

Docker Compose is a tool to manage multi-container applications.

  1. Install it with:
sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
Enter fullscreen mode Exit fullscreen mode
  1. Make it executable
sudo chmod +x /usr/local/bin/docker-compose
Enter fullscreen mode Exit fullscreen mode
  1. Verify the installation:
docker-compose --version
Enter fullscreen mode Exit fullscreen mode

2. Setting Up Docker on macOS Using Colima

Colima is a lightweight, Docker-compatible container runtime for macOS that doesn’t require Docker Desktop. It uses qemu under the hood and integrates seamlessly with the Docker CLI.

We use this as Docker Desktop is not for free anymore.

Step 1: Install Colima

1. Install Colima via Homebrew:

brew install colima
Enter fullscreen mode Exit fullscreen mode

2. Verify the installation:

colima version
Enter fullscreen mode Exit fullscreen mode

Step 2: Install Docker CLI

  1. Install Docker CLI tools via Homebrew:
brew install docker docker-compose
Enter fullscreen mode Exit fullscreen mode
  1. Verify the installation:
docker --version
docker-compose --version
Enter fullscreen mode Exit fullscreen mode

Step 3: Start Colima

  1. Start Colima with Docker support:
colima start
Enter fullscreen mode Exit fullscreen mode
  1. Verify Colima is running:
colima status
Enter fullscreen mode Exit fullscreen mode

Step 4: Test Docker with Colima

  1. Run the hello-world Docker image to confirm everything works:
docker run hello-world
Enter fullscreen mode Exit fullscreen mode
  1. Check running containers:
docker ps
Enter fullscreen mode Exit fullscreen mode

At this point, we're setting up the Docker environment, which might feel overwhelming. Don't worry—later, we'll dive into what Docker is and how it will help us during development and production as we build the app.

Top comments (0)