Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create the necessary Docker assets to containerize app #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Include any files or directories that you don't want to be copied to your
# container here (e.g., local build artifacts, temporary files, etc.).
#
# For more help, visit the .dockerignore file reference guide at
# https://docs.docker.com/go/build-context-dockerignore/

**/.DS_Store
**/__pycache__
**/.venv
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/bin
**/charts
**/docker-compose*
**/compose.y*ml
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md
35 changes: 35 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
ARG PYTHON_VERSION=3.11.4
FROM python:${PYTHON_VERSION}-slim AS base

# Prevents Python from writing pyc files.
ENV PYTHONDONTWRITEBYTECODE=1

# Keeps Python from buffering stdout and stderr to avoid situations where
# the application crashes without emitting any logs due to buffering.
ENV PYTHONUNBUFFERED=1

WORKDIR /app

# Create a non-privileged user that the app will run under.
# See https://docs.docker.com/go/dockerfile-user-best-practices/
ARG UID=10001
RUN adduser \
--disabled-password \
--gecos "" \
--home "/nonexistent" \
--shell "/sbin/nologin" \
--no-create-home \
--uid "${UID}" \
appuser

# Switch to the non-privileged user to run the application.
USER appuser

# Copy the source code into the container.
COPY . .

# Expose the port that the application listens on.
EXPOSE 5020

# Run the application.
CMD python3 server.py --host 0.0.0.0 --port 5020
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,28 @@ Make sure the Python is version 3.7 or newer.

The default listening port is **5020**.

## Execute in container

Make sure that You have installed the latest version of [Docker Desktop](https://docs.docker.com/get-started/get-docker/).

Run the following command in a terminal.

```sh
docker compose up --build
```

You can run the application detached from the terminal by adding the -d option.

```sh
docker compose up --build -d
```

Run the following command to stop the application.

```sh
docker compose down
```

## Impelemented Functions

1. Read Coils
Expand Down
6 changes: 6 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
services:
server:
build:
context: .
ports:
- 5020:5020
7 changes: 6 additions & 1 deletion server.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from time import time
import asyncio, socket
import ssl
import argparse

from modbus_exception import modbus_exception
from modbus_functions import func_dict
Expand Down Expand Up @@ -246,7 +247,11 @@ def run(self):


if __name__ == '__main__':
server = modbus_server()
parser = argparse.ArgumentParser()
parser.add_argument("--host", help="Specifies the TCP/IP address on which the server is to listen for connections from clients. The default value is 127.0.0.1", default="127.0.0.1")
parser.add_argument("--port", type=int, help="The TCP port the server listens on; 5020 by default.", default=5020)
args = parser.parse_args()
server = modbus_server(args.host, args.port)
print("MODBUS server listens on {}:{}".format(server.host, server.port))
server.run()
print("MODBUS server closed")