Skip to main content

Uploading Docker images to a repository

Overview

Docker makes it simple to share containerized applications by pushing images to public or private repositories like Docker Hub. This article explains the steps to build, tag, and upload a Docker image to a public repository, complete with a practical exercise.


Videotutorial

See also...

What is a Docker Repository?

A Docker repository is a centralized location where Docker images are stored and managed. Repositories can be public, allowing anyone to pull and use the images, or private, restricting access to authorized users. Docker Hub is the most popular public repository, but you can also use alternatives like Amazon Elastic Container Registry (ECR), Google Container Registry (GCR), or host your own private registry.

Repositories are crucial for sharing and distributing containerized applications. Each repository can host multiple versions of an image, identified by unique tags.

Understanding the Workflow

  1. Build: Create a Docker image from a Dockerfile.
  2. Tag: Assign a unique name to the image, usually including the repository name and version.
  3. Upload (Push): Send the tagged image to a public repository, such as Docker Hub, for others to access.

By following this workflow, you can ensure that your containerized application is easy to share and deploy across various environments.

Prerequisites

  • Docker installed on your system.
  • A Docker Hub account. (Sign up at Docker Hub if you don’t have one.)
  • Basic knowledge of Dockerfiles and containerization.

Practice: Building, Tagging, and Uploading a Docker Image

This practice demonstrates how to build a simple Node.js application, tag the resulting image, and upload it to Docker Hub.

1. Set Up the Application

Create a new directory for your project:

mkdir 05_docker-repository
cd 05_docker-repository

Inside the directory, create the following files:

  • app.js:

    const http = require('http');

    const server = http.createServer((req, res) => {
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.end('Hello, Docker Hub!\n');
    });

    server.listen(8080, () => {
    console.log('Server running on http://localhost:8080');
    });
  • package.json:

    {
    "name": "docker-repository",
    "version": "1.0.0",
    "main": "app.js",
    "dependencies": {
    "http": "^0.0.1-security"
    }
    }
  • Dockerfile:

    # Use Node.js as the base image
    FROM node:14

    # Set the working directory
    WORKDIR /app

    # Copy application files
    COPY package*.json ./
    COPY app.js ./

    # Install dependencies
    RUN npm install

    # Expose the application port
    EXPOSE 8080

    # Define the default command
    CMD ["node", "app.js"]

2. Build the Docker Image

Run the following command to build the Docker image:

docker build -t docker-repo-practice:1.0 .

3. Tag the Image

Docker Hub requires images to be tagged with your Docker Hub username and repository name. For example:

docker tag docker-repo-practice:1.0 <your-docker-hub-username>/docker-repo-practice:1.0

Replace <your-docker-hub-username> with your actual Docker Hub username.

4. Log In to Docker Hub

Authenticate with Docker Hub:

docker login

You’ll be prompted to enter your Docker Hub username and password.

5. Push the Image to Docker Hub

Upload the tagged image to Docker Hub:

docker push <your-docker-hub-username>/docker-repo-practice:1.0

6. Verify the Image on Docker Hub

Visit Docker Hub and log in to your account. Navigate to your repositories, and you should see the docker-repo-practice repository with the uploaded image.

7. Run the Image from Docker Hub

You (or anyone else) can pull and run the image from Docker Hub using:

docker run -p 8080:8080 <your-docker-hub-username>/docker-repo-practice:1.0

Open a browser and navigate to http://localhost:8080. You should see:

Hello, Docker Hub!

Summary

By building, tagging, and uploading images to Docker Hub, you make your applications accessible to others, enabling seamless sharing and deployment. This process is essential for collaborative development and simplifies distributing containerized applications across environments.