Tuesday 5 December 2023

Edit, add, or remove ports of an existing Docker container

In some cases you need to assign additional ports to a running Docker container, or change the ports in use. This is completely possible without creating a new docker image or re-running docker run to create a new container. To change the ports of a running Docker Container, follow the instructions below.

Example: we have a container of image php:8.1-apache configured with port mapping 8080->80/tcp. We will edit this container to open port 8081->443/tcp.

Warning! You should back up before editing any files

Step 1: Go to your container configuration directory

Go to the directory where the containers are stored

cd /var/lib/docker/containers

Here you will see folders with names corresponding to container ids. Then access the directory of the corresponding container you need to edit.

The full path will be as follows

/var/lib/docker/containers/your_container_id_hash

You can find the container's hash id via the command

docker ps -a

Step 2: Stop docker service (docker.socket)

systemctl stop docker.socket

Step 3: Edit expose ports config in file config.v2.json

We will declare port 443 on the container

Find json string 

"ExposedPorts":{"80/tcp":{}}

Then add new port

"ExposedPorts":{"80/tcp":{},"443/tcp":{}}

Step 4: Edit ports mapping config in file hostconfig.json

We will map port 8081 on the host computer to the newly created port 443/tcp on the container

Find json string 

"PortBindings":{"80/tcp":[{"HostIp":"","HostPort":"8080"}]}

Then add new port

"PortBindings":{"443/tcp":[{"HostIp":"","HostPort":"8081"}],"80/tcp":[{"HostIp":"","HostPort":"8080"}]}

Step 5: Restart the docker service

systemctl start docker

Done !

Sunday 12 February 2023

PHP - Access ChatGPT API using cURL

To access OpenAI's ChatGPT API using PHP, you can use any HTTP client library that supports making HTTP requests with JSON payloads, such as Guzzle or cURL. You may find it a bit ridiculous, because ChatGPT hasn't officially announced the API yet. But the fact that OpenAI already has APIs available to provide full task processing like ChatGPT. This REST API can answer your questions or handle your requests with the same results as ChatGPT.

How GPT Chat Works

As a general-purpose language model, ChatGPT is likely to use a combination of OpenAI's available models and techniques to generate its responses, depending on the context and nature of the question. Additionally, OpenAI may update or modify its models over time, so the specific models used by ChatGPT may change as well. In other words, if you use the OpenAI API with the right Model, you can get the same results as ChatGPT. Of course ChatGPT will have more features and especially the ability to link topics with your messages.

How to access OpenAI ChatGPT API using Curl

You can study the documents and examples at the following links:

OpenAI Docs: https://platform.openai.com/docs/introduction
Models: https://platform.openai.com/docs/models/gpt-3
Features and examples: https://platform.openai.com/examples
Get API Key: https://platform.openai.com/account/api-keys

ChatGPT API Sample:

<?php
$data = array(
  "prompt" => "Is Climate Change worrisome?", //Your question or request
  "temperature" => 0.5,
  "max_tokens" => 500
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/engines/davinci-codex/completions');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: Bearer YOUR_API_KEY';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$response = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);
echo $response;
/**
 * Output Response :
 * Yes, climate change is dangerous and has significant consequences
 * for the planet and its inhabitants. The warming of the Earth's climate
 * due to increased greenhouse gas emissions, primarily from human
 * activities such as burning fossil fuels, is causing more frequent
 * and severe natural disasters, such as floods, heatwaves, wildfires, and storms.
 * */

Note:

- Unlike ChatGPT, OpenAI API is not free but charges based on the number of input and output characters each of your APIs

- A programmer can use OpenAI's API to integrate various natural language processing capabilities into their applications, such as text generation, question answering, language translation, sentiment analysis, and more. With the API, You can leverage the power of OpenAI's advanced machine learning models and algorithms without having to build them from scratch. However, note that OpenAI has some usage restrictions for their API, so make sure to review their documentation before integrating it into your project.


Friday 10 February 2023

How to fix error "system is deadlocked on memory" on Vultr VPS

The error "system is deadlocked on memory" "end Kernel panic - not syncing" usually indicates that the system has run out of available memory and processes are unable to allocate additional memory. This error comes from the VPS kernel, so it can be encountered in both Linux or Windows VPS operating systems. To resolve the issue on your Vultr VPS or any other service provider (Linode, Digitalocean ...) you can try the following steps:

Note: The first thing you need to do when you encounter this error is to quickly backup or snapshot VPS. Because the wrong operations at this time can cause you to lose the data in your VPS. 

Case 1 : Error on startup (can not start VPS)

  1. Restart the server: If the issue persists, you can restart your Vultr VPS to clear the memory and resolve the deadlock.

  2. Turn off the startup scripts with VPS (in VPS management) that you added (if exist)

  3. Upgrading to a VPS droplet with more memory: If other solutions don't work, upgrading to a more configurable VPS plan might solve the situation. Upgrading VPS to a higher droplet at Vultr is easy, safe and does not affect your data or applications.

  4. Contact Vultr for support: The last resort or if you are too worried about the current data in the VPS. Experts from Vultr will be able to help you fix the problem

Case 2: Error during operation (sometimes encounter)

  1. Monitor resource usage: Use the 'top',' htop' command or "Task Manager' to monitor the resource usage of your system. Identify the processes that are consuming large amounts of memory and determine if they can be terminated or if their resource usage can be optimized.

  2. Kill processes: If a specific process is consuming a large amount of memory and is not responding, terminate it.

  3. Increase swap space(Linux): If your system does not have enough physical memory, you can increase the amount of swap space to temporarily address the issue.

  4. Optimize application performance: If the issue recurs frequently, you may need to optimize the performance of your applications

It's important to monitor your system and its resource usage to identify and resolve issues before they become severe. Consider using a system monitoring tool to automatically alert you to any issues.