To build your own Docker image, follow these steps:
Create a Directory: Start by creating a new directory for your project.
mkdir my-app cd my-appCreate a Dockerfile: In this directory, create a file named
Dockerfilewith the necessary instructions. Here’s a simple example:# Use a base image FROM python:3.7-slim # Set the working directory WORKDIR /app # Copy the current directory contents into the container COPY . /app # Install required packages RUN pip install --no-cache-dir -r requirements.txt # Set environment variables ENV PORT=80 # Expose the port EXPOSE 80 # Command to run the application CMD ["python", "app.py"]Create Required Files: Create a
requirements.txtfile for dependencies and anapp.pyfile for your application logic. For example:requirements.txt:flaskapp.py:from flask import Flask import os app = Flask(__name__) @app.route("/") def hello(): return "Hello, World!" if __name__ == "__main__": app.run(debug=False, host='0.0.0.0', port=int(os.environ.get('PORT', 80)))
Build the Docker Image: Run the following command in the terminal to build your Docker image:
docker build -t my-app .Run the Docker Container: After building the image, you can run it with:
docker run -p 8888:80 my-app
This will start your application, and you can access it at http://localhost:8888.
Feel free to explore more about Docker and its features through LabEx labs!
