Dockerizing the Python app and running it on App Server
Shubham K. Sawant
DevSecOps @PwC | 3x AWS | Terraform Certified | GitOps Certified | Scripting | Infrastructure As a Code | Linux | CICD | Docker | Kubernetes | Ansible | Azure | AIOps
1. Create a Dockerfile
# Use any Python image as the base image
FROM python:3.8-slim
# Set the working directory
WORKDIR /app
# Copy the requirements.txt file
COPY src/requirements.txt .
# Install dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Copy the application code
COPY src/ .
# Expose port 3002
EXPOSE 3002
# Run the app
CMD ["python", "server.py"]
2. Build the Docker Image and tags it as nautilus/python-app.
sudo docker build -t nautilus/python-app .
3. Create and Run the Docker Container
sudo docker run -d --name pythonapp_nautilus -p 8097:3002 nautilus/python-app
4. Test the Application
Try it yourself code repo https://github.com/shubhamksawant/python_app.git
Reachout to me on linkedin :-https://www.dhirubhai.net/in/shubhamsawant/
Checkout my work on GitHub :-https://github.com/shubhamksawant/shubhamksawant
Checkout more Blogs on Medium - https://medium.com/@shubhamksawant
#DevOps #mariadb #docker #dockerfile #LearningJourney #shubhamksawant #DevOps #DevSecOps #AIOps #LearnWithShubham #DevOpsWithShubham #DevSecOpsWithShubham #AIOpsWithShubham #everydaylearning #what_did_i_learn_today
DevSecOps @PwC | 3x AWS | Terraform Certified | GitOps Certified | Scripting | Infrastructure As a Code | Linux | CICD | Docker | Kubernetes | Ansible | Azure | AIOps
6 个月When building Docker images on a Mac with ARM architecture (Apple Silicon), you might encounter the following error after CDK deployment when running this image on ECS (using default x86 architecture): exec /usr/bin/sh: exec format error This error occurs because the Docker image was built for ARM architecture but is being run on x86. To handle this, there are below options: a. Update your Dockerfile to include the platform specification: FROM --platform=linux/amd64 python:3.10 b. Build the Docker image with the correct platform (this option is for local testing and if pushing the image to ECR manually): docker build --platform linux/amd64 -t your-image-name .
DevSecOps @PwC | 3x AWS | Terraform Certified | GitOps Certified | Scripting | Infrastructure As a Code | Linux | CICD | Docker | Kubernetes | Ansible | Azure | AIOps
6 个月When building Docker images with Alpine Linux, to optimize Docker images, consider using the "--virtual" flag with "apk" add. This approach groups packages under a virtual name, which can be removed later, keeping your image size lean. Example: RUN apk add --virtual .build-deps package1 package2 Why Use It? -> Create Virtual Packages: The .build-deps is a virtual package that groups multiple packages together. -> Simplify Cleanup: Once you’ve built your application, you can easily remove these packages with apk del .build-deps, reducing image size and keeping only what you need. This method keeps your images small and secure by removing unnecessary dependencies after the build process. Give it a try and see the difference in your Docker image sizes!
DevSecOps @PwC | 3x AWS | Terraform Certified | GitOps Certified | Scripting | Infrastructure As a Code | Linux | CICD | Docker | Kubernetes | Ansible | Azure | AIOps
6 个月While reviewing Dockerfiles, I noticed not everyone uses the --no-install-recommends option. This flag is crucial as it prevents the installation of unnecessary packages, leading to smaller image sizes and faster build times. Example: RUN apt-get install -y --no-install-recommends <package> By adopting this practice, you’ll keep your images lean and your builds efficient.