Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/sir-chester-king/python-application-using-docker-volumes

This is a simple app in python using the Docker volumes to store data.
https://github.com/sir-chester-king/python-application-using-docker-volumes

docker docker-images docker-volumes dockerfile python

Last synced: 5 days ago
JSON representation

This is a simple app in python using the Docker volumes to store data.

Awesome Lists containing this project

README

        

# Python Application Using **Docker Volumes**
## Table Of Contents
* [Description](#description)
* [Main Application](#main_app)
- [Store_Data](#store_data)
- [View_Data](#view_data)
* [Dockerfile](#dockerfile)
- [Command Dockerfile](#command_file)
- [Build Docker Image](#build_image)
- [Create Docker Volume](#create_volume)
- [Run Docker Container](#run_container)
* [View Data In The Docker Volume](#view_data_volume)

---

## Description
This application allow to user to store into a file saome User's data info, such as Name, Surname, Address and Phone Number and to view the data stored.

The storing of data's are set in a file, and this file, will be stored into a [Docker Volumes](https://docs.docker.com/engine/storage/volumes) (NOT into some project's folder).

The purpose of this app is to understand how the data persist, even when the container it's restarted or removed; of course the Volume used is a Persistent Volume, not a Unknow Volume (temporary volume).

The application works via Terminal bash, not GUI.

Application is write in [Python](https://www.python.org).

The Tree of application is:
- **`Project_Pythony/`**: The root directory of the project.
- **`Main_Code/`**: Contains the main application logic.
- **`Classes/`**: Includes additional modules used by the main application.
- **`View_Users/`**: Manages user view list functionality.
- **`Store_Data/`**: Handles data storage operations.
- **`Create_Users/`**: Manages user creation functionality.
- **`Dockerfile`**: Defines the Docker container setup for the project.
- **`README.md`**: Documentation for the project.


---

## Main Application
The application in the main page, show to user a menu list to create a new user, or view the list of all users.

The input is via Terminal command.
```
menu_app = {
"1": "Create new user",
"2": "View list users"
}
```
As you can see, it was used a dictionary to use the pair Key: Values to bind the option with the action.

It be used a match statesman to call the proper function based on user's choice:
```
# Call the property function based on the user's chosen option.
match option_chosen:
case "1":
Create_Users.Create_users.new_user()
case "2":
View_Users.View_users.list_users_volume()
case _:
return 0
```


### Store_Data
This function is structured for storing the data of new users into the file in the Docker Volume Directory.

Initally it created the path of Docker Volume where the data will be stored (the path is harded code inside the code).
```
# This is the PATH inside the Docker Container Volume
path_volume_docker = "/Docker_Directory/Storage/User_Data.txt"

# Check if the directory inside the volume exist or not.
# In case it doesn't exist, it is created.
directory = os.path.dirname(path_volume_docker)
if not os.path.exists(directory):
os.makedirs(directory)
print(f"Created directory: {directory}")
```
The " /Docker_Directory/Storage/User_Data.txt< /ins>" will be the path inside the Docker Volume where the file "User_Data.txt" will store data.


### View_Data
This function is used to view all the users are stored inside the Docker Volume file (/Docker_Directory/Storage/User_Data.txt).

It be defined the path of Docker Volume where the data has been stored (the path is harded code inside the code).

```
# This is the PATH inside the Docker Container Volume
path_volume_docker = "/Docker_Directory/Storage/User_Data.txt"

# Check if the directory inside the volume exist or not.
directory = os.path.dirname(path_volume_docker)
if not os.path.exists(directory):
print(f"The directory {directory} was not found")
```

---

## Dockerfile
This file contain all commands used to build the Image that Containers will use.

The Image is a snapshot of the source code, and when it did build, the Image is in read-only mode, and you cannot change the code. If you want to create a container based to the new image, you must re-build the image.

--

### Command Dockerfile
The commands used to build the image that it'll be used to create the container that has the code, you must declare some parameters.

In this image it used the following commands:
- FROM
- LABEL
- WORKDIR
- COPY
- ENV
- RUN
- CMD

The FROM command it used to pull all dependenties based on the image that we pass as a parameter.

In this case, we defined an image for a Python application, therefore with this command, we pull oll the dependenties from the official [Python Image](https://hub.docker.com/_/python), stored in the [Docker Hub](https://hub.docker.com).
```
FROM python:latest
```
The word " latest " define to use the latest versione of the image we want to pull.

The WORKDIR command it used to define our work directory that all the next following command in the Dockerfile will be executed.

```
WORKDIR /Docker_Directory
```

The COPY command it used to say to Docker, that it must copy all the file stored in the same directory of Dockerfile, to some directory in the container (that we pecified).
```
COPY . .
```

The ENV command it used to set the wanted variable to be include the wanted directory.
```
# Set the PYTHONPATH to include the "Docker_Directory" directory
ENV PYTHONPATH "${PYTHONPATH}:/Docker_Directory"
```

The RUN command it used to run a specific command in the Container filesystem.
```
# Ensure the storage directory exists
RUN mkdir -p /Docker_Directory/Storage
```

The CMD command it used to say to Docker to run the command we specified in the dockerfile.
```
CMD ["python", "./Main_Code/main.py"]
```

--

### Build Docker Image
To build image, you must use the BUILD command, and pass where the dockerfile is stored, as a parameter.

It be the result.

```
# If you ware in the same directory (as path) of where Dockerfile is stored, you can pass it as " . " argument.
docker build -t python_app_image:1 .
```
![Alt text](Readme_Screen/State_build.png)


To view the image was builted, you can view with the following command:
```
docker image ls
```

or via Docker Desktop app:
![Alt text](Readme_Screen/Docker_Hub_Image.png)

--

### Create The Docker Volume
After you successfully build the Image, you can create the Docker Volume that it will be used to store data.

To create the Persisten Volume, you must use the following command:
```
docker volume Volume_Python_App
```

To view the Volume created, you can see it with the:
```
docker volume ls
```
![Alt text](Readme_Screen/List_volume_terminal.png)

or via Docker Desktop app:
![Alt text](Readme_Screen/List_volume_docker_hub.png)

--

### Run Docker Container
After you successfully build the Image and created the Docker Volume, you can create and run the Container will contain the python app.

To crate the container, you must use the following command:
```
docker run --name container_python_app -ti --rm -v Volume_Python_App:/Docker_Directory/Storage python_app_image:1
```
- --name: specify the Container name.
- -ti: specify the Container will be in interactive mode.
- -rm; specify that the Container will be automatically removed whene the execution of the application will terminate.
- -v Volume_Python_App:/Docker_Directory/Storage: specify to mount the Volume with name Volume_Python_App, in the /Docker_Directory/Storage (this path is used to store data).
- python_app_image:1: specify the name of Image that will be used to create the Container.

When you create the container, the app start immediatly, 'cause, in the Dockerfile we declared a CMD command the run the "main.py" file.

![Alt text](Readme_Screen/Start_app.png)
If you wanna see the list of container created, you must use the following command:
```
docker ps
```
If you wanna see the list of container that no longer used, for example, such as it was terminated 'cause the app in the container finished the work.

You must use the following command:
```
docker ps -a
```

---

## View Data In The Docker Volume
To view the data after it be stored in the Volume, you can view with 2 mode:
- Using a function of the application (if specified)
- Using Docker Desktop app

With the function of app:
![Alt text](Readme_Screen/Option_view_Data.png)
![Alt text](Readme_Screen/View_Data_Volume_Func_App.png)

With the Docker Desktop app:
![Alt text](Readme_Screen/Volume_Data_Docker_Hub.png)
![Alt text](Readme_Screen/View_Data_Volume_Docker_Hub.png)

---
## Author
- Nicola Ricciardi