In software develompent, containerization is a method to package an application and its dependencies into a single image that can be run on any machine. Docker is a popular platform for developing, shipping, and running applications in containers.
- Each app runs in its own container, which is like a lightweight, stand-alone, executable package of a piece of software that includes everything needed to run it: code, runtime, system tools, system libraries, and settings.
- Containers isolate software from its surroundings, for example, differences between development and staging environments and help reduce conflicts between teams running different software on the same infrastructure. When compared to virtual machines, containers are more lightweight and efficient.
- By containerizing an application, the developer can be sure that the application will run on any other machine regardless of any customized settings that machine might have that could differ from the machine used for writing and testing the code.
The 2024 Octoverse report highlights the growing adoption of Dockerfiles as part of the broader trend toward cloud-native development and Infrastructure as Code (IaC) practices. Developers increasingly rely on Dockerfiles to standardize and automate deployments. To learn more about Docker, you can watch the following video: Docker Overview.
In this lab, we will containerize a Python Flask app using Docker
Open a new VSCode terminal.
Check that the docker command-line client is available.
docker --versionCreate a folder flask-hello2complete the following tasks.
mkdir flask-hello2
cd flask-hello2
Pull a docker inmage python:slim, a lightweight version of python docker image, from Dockerhub. The name of the image is python and the tag is slim.
docker pull python:slim
The Docker image will be pulled to the Docker host if it is not available.
Check that the image is successfully pulled.
docker image ls
Run the image and launch the bash shell.
docker run -it python:slim bashChecking that the python:slim Docker container is running.
In the terminal, check the Python version inside the Docker container.
python --versionWhat is the version of Python inside the container?
Run a python statement inside the container.
python -c "print(1+2)"Exit the bash shell.
exitAfter you have exited the container's shell, the container is now in the stopped state.
In the Docker VSCode extension, right-click the stoped (exited) container and select removeto remove the container.
Inside the flask-hello2 folder, create a Python script app.py as follows.
import os
import socket
from flask import Flask,request,jsonify
app = Flask(__name__)
@app.route("/")
def main():
return "Welcome Python flask!"
@app.route('/about')
def about():
return 'I am '+socket.gethostname()
@app.route('/users')
def get_users():
json_data = [{"name":"alice","age":18},{"name":"bob", "age": 22}]
return jsonify(json_data),200
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000, debug=True, use_reloader=True) Save the file (Ctrl+S).
In the same folder, create a fileDockerfile as follows.
# Use an official Python runtime as a parent image
FROM python:slim
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Install any needed packages specified in requirements.txt
RUN pip install -r requirements.txt
# Run app.py when the container launches
CMD ["python", "app.py"]
Also, define requirements.txt as follows.
flaskYour folder structure should be as follows.
Build the docker image using the docker build command.
- The option
-tspecifies the docker image's name - The dot indicates that the current directory will be used for the Docker build context (where the files and directories will be available to the Docker engine when you run docker build)
docker build . -t myappCheck that the myapp docker image is created.
docker image ls
You can now view your new docker image under the VSCode Docker Explorer.
To run the docker image, execute
docker run -p 15000:5000 myapp
The -p option is used to map the port 5000 inside (in which the flask app is listening) to the container host's port15000.
To access the application outside the container, we should use the port 15000.
- If you are using your own local machine, you can access the application at
http://localhost:15000. - If you are using the GitHub Codespaces, navigate to the URL mapped to localhost port 5000.
In the browser, navigates to the /, /about and /users endpoint to view the app.
Start two more instances of myapp container
docker run -p 15001:5000 myapp
docker run -p 15002:5000 myapp
For these two containers, the ports 15001 and 15002 are mapped to the port 5000 inside the container. You can access the application at http://localhost:15001 and http://localhost:15002 respectively.
Navigates to the /about endpoints of the ports corresponding to the containers. Observe the hostname of the containers.
In your running container in the Docker Explorer in VSCode, right-click the running containers to stop and remove the containers.
Watch the following video about the Best practices for using Docker in production.
Discuss how you may apply the best practices in your Dockerfile. Modify your Dockerfile to apply the best practices.
Apply a Docker account (if you don't have one yet).
In the terminal, login to Docker hub using your ID and password ( You may also apply an access token and use the token as password for login https://docs.docker.com/docker-hub/access-tokens/)
If you are using codespaces, you may need to first logout before you can login.
docker logout
docker login -u [Your Docker ID]Before you can push your image to the Docker hub, you need to tag your image with your Docker ID as the prefix.
docker tag myapp:latest [your Docker ID]/myapp:latest
docker push [your Docker ID]/myapp:latest
Push your image to the Docker hub.
docker push [your Docker ID]/myapp
Check that your Docker image is pushed to the Dockerhub. Example:
You can now run your app at any computer with Docker installed!
docker run -p 15000:5000 [your Dockerhub ID]/myapp
Create a new branch "task2" and commit your changes. Push the changes to the remote repository.
git checkout -b task2
git add .
git commit -m "Containerized the Python Flask app"
git push origin task2
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)