In this series, first part of guide was on how to install Docker and use commands, second part was installation of some UI to visually check what is going on. In Part 3 of Docker Tutorial For Beginners, we will learn basics about Docker File, Port, Containers, Environment, Registry and Tag. Normally if we write a Python application, we install a Python runtime on the machine. But, with Docker, we use a portable Python runtime as an image without actual installation on real machine. So achieve the goal, we need a direction file. That direction file is Dockerfile. Additionally, there will be a requirements text file listing what Python stuffs needed. Examples are given with Python because it is easy.
Docker Tutorial For Beginners : How To Create A Docker Application & Make it Running
We talked about Dockerfile. You can create an empty directory, take that it is located at /app
. Then create 3 files inside it :
1 2 3 | touch Dockerfile touch requirements.txt touch app.py |
That Dockerfile
will have this content :
---
1 2 3 4 5 6 7 | FROM python:2.7-slim WORKDIR /app ADD . /app RUN pip install -r requirements.txt EXPOSE 80 ENV NAME World CMD ["python", "app.py"] |
requirements.txt
will have this content :
1 2 | Flask Redis |
app.py
will have this content :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | from flask import Flask from redis import Redis, RedisError import os import socket # Connect to Redis redis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2) app = Flask(__name__) @app.route("/") def hello(): try: visits = redis.incr("counter") except RedisError: visits = "<i>cannot connect to Redis, counter disabled</i>" html = "<h3>Hello {name}!</h3>" \ "<b>Hostname:</b> {hostname}<br/>" \ "<b>Visits:</b> {visits}" return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits) if __name__ == "__main__": app.run(host='0.0.0.0', port=80) |
The above is official example. Now we need to learn how to build those 3 files to Docker app, tag it and run it. Let us run the commands :
1 2 3 4 | docker build -t newexamplepython . docker images docker run -d -p 4000:80 newexamplepython docker ps |
Now, if you point your browser to http://localhost:4000
, you’ll see the Python application running.
Next steps are optional and demonstration of how to push the application to some service, in the way we push application to GitHub repo. In part 2, we talked about Docker cloud. You need an account and run :
1 2 3 4 5 | docker login # example tag docker tag newexamplepython abhishekghosh/get-started:part3 docker images docker push abhishekghosh/get-started:part3 |
You can see the format of the commands used above :
1 | docker <command> username/repository:tag |
Now from that “repo”, we can “pull” the image on any computer and run it exactly like normally we can do with some Node.js app from Github.
Practice these steps with applications in different programming languages, combine with different Databases and test.
Tagged With Microsoft , docker for windows tutorial for beginners , docker tutorials , docker voor beginners