This blog talks about hosting cars-api Flask app using Docker. Docker is a set of platform as a service to deliver software as packages, called Docker containers. These are the standalone, lightweight, executable packages that could include necessary packages to run an application. Docker Images would turn to containers when they run on the Docker Engine.
In the below steps, we are showing how to host the Flask app and launch the application with the help of Docker containers.
1. Creating the Docker image
2. Run the Docker image
3. Launch the app hosted in Docker
1. Creating the Docker Image :
This section describes how to build the docker image.
a)Create the Docker file from scratch and then build the Docker Image.
Create a Dockerfile in your working directory and add the necessary line of codes as required for your project.
Dockerfile may look like this :
#Dockerfile to build an image/container to host cars-api #Pull python Image FROM python LABEL maintainer = "Qxf2 Services" #Clone cars-api repository for Docker Image creation RUN git clone https://github.com/qxf2/cars-api.git #Set working directory WORKDIR /cars-api #Install packages listed in requirements.txt file RUN pip install -r requirements.txt #Make port 5000 available to the container EXPOSE 5000 #Execute command ENTRYPOINT [ "python" ] CMD [ "cars_app.py" ]
Build the Docker Image. Make sure you are in the root directory of the project and run the following command.
docker build --tag cars-api-docker .
You can continue with section 2 if you build a docker image now.
b)Build the docker image from the GitHub followed by the branch. Provide the name that you would like to give as the tag.
docker build --tag cars-api-docker https://github.com/qxf2/cars-api.git#master
Note: You should have the Dockerfile in your GitHub repository to create the Image.
2. Run the Docker Image into containers:
Now run the container from the Docker image that you just created. There are 2 ways you can run the Docker container.
a) Foreground mode will be helpful to check if any errors while running as the output would be displayed.
docker run -p 5000:5000 cars-api-docker
You can continue with section 3b, launch the app in the browser. If you want to try out in the terminal, open another terminal and follow the steps in section 3a.
b) In detached mode, the Docker container would be running in the background of your terminal, and inputs/outputs will not be displayed.
docker run -d -p 5000:5000 cars-api-docker
Note: This would help in porting the host( your laptop) to the containers. This would be helpful in accessing the container from the external environment.
You can list the running containers using the command below. Kindly note the container id for your reference.
3. Launch the app hosted in Docker container
We can launch the Flask app in 2 ways
a) Launch the app inside the container :
This is the command to execute an interactive bash on the container
docker exec -it [container-id] /bin/bash
You would get the root user as in the terminal then verify the Flask app.
This would display the HTML content of the index page. Kindly refer to the screenshot of the below code. You can see the list of cars.
curl -u qxf2:qxf2 http://127.0.0.1:5000/cars
b) Launch the app in the host machine by launching the URL in the browser.
You can stop the container after all the tests.
docker stop [container-id]
Enhancements: You can create a GitHub action that would build the Docker image and run the container after the code change. For each and every push/pull request the new build would be created and pushed into the Docker Image.
I started my career in IIIT Bangalore incubated startup company as a software engineer and soon started working on digital and e-learning solutions as QA. Later moved to Oracle as User Assistance QA Engineer and gained multiple Oracle cloud products and testing skills. Oracle gave opportunities with products, courses, and environments to explore and enhance testing skills. Now joined Qxf2, to explore in testing perspective. I believe Quality means doing it right, even when no one is looking. A number of bugs are not evaluating the product, the quality of the bug also matters. I love cooking, reading, and DIY crafts.