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?
Yeah, that’s it.
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 pythonWORKDIR /appADD . /appRUN pip install -r requirements.txtEXPOSE 80ENV NAME worldCMD [“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 Flaskimport osimport 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.
ls will now show you this
$ lsapp.py requirements.txt Dockerfile
Now create the image.
docker build -t imagebuildinginprocess .
Where is your image? It’s in your local image registry.
$ docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEimagebuildinginprocess latest 4728a04a9d39 14 minutes ago 694MB
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.
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.
docker login
docker tag imagebuildinginprocess rusrushal13/get-started:part1
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 feedback for improvement ;)