This blog is created for the purpose of understanding and deploying applications using Podman in IBM Code Engine via CLI.
Introduction
IBM Code Engine is a fully managed serverless platform that enables developers to build, deploy, and scale containerized applications with ease. By combining the flexibility of containers with a simplified user interface and CLI tools, Code Engine allows for seamless deployment of web-based applications. In this blog, we’ll explore how to deploy a web application to IBM Code Engine using Podman via the Command Line Interface (CLI).
Prerequisites
Before you begin, ensure the following requirements are met:
-
IBM Cloud Account: Sign up for an IBM Cloud account if you don’t already have one.
-
IBM Cloud CLI: Install the IBM Cloud CLI on your machine.
-
Code Engine Plugin: Add the IBM Code Engine CLI plugin using the command:
ibmcloud plugin install code-engine
-
Podman: Install Podman for building and managing container images. You can download it from Podman’s official website.
-
Podmanfile: Ensure your web application has a Podmanfile to define its container build process.
-
Source Code and Dependencies: Have the source code of your web application (e.g., .py
file) and a dependencies file (e.g., .txt
).
Note: The source code can be in any programming language. For this guide, Here we are using Python code.
Step-by-Step Guide
1. Start Podman
Ensure Podman is installed and start it:
podman machine start
This command initializes the Podman machine.
2. Install Required Plugins for IBM Cloud
Install all necessary plugins:
ibmcloud plugin install -a
3. Update IBM Cloud CLI Plugins
Ensure your plugins are up to date:
ibmcloud update
4. Log in to IBM Cloud
Use Single Sign-On (SSO) to log in:
ibmcloud login --sso
Authenticate with your IBM Cloud account and select the desired account.
5. Log in to IBM Cloud Container Registry (CR)
Log in to the IBM Container Registry:
ibmcloud cr login
6. Generate an API Key for Podman Authentication
Generate an API key for Podman to authenticate with IBM Container Registry:
ibmcloud iam api-key-create PodmanAPIKey -d "API key for Podman login" --file ~/podman_api_key.json
This saves the API key to a file named podman_api_key.json
. Keep this file secure.
Fetch the API key from the JSON file: (or)The Json file will be in the main directory (For ex: Users/xxxx/podman_api_key.json)
cat ~/podman_api_key.json
7. Authenticate Podman with IBM Container Registry
Log in to the IBM Container Registry using the API key:
podman login us.icr.io -u iamapikey -p <APIKey>
8. Create Your Application
Prepare the following files for your application:
-
Source Code: Example app.py
(Python-based web app):
- The code is written in pyton with the simple application to print Hello world. Normally the source cod contains the logic of the application.
- We can use the "vi editor" to create these files. Use command vi filename, a text editor opensup press 'i' in the keyboard to enter the contents. To save and quit use <esc+:+wq!>.
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello, World!"
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8080)
-
Dependencies: Example dep.txt
:
- It refer to external software libraries, packages, or components that a program or application requires to function properly.
- These dependencies provide the necessary functionality without requiring you to write the code from scratch.
For ex: - an application might depend on a specific version of a library for database access, web frameworks, or utilities like logging and error handling.
- Here we have used Flask-python based web framework
flask
-
Podmanfile: Defines the build process for your container:
- This is the text file that contains instructions to build the container file, it defines how the app and dependencies are setup.
FROM python:3.9-slim
WORKDIR /app
COPY dep.txt .
RUN pip install -r dep.txt
COPY app.py .
CMD ["python", "app.py"]
Note: Keep all files in a single folder, e.g., SimpleApp
.
9. Build the Container Image with Podman
Navigate to the application directory and build the image:
podman build -t <filename> -f <podmanfile> .
This command builds the container image and assigns it a name (simpleapp
).
Now we will be getting the container ID.
10. Run the Container using built image
Run the container locally to verify functionality:
podman run -d -p 8080:8080 <container name>
Now we will be getting image ID.
11. To verify that the container is running:
podman ps
12. Test the Application
Access the application in your web browser: http://localhost:8080
Additional Tips
- To Verify Container Images: Use
podman images
- To Stop a Running Container: Use
podman stop <container/image ID>
- To Remove a Container Image: Use
podman rm <container/image ID>
- To Stop Podman Machine: Use
podman machine stop
Conclusion
Deploying a web-based application to IBM Code Engine using Podman via CLI is a straightforward process. With the serverless capabilities of Code Engine and the flexibility of Podman, developers can focus on building robust applications without worrying about infrastructure management. Try deploying your application today and experience the power of IBM Code Engine!