About Me

My photo
Mumbai, Maharastra, India
He has more than 7.6 years of experience in the software development. He has spent most of the times in web/desktop application development. He has sound knowledge in various database concepts. You can reach him at viki.keshari@gmail.com https://www.linkedin.com/in/vikrammahapatra/ https://twitter.com/VikramMahapatra http://www.facebook.com/viki.keshari

Search This Blog

Friday, September 27, 2024

Docker Post-6 Step-by-step guide to create your first Docker file with a Python script

Docker makes it easy to package and deploy applications. If you're new to Docker, here's a simple guide on how to create your first Docker file, which runs a Python script (`hello.py`). This post will cover setting up the Docker file, creating a basic Python script, and handling dependencies through a `requirements.txt` file.

1. Create the Python script `hello.py`

This script is very simple and prints out a message:


# hello.py
print("Hello, World!")


You can change the message, but this one is just for demonstration.

2. Add dependencies in `requirements.txt`

Even though our script doesn’t require any external libraries, in most real-world cases, you'd need to install packages. If you want to see how to do that, include a common package like `requests` in the file. Otherwise, leave it empty.


# requirements.txt
requests


This file tells Docker which packages are required to run your Python code. It will install these during the Docker build process.

3. Write the Dockerfile

The `Dockerfile` is where you define the steps to create a container image for your application. Here’s what a simple Dockerfile would look like:


```dockerfile
# Step 1: Start with the official Python image
FROM python:3.10-slim

# Step 2: Set the working directory in the container
WORKDIR /app

# Step 3: Copy your local files into the container
COPY . /app

# Step 4: Install dependencies from the requirements.txt file
RUN pip install --no-cache-dir -r requirements.txt

# Step 5: Expose a port (not needed for this example but useful for web apps)
EXPOSE 80

# Step 6: Define the command to run the Python script
CMD ["python", "hello.py"]
```


Here’s a quick explanation of the steps:

- FROM: We use the official Python image with version 3.10 as our base.
- WORKDIR: This sets the `/app` folder inside the container as the working directory.
- COPY: This copies all the files from your current directory into the container.
- RUN: This command installs all the Python dependencies listed in `requirements.txt`.
- EXPOSE: While this isn’t necessary for simple scripts, it’s good practice to expose a port for future applications.
- CMD: This tells Docker to run `hello.py` when the container starts.

4. Build the Docker image

Now that all files are ready, you can build your Docker image. Open a terminal in the directory where you’ve saved the files and run:


```bash
docker build -t hello-python .
```


This command will build your image using the instructions in the Dockerfile.

5. Run the Docker container

After building the image, you can run the container and see your script in action:


```bash
docker run hello-python
```


This will execute the Python script inside the Docker container, and you should see:

```
Hello, WorlDirectory structure

Ensure that your files are arranged like this:

```
/your_project_directory
   ├── Dockerfile
   ├── hello.py
   └── requirements.txt
```

Conclusion

And that’s it! You've now created your first Docker container that runs a simple Python script. Docker is a powerful tool that helps package your application and its dependencies so that it runs smoothly on any system. This simple project is just the beginning — from here, you can start building more complex applications.


Post Reference: Vikram Aristocratic Elfin Share

No comments:

Post a Comment