DEV Community

Rushal Verma
Rushal Verma

Posted on

Publish your first image to Docker Hub

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.

FROM python:3.6
WORKDIR /app
ADD . /app
RUN pip install -r requirements.txt
EXPOSE 80
ENV NAME world
CMD [“python”, “app.py”]

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

from flask import Flask
import os
import socket
app = Flask(__name__)
@app.route("/")
def hello():
    html = "<h3>Hello {name}!</h3>" \
           "<b>Hostname:</b> {hostname}<br/>"
    return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname())
if __name__ == "__main__":
    app.run(host='0.0.0.0', port=80)

requirements.txt

Flask

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

$ ls
app.py        requirements.txt        Dockerfile

Now create the image.

docker build -t imagebuildinginprocess .

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

$ docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
imagebuildinginprocess    latest              4728a04a9d39        14 minutes ago      694MB

Lets Run it too

docker run -p 4000:80 imagebuildinginprocess

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)
docker login
  • 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)
docker tag imagebuildinginprocess rusrushal13/get-started:part1
  • 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.
docker push rusrushal13/get-started:part1

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 feedbacks for improvement ;)

Top comments (4)

Collapse
 
kakty3 profile image
kakty3

It is better to explicitly set docker image tag, e.g:

FROM python:3.6
Enter fullscreen mode Exit fullscreen mode

Otherwise, it is unpredictable, which image version will be base.

Collapse
 
rusrushal13 profile image
Rushal Verma

Thank you for correcting me. I updated the post :)

Collapse
 
rusrushal13 profile image
Rushal Verma • Edited

Every time Docker uses the latest base image. Here it is python 2.7 base image, as it is official, You can run the container and can check it ;)
BTW thanks for your feedback

Collapse
 
kakty3 profile image
kakty3

No, python:latest stays for python:3.6, you can see it on Dockehub: library/python