Publish your first Docker Image to Docker Hub

Rushal Verma
HackerNoon.com
3 min readJul 9, 2017

--

As you are familiar with Docker from my previous post. Let dive in to explore more.

Now you know how to run a container and pull an image, now we should publish our image for others too. Why you should have all the fun ;)

So what we need to publish our Docker Image?

  • A Dockerfile
  • Your App

Yeah, that’s it.

Why we need my app the Docker way?

Historically we have to our app(maybe python app) and we need python(or all dependencies) runtime environment on our machine. But then it creates a situation where the environment on your machine has to be just so in order for your app to run as expected and for your server too where you are running the server. With Docker, you don’t need anything(no environment). You can just grab a portable Python runtime as an image, no installation necessary. Then, your build can include the base Python image right alongside your app code, ensuring that your app, its dependencies, and the runtime, all travel together. These portable images are defined by something called a Dockerfile.

Dockerfile serves as the environment file inside the container. It helps in creating an isolated environment for your container, what ports will be exposed to outside world, what files you want to “copy in” to that environment. However, after doing that, you can expect that the build of your app defined in this Dockerfile will behave exactly the same wherever it runs.

So let’s create a directory and make a Dockerfile.

So you have your Dockerfile. You can see the syntax is pretty easy and self-explanatory.

Now we need our app. Let’s create one, a python app ;)

app.py

requirements.txt

Now you have all the things in order to proceed. Now just build your app.

Let’s Build it

ls will now show you this

Now create the image.

Where is your image? It’s in your local image registry.

Let’s Run it too

What we did here is mapping the port 4000 to the container exposed port 80. You should see a notice that Python is serving your app at http://0.0.0.0:80. But that message is coming from inside the container, which doesn’t know you mapped to port 80 of that container to 4000, making the URL http://localhost:4000. Go to that URL in a web browser to see the display content served up on a web page, including “Hello World” text and the container ID.

Let’s Share it :D

we will be pushing our built image to the registry so that we can use it anywhere. The Docker CLI uses Docker’s public registry by default.

  • Log into the Docker public registry on your local machine.(If you don’t have account make it here cloud.docker.com)
  • Tag the image: It is more like naming the version of the image. It’s optional but it is recommended as it helps in maintaining the version(same like ubuntu:16.04 and ubuntu:17.04)
  • Publish the image: Upload your tagged image to the repository: Once complete, the results of this upload are publicly available. If you log into Docker Hub, you will see the new image there, with its pull command.

Yeah, that’s it, you are done. Now you can go to Docker hub and can check about it also ;). You published your first image.

I found out this GitHub repository really awesome. Have a look on it https://github.com/jessfraz/dockerfiles

Do give me feedback for improvement ;)

--

--