Category: Uncategorized

  • How to Build and Run Docker Images on Ubuntu: A Step-by-Step Guide

    How to Build and Run Docker Images on Ubuntu: A Step-by-Step Guide

    Docker has become a go-to tool for developers looking to containerize applications and streamline deployments. In this guide, we’ll walk you through the process of building and running Docker images on the latest version of Ubuntu. By the end, you’ll have a fully functional Nginx container serving a simple “Hello World!” web page.

    For those looking to run their Docker containers on powerful yet compact hardware, consider using an AMD Mini PC for your projects. You can explore purchase options here:
    👉 Check Out AMD Mini PCs

    If you’d prefer hosting your containers in the cloud for scalability and high availability, you can deploy your images on a Cloud VM instead:
    👉 Deploy on a Cloud VM

    Disclaimer: Don’t try this on a production system, do it on a dev or a test box that can be restored from scratch if anything goes wrong. Be sure to take a backup or snapshot of your system before you begin. If anything goes wrong you can just restore it and start over. Good luck!


    🚀 Step 1: Install Docker on Ubuntu

    First, ensure your system is up to date:

    sudo apt update && sudo apt upgrade -y
    

    Now, install the required packages:

    sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
    

    Next, add the Docker GPG key and repository:

    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
    echo "deb [arch=amd64 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
    

    Now, install Docker:

    sudo apt update
    sudo apt install -y docker-ce docker-ce-cli containerd.io
    

    Verify that Docker is installed correctly:

    docker --version
    

    To run Docker without sudo, add your user to the Docker group:

    sudo usermod -aG docker $USER
    

    Log out and back in for the changes to take effect.


    🏗 Step 2: Create a Dockerfile

    A Dockerfile is a script that defines how a Docker image should be built. Let’s create one that sets up an Nginx server and serves a “Hello World!” page.

    Create a new directory for your project:

    mkdir ~/docker-nginx && cd ~/docker-nginx
    

    Create a Dockerfile:

    nano Dockerfile
    

    Add the following contents:

    # Use the official Nginx image
    FROM nginx:latest
    
    # Remove the default Nginx HTML content
    RUN rm -rf /usr/share/nginx/html/*
    
    # Copy custom index.html into the container
    COPY index.html /usr/share/nginx/html/
    
    # Expose port 80
    EXPOSE 80
    
    # Start Nginx
    CMD ["nginx", "-g", "daemon off;"]
    

    Save and exit (press CTRL+X, then Y, then Enter).


    📝 Step 3: Create an HTML File

    Now, create a simple index.html file to serve as the homepage.

    nano index.html
    

    Add the following content:

    <!DOCTYPE html>
    <html>
    <head>
        <title>Docker Nginx</title>
    </head>
    <body>
        <h1>Hello World!</h1>
        <p>Welcome to my Nginx container running on Docker!</p>
    </body>
    </html>
    

    Save and exit.


    🔨 Step 4: Build and Run the Docker Image

    Now that everything is set up, let’s build the Docker image:

    docker build -t my-nginx .
    

    Once the image is built, run the container:

    docker run -d -p 8080:80 my-nginx
    

    Your Nginx server should now be running! To verify, open a web browser and go to:

    http://localhost:8080
    

    You should see the “Hello World!” page displayed. 🎉


    🏢 Step 5: Hosting Docker Images on a Cloud VM

    If you want to deploy your Docker container on a cloud server instead of your local machine, you can use a Cloud VM provider like Vultr:

    👉 Deploy on a Cloud VM

    Steps to Deploy on a Cloud VM:

    1. Create a new VM with Ubuntu installed.
    2. Follow Steps 1-4 to install Docker and set up your container.
    3. Allow external traffic to port 8080: sudo ufw allow 8080/tcp
    4. Access your site using the public IP address of your cloud VM: http://YOUR_VM_IP:8080

    🛠️ Managing Docker Containers

    To list running containers:

    docker ps
    

    To stop a running container:

    docker stop <container_id>
    

    To remove a container:

    docker rm <container_id>
    

    To remove an image:

    docker rmi my-nginx
    

    📌 Conclusion

    You’ve now successfully built and deployed an Nginx Docker container on Ubuntu! 🚀 Whether you’re running it locally on an AMD Mini PC or hosting it in the cloud, this setup allows for a lightweight and portable web server.

    Have questions? Drop them in the comments below! 🚀