You can check the original post in my personal blog
What is Docker Image
Docker image is a snapshot of a container. It's immutable and inert. Container is an instance of an image.
What is Dockerfiles
Is a file that contains instructions to create an image and running it the way you want.
Creating a Dockerfile
This is an example of code that we're gonna put inside a docker image after creating one from a dockerfile.
Python code to app.py
from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "hello world!" if __name__ == "__main__": app.run(host="0.0.0.0")
Example of Dockerfile that will build the image:
FROM python:3.6.1-alpine RUN pip install flask CMD ["python", "app.py"] COPY app.py /app.py
FROM python:3.6.1-alpine means that you're using a python 3.6.1 built-in Alpine image, you can see what have in the image looking for the tag in the official Python page on Docker Hub. More light the OS, faster is the deploy.
By security measures it's recommended to verify if the image you are using is the official from the app or language and if it's certified by Docker. Also, verify what have in the dockerfile.
RUN pip install flask is the call to run pip install flask command, which is the dependency of our app. Run serves to execute the following command to configure the container environment. Usually used to install dependencies or build the project.
CMD ["python", "app.py"] is the command that will be executed after you initialized the container You must be careful to only have one CMD for dockerfile. If have more than one, it will always execute the last.
COPY app.py /app.py add a copy of app.py file inside the container in the /app.py folder. It looks kind of odd putting after CMD command, but because of layers, in case you change the file, the cache will only modify the last line in the image.
There is no need to have python, pip, and flask installed. The vantage of using docker is that you don't need to have all installed to run the app, only need a container. After you finished using, just delete the container.
The dockerfile has all the instructions the built the image and have it running (container) the way you desire.
Building the image
docker image build -t python-hello-world .
-t allows you to add a name to the image.
If you change some file, just run docker image build again
Check if the image was built.
docker image ls
Shows all images that you have created or download from docker hub.
Executing the docker image
docker run -p 5001:5000 -d python-hello-world
-p maps the port that is running inside the container with the one from the host.
-d run the image in the background
You can check on localhost:5001 if the container is running correctly.
It's basicly the series of steps which the container will built from the image. Which command on the docker file will be a layer. It's important to notice that everytime a layer is changed, the consectives are gonna change either. Because if one step has changed, the consectives steps must run to create the image. So, for bether performance, add the line that will have more changes for the last.
This question from stackoverflow has a good answer for that and this medium post from Jessica G either
So that's all, folks. You learned how to create a dockerfile and image. Thanks for reading and keep going. Don't forget to drink water and eat clean..
Top comments (0)