Dockerizing the Python app and running it on App Server

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"]        

  • FROM python:3.8-slim: This specifies a lightweight Python 3.8 image.
  • WORKDIR /app: Sets the working directory inside the container.

  • COPY src/requirements.txt .: Copies the requirements.txt file into the container.
  • RUN pip install --no-cache-dir -r requirements.txt: Installs the dependencies listed in requirements.txt.
  • COPY src/ . : Copies the app source code from the src directory.
  • EXPOSE 3002: Exposes port 3002.
  • CMD ["python", "server.py"]: Runs the Python script server.py when the container starts.

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

  • Create and Run a Container Named pythonapp_nautilus:

  sudo docker run -d --name pythonapp_nautilus -p 8097:3002 nautilus/python-app        

  • -d: Runs the container in detached mode.
  • --name pythonapp_nautilus: Names the container pythonapp_nautilus.
  • -p 8097:3002: Maps port 8097 on the host to port 3002 in the container.

4. Test the Application

curl https://localhost:8097/

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


Shubham K. Sawant

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 .

回复
Shubham K. Sawant

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!

回复
Shubham K. Sawant

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.

回复

要查看或添加评论,请登录

Shubham K. Sawant的更多文章

社区洞察

其他会员也浏览了