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 ?
Portability: Docker containers run the same way on any system—whether it's your laptop, a testing server, or a production cloud instance.
Consistency: Eliminates the "works on my machine" problem by ensuring uniform environments.
Efficiency: Containers are lightweight and use fewer resources than traditional virtual machines.
Rapid Deployment: Docker simplifies and speeds up the deployment process.
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:
- One teammate might have Node.js 18, another has Node.js 16, and things break.
- Setting up tools for each new project takes time.
How Docker Helps:
- Share the docker-compose.yml file with your team.
- Everyone runs docker-compose up and gets the exact same environment, regardless of their computer.
Why this is helpful:
- No "works on my machine" issues.
- 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:
- Your app code.
- Node.js (the version your app needs).
- 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
- 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
Step 2: Install Docker
1. Uninstall Old Versions (if any):
sudo apt remove docker docker-engine docker.io containerd runc
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
3. Verify Docker Installation:
docker --version
Step 3: Allow Docker Without sudo
- Add your user to the docker group:
sudo usermod -aG docker $USER
- 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.
- 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
- Make it executable
sudo chmod +x /usr/local/bin/docker-compose
- Verify the installation:
docker-compose --version
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
2. Verify the installation:
colima version
Step 2: Install Docker CLI
- Install Docker CLI tools via Homebrew:
brew install docker docker-compose
- Verify the installation:
docker --version
docker-compose --version
Step 3: Start Colima
- Start Colima with Docker support:
colima start
- Verify Colima is running:
colima status
Step 4: Test Docker with Colima
- Run the hello-world Docker image to confirm everything works:
docker run hello-world
- Check running containers:
docker ps
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)