How to Build Docker Python Images? A Step-By-Step Guide
Step 1: Create A Dockerfile
- Firstly write a dockerfile for supporting python applications and try to python application running instruction with the CMD options for the default run. The following is the sample dockerfile that we are using here:
# Use the official Python image as a base
FROM python:3.9-slim
# Set the working directory in the container
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Install any needed dependencies specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable
ENV NAME World
# Run app.py when the container launches
CMD ["python", "app.py"]
Step 2: Create requirements.txt and app.py Files
- Try on provide the requirements.txt with specifying the names of required software names and app.py file with writing the python application. The following are the sample code of those requirements.txt and app.py.
Filename: requirements.txt
Flask==2.1.0
Filename: app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=80)
Step 3: Build a Docker Image
- The following command is used for building the docker python image from the Dockerfile:
docker image build -t mypython-app:v1 .
- The following shows it practically of building of python based docker image from the Dockerfile:
Step 4: Verify the Docker Image
- Use the following command to verify the success build of docker application:
docker images
- The following screenshot shows the success build of python based docker image.
Step 5: Run a Python based Docker Container
- Run a container to that docker python image with the following command:
docker run -dit-p 80:80 --name mycontainer1 mypython-app:v1
- The following command is the successful run of that python based docker container:
Step 6: Access the Docker Container
- Now, try on access the docker container with public IP of the host system with the exposed port to see the successful run of the containerized python application. The following screenshot illustrates the successful accessing of it.
What is Docker Image?
Docker Image is an executable package of software that includes everything needed to run an application. This image informs how a container should instantiate, determining which software components will run and how. Docker Container is a virtual environment that bundles application code with all the dependencies required to run the application. The application runs quickly and reliably from one computing environment to another.
Table of Content
- What is Docker Image?
- SubCommands of Docker Image
- Docker Image Prune
- Docker Image Build
- Docker Image Tag
- Uses of Docker Images
- Difference between Docker Image VS Docker Container
- Structure Of Docker Image
- How To Create A Docker Image And Run It As Container?
- How to Build Docker Python Images? A Step-By-Step Guide
- Docker Image commands
- Docker Images – FAQs