In a previous article, we showed how to set up an OPC UA server in just minutes using Co-Pilot and Visual Code. This type of application or microservice can be interesting to isolate from the operating system and have the ability to run them independently.
This is where Docker technology comes in with its containers. But, what is Docker?
Docker is a tool that allows you to package an application with all its dependencies in a container, ensuring that it runs the same in any environment. It's like a portable box that carries your software ready to run wherever you want.
Next, let's look at the steps to have Docker available and see the specific example of Dockerizing the OPC UA Server from the previous article.
Installation Docker
The first step is to download the Docker Desktop application. With it, we will manage the images and containers on our machine, meaning we can run any application that has previously been embedded in a Docker. Docker is free for personal use.
Once installed, we will see something like this:
And now we have the application ready to load in the images we want.
Generating Docker Image and Container
This section has a bit more substance, as this is where we are going to convert the application we have in Visual Code into a Docker-compatible image.
As a tip, Docker is essentially a "mini-PC," so we will need to install the dependencies or requirements it needs, depending on the functionality it will perform.
Step 1: Create a Dockerfile in the root of the project. You will see the whale icon, which indicates that the name is correct.
The content of the Dockerfile must contain the information to build the application; I suggest asking ChatGPT for help with this. The code in this case:
# Updated base image
FROM python:3.12-slim-bookworm
# Prevent interactive problems
ENV DEBIAN_FRONTEND=noninteractive
# Working directory inside the container
WORKDIR /app
# Copy and install dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Copy the project code
COPY . .
# Expose the port that the OPC UA server or web server will use
EXPOSE 4840
EXPOSE 5000
# This CMD is ignored if you use docker-compose with "command"
CMD ["python", "opc_server.py"]
Step 2: Create docker-compose.yml to configure the services.
version: '3.9'
services:
opcua-server:
build:
context: .
container_name: opcua-server
ports:
- "4840:4840"
restart: unless-stopped
command: python opc_server.py
opcua-web:
build:
context: .
container_name: opcua-client-web
ports:
- "5000:5000"
restart: unless-stopped
command: python opc_client_web.py
Step 3: Docker ignore. Using a file named ".dockerignore," we ensure that when the application is packaged, unnecessary items like the cache or others are not included.
venv/
__pycache__/
*.pyc
.DS_Store
.git/
Step 4: Libraries. Check that requirements.txt contains all the libraries used in the project. To do this, we run the following code in the terminal, which will add all the libraries to our file.
pip freeze > requirements.txt
And after running, all the libraries from my venv environment will be added to my requirements.txt file.
Step 5: Build the Docker image and start the container. To do this, we open the terminal and run
docker-compose up --build
Once this command is executed, our application will start to be built, compiling it with all the dependencies and libraries. Additionally, it will generate a container and start it. With this instruction, we save more steps.
If everything went well, the image should now be available in Docker Desktop:
And the containers started, each on its port:
Results
Now we have the OPC UA server running, which we can check again using UA Expert:
And our Web client collecting values:
Export
If we want to export the image so it can be used on other devices. First, we execute a command to check what our images are
docker images
And we will have something like this:
Then we proceed to create the .tar files, which are the compressed versions of our images in the format that Docker accepts.
To save the images together:
docker save -o full_backup.tar opcserver-opcua-server opcserver-opcua-web
And to import it on another PC with Docker:
docker load -i full_backup.tar
Conclusions
- Docker is a very interesting technology for packaging applications and making them independent of the operating system where we want to run them.
- It has a very intuitive environment and a lot of documentation, making its implementation very easy.
- For applications like an OPC UA server, it can be very useful in testing environments, where we need to set up a service with utility without wasting much time setting it up.