Step-by-Step Guide Building and Pushing a Docker Image to a Docker Repository.

EmmyEmmy
4 min read

Introduction

What is Docker? Docker is an open platform for developing and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly.

Assume you want to launch an application on your computer normally, you would install all of the software and dependencies required for the application, but this can be time consuming.

Docker addresses this issue by allowing you to package your application and everything it requires (such as configurations, and dependencies) into a “container.” You can then run this container on any computer with Docker installed, and it will function exactly the same way each time.

Think of Docker as a car with its engine, the engine makes the car move. If you’re new to Docker, don’t worry! This guide will break everything down for you, step-by-step. You’ll learn how to take a simple application, build it into a Docker image, and push it to Docker Hub so you or others can easily run it.

What is an image?

Without going too deep yet, think of an image as a recipe for cooking a particular dish
It contains the ingredients and instructions on how to make that particular dish.

Before moving further, we need to know what a container is.
In a simple definition, a Container is an instance of an image. This is the environment where your application actually runs.

Any machine that runs this container using this image, will then be able to run the application as it was built without needing anything else pre-installed on the machine.

Prerequisites

Before we begin, ensure you have the following:

  • Docker Installed: Docker must be installed on your computer. You can download it from here.

  • Docker Account: Sign up for a free Docker Hub account here

  • Sample Application: For this guide, let’s use a basic Python app you can download here app.py

Step 1: Create a Docker file

Before building a Docker image, we need to tell Docker how to set up the environment for your application. This is done using a Docker file. It’s a simple text file that contains all the commands needed to build your image.
Think of this as a manual or instructions on how to build the image.
NOTE: You can also download an image from Docker Hub and build your own application from it.

Create a folder called my-docker-app on your computer
Inside that folder, create a file named app.py
In the same folder, create a file named requirements.txt

What we’ve done:

  • Created a Python app that uses a library called Flask to display “Shopping made easy” in a browser.

  • Listed Flask as a dependency in requirements.txt

These are the contents or instructions inside our Docker file

Use an official Python runtime as a parent image

FROM python:3.9-slim

Set the working directory in the container

WORKDIR /app

COPY . /app

RUN pip install — no-cache-dir -r requirements.txt

Step 2: Build the Docker Image

Now let’s use the Docker file to build the image for the app.
Open a terminal or command prompt, i use Vs code (visual Studio) code editor, its faster and easy to navigate. Now you don't have to know all the commands, when you use it everyday you ll get familiar with the commands.

Build the Image

$ docker build -t image3:img3 .

Verify the image is built successfully

$ docker images

Tag the Image

$ docker tag img3 :img3

Tagging the image makes it easier to identify the latest version of an image

Run the image and start up container

$ docker run -d -p 5000:9000 image3:img3

Maps port 5000 we exposed in our Python file on your computer to port 9000 in the container.

Verify the container is running

$ docker ps

No we have the image and container running, lets verify that the URL is working when you hit the port number assigned to the URL
We can either do that on a browser or CURL (command line utility) to check a URL page
Open your browser and go to http://localhost:5000. You’ll see our message we defined in our python app.
I used CURL on terminal and below is the response from the URL.

Confirm the contents of the URL

$ curl localhost:5000
Shopping made easy

We have successfully built our image and confirmed the URL is working, now lets push our image to Docker Hub

Step3 :Push our image to Docker Hub

First we need to login to our Docker Hub account we created

$ docker login

We tag our image to our Docker Hub username and Repo

$ docker tag image3:img3 emmyjegz/prodapp:img3

After tagging push the image to Docker Hub where it can be downloaded on another machine without having to install the dependencies

$ docker push emmyjegz/prodapp:img3
The push refers to repository [docker.io/emmyjegz/prodapp]

We can Login to our Docker hub Account on the web browser to confirm that our image has been successfully pushed, or we can simply do a pull request which means to download an image from Docker Hub.

We have successfully built our image and pushed to our repository on Docker Hub and verified our app is up and running, we tested it locally when we hit the port address we exposed when building our app and we got our response from the URL.

1
Subscribe to my newsletter

Read articles from Emmy directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Emmy
Emmy

I'm passionate about implementing, and managing scalable and secure cloud infrastructure with expertise in platforms like AWS, Azure. Staying updated with cloud technologies, and enabling businesses to leverage the full potential of the cloud.