Infrastructure
Hands on Practice

Practice 1: Building a lencx Docker Image Objective: Learn how to build a custom Docker image using a dockerfile and dependencies.

Steps:

1. Prepare the Workspace
Navigate to the directory where you will create the necessary files and resources for building the Docker image.

mkdir -p /home/user/lens-docker/images/custom/ci /home/user/lens-docker/images/custom/resources  
cd /home/user/lens-docker/images/custom  

2. Create the Containerfile (name of the dockerfile)
Create the Containerfile in the /home/user/lens-docker/images/custom directory.

touch Containerfile  

Add the content of the Containerfile from the following link:
Containerfile Content

3. Create the apps.json File
Create the apps.json file in the /home/user/lens-docker/images/custom/ci directory.

echo '[  
{  
'url': '[https://github.com/frappe/erpnext.git](https://github.com/frappe/erpnext.git)',  
'branch': 'v14.44.0'  
},  
{  
'url': '[https://github.com/frappe/payments.git](https://github.com/frappe/payments.git)',  
'branch': 'version-14'  
}  
]' > /home/user/lens-docker/images/custom/ci/apps.json  

4.Create the Nginx Resources
Navigate to the resources directory and create the Nginx configuration files.

cd /home/user/lens-docker/images/custom/resources  

5. Verify the Directory Structure
Ensure that the directory structure is correct.

tree /home/user/lens-docker/images/custom  

Expected output: ```plaintext
/home/user/lens-docker/images/custom
├── Containerfile
├── ci
│ └── apps.json
└── resources
├── nginx-entrypoint.sh
└── nginx-template.conf

 **6.Prepare `apps.json` as Base64** 
Convert the `apps.json` file into a Base64 string for use in the Docker build process. 
```bash  
APPS_JSON_BASE64=$(base64 -w 0 ./ci/apps.json)  

7. Build the Docker Image
Use the prepared Base64 string and build the Docker image with specific build arguments.

docker build --no-cache -t 'lenscx:v14.51.0' \  
--build-arg FRAPPE_PATH='[https://github.com/frappe/frappe](https://github.com/frappe/frappe)' \  
--build-arg FRAPPE_BRANCH='v14.51.0' \  
--build-arg PYTHON_VERSION='3.10.12' \  
--build-arg NODE_VERSION='16.20.1' \  
--build-arg APPS_JSON_BASE64='${APPS_JSON_BASE64}' \  
-f '/home/user/lens-docker/images/custom/Containerfile' \  
/home/user/lens-docker/images/custom  

Explanation:

  • The docker build command uses various build arguments to customize the image.
  • The APPS_JSON_BASE64 argument injects the apps configuration into the image.
  • The image is tagged as lenscx:v14.51.0.

8. Verify the Build
Once the build completes, verify the image creation.

docker images | grep lenscx  

Expected Outcome: - A Docker image named lenscx:v14.51.0 should be created.

Have a doubt?
Post it here, our mentors will help you out.