Running Your First Own Container Images in Docker

In the last blog, we discussed the basics of Docker and how to start using Docker. You can read the blog here. Today we will discuss how to create and run your container image in Docker. Let’s get started. 

Docker Desktop is one of the easiest ways to start running a container. You can install docker from their official website.

fvg uiRqbznnTQY7 3Bi2QeQAp N5OhdWdfBIEuRCsulWdtQsktU0 hVZRQ 0 sacEi7jwy5c8vnyVRFelRtE1rxyJLuY6PMAG ZSUyX0GkM2Q139HO Be6sX1Ir6mfsWO7FMQNRXkvV751EITZSbcrQ4FraBGrmnP5aOlUXBcBdjgfbQRof JiWQ

Docker Desktop

Docker desktop gives you a straightforward way to use any Docker image and run a container. 

You can choose to use any image. To start with, we advise you to take an image from Docker Hub. As discussed in the previous blog, Docker Hub is a public repository of Docker images that are verified by millions of users and hence safe to use.

WBnzxdY3eiP1gWjY x7zpD2mLT22KvleWeVPcY1iHkGNHVPV9YKQqp3zZvMEPS kNBm ZKCKN7FI6dAIUnzPlQo q0sJD1C2hh4Qyi7orRmkCEQdl7TuF7NJGG9YGklL09xd reVC3e6e92tHM O0QoAkLNZXuRG18rymLMEbG ZhfSy0V loMX XQ

You can use these images and build your own on top of it. For this blog, we will use Docker’s own “getting started” image from Docker Hub.

g839L WqajlR1dBJqxzW7rdDmBV01vBiffUOrnvNnmkOJkXFTd9lv0sgTOa9Qgae 2My6QqyVIusXm EBxO 4GypmF4FKrP16BYcYUVVMJ5ppzENsYTS2Js3IiCrYfq7KTsfzhEPZ kspwa hD0rp UVB7mwNCST9gD19LVUl56qZEGPzX8M9pMrQ

Docker Desktop – Getting started image

Installing Docker desktop from the website installs all essential binaries needed to run both UI and command line. 

To pull the image from Docker Hub you can use the pull command on your terminal: 

$ docker pull docker/getting-started

Or you can run the container with a single docker run command.

aJoG fdgl4XEVTuN2EreT8c9GcYvM CoeyXlKzIJg17OyEhJfD5T TxoSAKAobYWgdnTOaB0uuwKByfTa 4xabLN2aMJl yaArqd4DOuefhgoule3VThm6UI69DtnRySbH8 IDFS8VoSZSXvPfFI7DhvHP2Ch2IDsP7VSSBTNKIJ M3HZbrfk20B5g

We explain these commands in greater detail in our previous Docker blog here. If you not comfortable with using the terminal, you can use Docker Desktop to do the same thing. 

Register on Docker Hub

By default, Docker Desktop uses Docker Hub to download images. You can, of course, configure other registries to download images from.

But before you download repositories from Docker Hub, you need to register and login to the Hub

E2O00QF6CXNF09FuEubv4jURqk9oLiQZiw2x650L7WBcHEZeN9VzJkBmRI6tW dZ0TX7SGTKFmxxxPJS1UJTHkRd7NrIo41iRmqk92W2zFTic3lR3WeZfGCWTOgHAt1X23RZHgZxb4runJb1cvZb7NWit8ku34VIo7Bd2mj1qql9AHLR YqlHKmh5g

Although the getting-started image can be accessed from Github without registration, it is a good idea to register on Docker Hub portal to access many of the stable versions of Docker images for free.

When you start Docker Desktop, it will start a tutorial on how to run the getting-started docker image. In the below steps, we will also tell you how to do the same on command line as well. 

The steps are similar for running any docker image from a Git repository. In the end, we will also tell you how to download any Docker image from Docker Hub.

How to run a docker image from GitHub

Step 1: Clone the repository

The GitHub repository we will use is this

xDoe1wb9VAuI2Ts3bMmv

The Docker Desktop tutorial will automatically open a terminal on the side and let you run the command there. 

577JxOZ8jUMekQB0SNCoeeIX mDYyyzOjmK8vHU4Qq vsPjJLN4wEFT0o1jl5kb 69FkJhMWKmkdzr0sx SEcehwUQ7cZxUmL fqFgwQ18GQsj XGmBHU3xshZs GMzWdN Qi aVOil6Gg9OKOcSipaiaoDfp99bW mEk1AIQqf1wAizKI cHQ2LGw

If you are not running the Docker Tutorial, you can open a terminal on your machine and run the below commands to download the GitHub repository.

 $ docker run –name repo alpine/git clone https://github.com/docker/getting-started.git

$ docker cp repo:/git/getting-started/ .

These commands will basically copy the repository locally into our machine. Next, we need to build a docker image from the repository. Let’s see how to do it.

Step 2: Build the docker image

If you are running Docker Desktop, it will take you to the next step that builds a docker image from the repository. Click on the blue icon to copy the commands to the adjacent terminal.

c4NNL2F5Z1GTOodWE9PJ1Lj1USBk1y4vQnUUg4nEesbOZO

You can do the same on command line by running the below commands:

$ cd getting-started

This command will take you inside the local directory “getting-started”.

$ docker build -t docker101tutorial .

Docker build command helps create a docker image called docker101tutorial. This command reads the docker file inside the directory to know what to create in the image. 

DXNsIEsBmNfKAOcuyjI4HO4qYKOXpw7 BHkqMYaEAPEo22niVZl yL1 ajGqKvR74mcj 4cHzvsy0lZolkkr2q0HPZxm5FRaaJr6 v2 dPt0ldYv QjjFgt25 oN1DWrvf7yBn57rPlnRIskIvNTuwMyg2HNqc1FE52fsy 7wvsbksW2p 8XwDWCKw

Here’s a snippet of the Docker file in the getting-started repository.

3AebZy7KASUG1NJmcj5olvf 39oAcJdv0vnK5pAIjQLg4gdr0x0eTJcjCbIyRfPin5jlsPnjcXef7xjGUqhEknlwV8FiX yEPh3h Rz4NPVupoh8VtdtDOU 3qJHIFjACfONLWuONBi 6LQPDvZUTAqBoS5NevCfm3vUpswQpO78PY77PbmfPcV60Q

The Docker file helps in getting the necessary binaries to build a self-sufficient image. You can see the comments on the Dockerfile to understand which section does what. If you notice the last part set ups an nginx server to host static content. 


The containerized application eventually installs an application server and hosts some static content. Sections above that mentions all dependencies and gathers them to build the image. 

Let’s now try and run this image and see how the application comes up.

Step 3: Run the docker container

The next step is to run the container from the image. Docker run command will run a container based on the image and launch the application in it.

You can click on the blue button to make Docker desktop run the command on the right side of the screen. 

ArAxn0oR01QWfQv27kf4htt6ALivmgBisgbFC1wnaVkjc4evAKy1k5xV 00A75HRTXRcpGYTffjyCdNGwyNa4PeV1RUu7l7NFNt 2NhZEQoRgZ03bZbW zh98OBNynHEC mjRskCTZTYfTsXOtBBefQUU4Oo5e97kPsG8RTeq1FXdOi89twaiW64mA

If you are running it on a terminal, you can run the below command:

$ docker run -d -p 80:80 –name docker-tutorial docker101tutorial

This will start the container and run the docker tutorial application on localhost. You should now be able to access the application on your browser. 

Open your local browser and go to URL: http://localhost/tutorial/. You will see a screen similar to below: 

W6CbBRLnhOAYZBYLrzQ144AlhBN1xhi6Id Lzp1c dVEre3cUq CQh ltSH8dKRTWpi3 6A5eUjJgF9 ekItOj43es2 Y zi 3rrv0NlfYpDdmp75D7P MhemgNH Z1EP8jgXemdNg y5hCV Pdjq oXo326nf9Z3keQzeylTKIG6CSG5o tavT0Q

Congratulations! You have now run your first container image on docker. This is a full fledged application server nginx hosting a docker tutorial content, fully isolated from host OS.

If you are using Docker Desktop, you can check the status of your containers and images in the main dashboard.

STIDWLLBsQo1n4Fy4hHQocWUHnF0e18QKxBX8SeYbRWbg77aSZd7CWXM6xNsWUdzgjAicc6NFubcMrZlQqSCAzqv3mfIhRSopl2gSZ7TGe6glvhttuq

Troubleshooting errors

In case you have any errors during the execution of a container, you can check the logs and do other analysis by clicking on the container. Docker desktop provides many options to troubleshoot and other management controls.

When you click on the container, you will see a screen as below.

ES5LLdQgBo5KpEGqXf0j845w1xSs1ZxUrLm1b5XEJu5ol8E 6S3nQOPdrPxIxNUqsT12G cORiLn3y7oz2WNRk8p1GqJyYZDXIYo5E0eimiAwS26kfqcEHgolYlkjqqbarqlPMJWxG4f8p6oLLtDRMYp8SSXEcKxZWS

How to run other images from Docker Hub

The steps to run any docker image from Docker registry is pretty straightforward. By default, Docker Hub is the configured with docker. Once you pick the image you wish to run, copy the command you see on the right and execute it on your terminal window.

ffIdIbzoGV3nDu9qnkEf5uvxU2MLaa8 2Q7PNElQFlMr9ehdRBRcA5LfG7JtJXRDTh d8bEWsAtORu NOpv9cMksfUPoAbF7oi7jiwPRUp5iy pZtmbVRL4XsmXmAsFp VSaToTmqpK3ux0OhVgwYUHkDeOHbPWk m4lNjwdf3U9pcrRA8SoBvtKw

When you run the command “docker pull” mentioned in the Docker Hub page, it will download the image locally. You will see an output similar to what you see below. 

K

Once the download is complete, you will see the image in the list of local images in the Docker Desktop.

2gPf2Jjf4ktXaur2Su1DAQ5hCoef8vjmt FrDrKnmcQXKoeN3G3bydoa4heJAKH y9O8N3IJ3PC84FWaiNwNG0jeKruPUqWBu5 ZC2JmBKmBpiEa58Npnn E4ToVy6H cdW03WHDNqHoxjSMr7Vghv5lTjdrgeglfyzqhGYquVuHM8

You can now run the container by clicking on blue “Run” button beside the image. 

SpP1caCkHmBez8kKpnweMfxRiKbVlMfUdHewcUd0XyaONeuy19YNHStiSMDU647Am1HakUiuUvsDi10nfsxTp8CgxBpJGHEQRfKnj9D8UobdWGzz4EJ5n3KEghxRffbvDwuQfLHUxW6sVwA 4vXoaPPjRy84htOxUVM

And that is it. That is how you can run any container image in Docker.

Creating your own docker image

You can also create your own docker image and push it to Docker Hub. Start by logging into Docker Hub and go to Repositories > Create a repository.

Name your repo and give a description. Free account on Docker Hub allows only one private repo. You can create as many public repositories as you wish. 

In the below example, we are creating a public repo called “abc

vD7dv5vLI AMCfaeeTnFQXMy4tx 82 2XU2l0bynO6cc1dNseZI124NgXEWOGPoj7j0qGNObgViT 3MsSnZZ uGWhF1f5mV

This will create an empty docker repository. 

ST8biZ7RlIeokDBMmv jX1G31 vmK64qJVFf h3BFMt 83U95IdvWg41sQpI9xHciO4zHSVDqgMOMASBJGyBU3vSO Rbm6z Q4r jVz0BWoz0I3gILjFiSpsMfR

You can start filling this repository with image files, starting with a Dockerfile. Create a directory in your local machine to host your Dockerfile and write a simple hello world content inside the Dockerfile.

See a sample below:

niO0TbufXsA3AHjTjX33OS25HKqLmGS

Docker content we wrote was as follows:

WLWG5g2TersPOiljsiCnyIXcvXfu qcIGLh31Uk2o4yU5J

Once you have created this simple dockerfile. You can build the docker image with the Dockerfile using the below commands:

$ docker build -t <your-username>/<your-repo-name> .

This takes everything in the current directory and creates a docker image with dockerfile which is also in the current directory. 

dBMSnijlB8zd6vumZPsJ mnptG9jHqT z2OzDFrwtgxfy0QxLhBoVap7WJ8b9MhtqWjCi9lpsWrM62P mVRPBkgmo9WzLP9KsyQrzhfusxp9iAls18ZoaNhPSupBTVvpu29AeCDzFkt5sL2czoJdyJJJmKSC7L1tqGeCHuOnXRSax EYf679YtN Sg

Next, test the docker image by running the image with docker run command.

$ docker run <your-username>/<your-repo-name>

mBjvQ9niEDTH2IqCJ1 UhBcBoRkskLTCSp77rDwPUaIeROQFbH0wMw8Wbw87P GM JEuTqbH iz84tjolEhzbt8vj3NfXgzTCExbkoBvPptffBRWDoPeyoz2zda4YK7bebxOhNsFS0CzlkmsKC6ZD3

If you open Docker Desktop, you will see the container listed in the list. 

nT4adi1Ib4aoB1rT6uVqxrYF FW9 pf81b 272lBiIXXUeQJJTzzvMHwoY XW7NRv6VQH rwedvcixKlQeIsJzH 929Vs r1XRBrQ1 stvc 9 cH9Y8IFPGPFiYJUjQAwJtY1tc3Begikz24DgY53ewn3gGMq6FOlvtG9 4iaItPkybszoLyJ rl8A

latest” is the tag given to the image. Once you have tested your container, you can push your new container image to Docket hub with “docker push” command.

$ docker push <your-username>/<your-repo-name>

The output would look similar to below:

MmYnuVzb Tf GRhYIXA7V qFvE6hrIchBoBjsrSZnSmwukxs8a4BTrjPseb0CYful7MN65PmD9SNIJAg2BwQG KgMI8ucXqVZ1HfYod0eEFh

Congratulations! You have successfully created your own docker image and run it. The image is now uploaded to your Docker Hub account and it is available for the world to use.

You can go to Docker Hub and click on Repositories > your-reponame to see your docker image uploaded on the site.

4WmAWbW95 ZpghoEuVJagV7j6 FsB1W8sS6alizDKZUvSBMOk3jkGh

And that is how you create and run your own container image in Docker.

But no infrastructure runs one or two containers for any practical use. There are usually thousands of containers that run different parts of the system. Once you start running multiple containers, you will need a way to manage containers better. 

Taikun – An easier alternative to manage your container infrastructure

Taikun provides a singular dashboard for all container orchestration across any kind of cloud. Taikun can be setup in private clouds, hybrid clouds or public clouds. It supports all major public cloud service providers like Google Cloud, Amazon AWS, Microsoft Azure and OpenStack. 

S1KrWv86myZTr86 6rClbwgYDHmBz1KHtRUlCT ZYzcoZZEgArhbdxMWsbEB0vG2qHspUCgAAbouK2GQY9njCzGVokWD1TxnE ApevpkpYZLSDEsqiGGVKRH5G8ur0zqhqsesk KL2EtQ tzy9M2ZbFUq76OfFquFUr6qBd5MZRFX2D R6K5dn9atw

Taikun helps get container management right.  Taikun to get ahead and leverage the power of containers quickly.

Try it now for FREE         Schedule a call with us