top of page

How to Build and Deploy a Docker Container

Introduction


Docker has revolutionized software development by making it easier to package, distribute, and deploy applications. Whether you are a developer or a system administrator, understanding how to build and deploy a Docker container is essential. This guide will walk you through the process of creating a Docker container and deploying it to a production environment.

Docker Container

Just like physical shipping containers standardize the transportation of goods, Docker standardizes the deployment of applications across different environments.

Prerequisites


Before you start, ensure you have the following installed on your system:

  • Docker: Install Docker from Docker's official website.

  • A Code Editor: VS Code, Sublime Text, or any other text editor.

  • A Sample Application: A simple Node.js or Python application to containerize.


Step 1: Create a Sample Application


To demonstrate Dockerization, let's create a simple Node.js application.

  1. Initialize a Node.js Project:


    mkdir docker-demo && cd docker-demo npm init -y


  2. Install Express:


    npm install express


  3. Create server.js file:


    const express = require('express'); const app = express(); const PORT = 3000; app.get('/', (req, res) => { res.send('Hello, Docker!'); }); app.listen(PORT, () => { console.log(`Server is running on port ${PORT}`); });


Step 2: Create a Dockerfile


A Dockerfile is a script that contains a set of instructions to create a Docker image. Create a Dockerfile in the project directory and add the following:

# Use official Node.js image
FROM node:14

# Set the working directory
WORKDIR /app

# Copy package.json and install dependencies
COPY package.json .
RUN npm install

# Copy the entire project
COPY . .

# Expose the application port
EXPOSE 3000

# Define the command to run the app
CMD ["node", "server.js"]

Step 3: Build a Docker Image


Once the Dockerfile is ready, we need to build the Docker image. Run the following command in the project directory:

docker build -t my-docker-app .

This command:

  • docker build instructs Docker to create an image.

  • -t my-docker-app assigns the name my-docker-app to the image.

  • . specifies the current directory as the build context.


Step 4: Run the Docker Container


After building the image, we can create and run a container from it:

docker run -p 4000:3000 my-docker-app
  • -p 4000:3000 maps port 3000 inside the container to port 4000 on your local machine.

  • my-docker-app is the name of the image.


Now, open a browser and visit http://localhost:4000. You should see Hello, Docker! displayed on the screen.


Step 5: Push the Docker Image to Docker Hub


To deploy the application, we need to store the Docker image in a public or private repository like Docker Hub.


  1. Login to Docker Hub:


docker login -u your-username 2. **Tag the Image:** bash docker tag my-docker-app your-username/my-docker-app:v1 3. **Push the Image:** bash docker push your-username/my-docker-app:v1 ```


Step 6: Deploying the Container to a Cloud Provider


You can deploy the Docker container to various cloud platforms like AWS, Google Cloud, Azure, or DigitalOcean. Below is a simple deployment using Docker Compose.


Using Docker Compose


Create a docker-compose.yml file and add:

version: '3'
services:
  web:
    image: your-username/my-docker-app:v1
    ports:
      - "4000:3000"

Run the application using:

docker-compose up -d

This will run the application in detached mode (-d flag), keeping it running in the background.

How to Build and Deploy a Docker Container


Step 7: Deploying to Kubernetes (Optional)


For larger-scale deployments, Kubernetes is a popular choice. Here's a basic Kubernetes deployment file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-docker-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: my-docker-app
  template:
    metadata:
      labels:
        app: my-docker-app
    spec:
      containers:
      - name: my-docker-app
        image: your-username/my-docker-app:v1
        ports:
        - containerPort: 3000

Apply the deployment using:

kubectl apply -f deployment.yaml

Conclusion


Docker simplifies application deployment by allowing developers to package their applications along with dependencies into a container. In this guide, we covered:

  • Creating a simple Node.js application

  • Writing a Dockerfile

  • Building and running a Docker container

  • Pushing an image to Docker Hub

  • Deploying using Docker Compose and Kubernetes


With this knowledge, you can now deploy your applications efficiently and scale them as needed!

Subscribe to our newsletter

Comments


bottom of page