[go: up one dir, main page]

Skip to content

Example of unrolling a server in a Docker container on Yandex Cloud#

In this example, the server logs the received messages and displays them in the virtual machine's console.

Before you start:

  1. Create an account in Yandex
  2. Generate an SSH key

Options for running the Python webhook server:


1. Creating an account in Yandex#

  • You need to create a Yandex account on the Yandex website.
  • Log in to Yandex cloud using the account you created.

You will be offered to create your first cloud. Agree and create it.

  • To work with Yandex services, you need to create a payment account.

It is necessary to link the card, a small amount will be charged and refunded.

Attention! When registering, you must indicate a card from the country in which you reside.

You can read more details here.

  • After registration, your account should have an Active status.

More details about the trial period and grants can be found here.


2. Generating an SSH key#

For creating a key, you can use PuTTY or OpenSSH programs, we will use Yandex's documentation.

Example for Linux/MacOS:

  • Run the following command in your terminal

    ssh-keygen -t ed25519
    

  • Specify the path to the folder where you want to save the key and the key's name.

    For our example: /Users/GreenAPI/ssh/yc_rsa

  • enter a passphrase (actually a password) to generate the key.

  • two files will be created in the folder:

yc_rsa.pub - the public part of the key

yc_rsa - the private part of the key


Creating a server using Yandex and a docker-compose file#

  1. Create a virtual machine

    All servers -> Infrastructure and Network -> Compute Cloud

  2. Configure the virtual machine

    Specify the name, description, availability zone.

    • In the section Select a boot disk image choose Container Solution -> Configure -> Docker-compose, you will need to paste the code from the file docker-compose.yml

      Next, the power of your virtual machine is configured, the values are selected depending on your needs, but for an example of server operation, minimum settings are sufficient.

      Attention! Field value interrupted means that the server's address will change when the machine is restarted.

    • Create a service account

      To manage the machine from the command line, the editor role is required.

    • Specify the login for the virtual machine user.

    • Open the file yc_rsa.pub and copy the contents into the SSH key field.

  3. Access to the virtual machine

    • To access the virtual machine, enter the following command in your computer's terminal

      ssh [-i identity_file] [user@]host[:port]

      identity_file - the path to the private part of the SSH key

      user - the username (login specified in the settings)

      host - the external address of the host is located in the properties of your virtual machine

      For our example, the command will look like this:

      ssh -i /Users/GreenAPI/ssh/yc_rsa green-api@84.252.137.226

    • After entering the command, you may see the message "The authenticity of the host cannot be established, continue" (yes). Enter the password (the passphrase you used when creating the SSH key), and the command line will change to "green-api@webhook-server:~$" (indicating that you have logged into the virtual machine).

  4. Running the container

    • When using this method, the virtual machine will automatically download all the necessary files and run the container.

      To view webhooks, you need to configure your instance in the console Green-Api office to work with the server, specifying the external IP address of the machine and the port.

      Notification Sending Address (URL) | http://158.160.6.161:8080 (example)

  5. Viewing received messages

    • View running containers

      docker ps
      

    • View the logs of the running container

      docker logs [OPTIONS] CONTAINER - show the container log

      CONTAINER - id or name of the running container

      OPTIONS - -f, --follow - continuous log output option

      (Ctrl + C - exit mode)

      For our example, the command will look like this:

      docker logs --follow webhookserver


Creating a server using Docker#

  1. Create a virtual machine

    All servers -> Infrastructure and Network -> Compute Cloud

  2. Configure the virtual machine

    Specify a name, description, availability zone.

    • In the Select a boot disk image section, choose Operating Systems -> Ubuntu

      In our example, we will run the virtual machine using the OS Ubuntu.

    • In the Disks and File Storage section, specify a disk size of 10 GB.

      Next, the power of your virtual machine is configured, the values are selected depending on your needs, but for an example of server operation, minimum settings are sufficient.

      Attention! The field value preemptible means that the server's address will change when the machine is restarted.

    • Create a service account

      To manage the machine from the command line, the editor role is required.

    • Specify the login for the virtual machine user.

    • Open the file yc_rsa.pub and copy its contents into the SSH key field.

  3. Access to the virtual machine

    • To access the virtual machine, enter the following command in your computer's terminal

      ssh [-i identity_file] [user@]host[:port]

      identity_file - the path to the private part of the SSH key

      user - the username (login specified in the settings)

      host - the external address of the host can be found in the properties of your virtual machine

      For our example, the command will look like this:

      ssh -i /Users/GreenAPI/ssh/yc_rsa green-api@84.252.137.226

    • After entering the command, you may see the message "The authenticity of the host cannot be established, continue" (yes), enter the password (the passphrase you used when creating the SSH key), and the command line will change to green-api@webhook-server:~$ (indicating that you have logged into the virtual machine).

  4. Running the container

    • When using this method, the virtual machine will not automatically download and install docker, you need to do this manually.

      To view notifications, you need to configure your instance in the Green-Api console to work with the server, specifying the external IP address of the machine and the port.

      Notification Sending Address (URL) | http://158.160.6.161:8080 (example)

    • Enter the following commands in the virtual machine terminal:

      sudo apt-get update
      
      sudo apt-get install \
          ca-certificates \
          curl \
          gnupg \
          lsb-release
      
      curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
      
      echo \
      "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
      $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
      
      sudo apt-get update
      
      sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin 
      

    • You need to log in to Docker using your account.

      To get the image from the Docker Hub website you need to have an account.

      sudo docker login 
      
      sudo docker pull greenapi/whatsapp-api-webhook-server-python
      

    • Launch the container in the background:

      sudo docker run --publish 8080:80 -itd greenapi/whatsapp-api-webhook-server-python
      

  5. Viewing received messages

    • Viewing running containers
      sudo docker ps
      
    • Viewing the logs of a running container

      sudo docker logs [OPTIONS] CONTAINER - show container logs

      CONTAINER - id or name of the running container

      OPTIONS - -f, --follow - continuous log output option

      (Ctrl + C - exit mode)

      For our example, the command will look like this:

      sudo docker logs --follow 29a6012fb262


Creating a server using a docker-compose file#

  1. Create a virtual machine

    All servers -> Infrastructure and Network -> Compute Cloud

  2. Configure the virtual machine

    Specify a name, description, availability zone.

    • In the Select a boot disk image section, choose Operating Systems -> Ubuntu

      In our example, we will run the virtual machine using the OS Ubuntu

    • In the Disks and File Storage section, specify a disk size of 10 GB.

      Next, the power of your virtual machine is configured, the values are selected depending on your needs, but for an example of server operation, minimum settings are sufficient.

      Attention! The field value preemptible means that the server's address will change when the machine is restarted.

    • Create a service account

      To manage the machine from the command line, the editor role is required.

    • Specify the login for the virtual machine user.

    • Open the file yc_rsa.pub and copy its contents into the SSH key field.

  3. Access to the virtual machine

    • To access the virtual machine, enter the following command in your computer's terminal

      ssh [-i identity_file] [user@]host[:port]

      identity_file - the path to the private part of the SSH key

      user - the username (login specified in the settings)

      host - the external address of the host can be found in the properties of your virtual machine

      For our example, the command will look like this:

      ssh -i /Users/GreenAPI/ssh/yc_rsa green-api@84.252.137.226

    • After entering the command, you may see the message "The authenticity of the host cannot be established, continue" (yes), enter the password (the passphrase you used when creating the SSH key), and the command line will change to green-api@webhook-server:~$ (indicating that you have logged into the virtual machine).

  4. Running the container

    • When using this method, the virtual machine will not automatically download and install docker, you need to do this manually.

      To view notifications, you need to configure your instance in the Green-Api console to work with the server, specifying the external IP address of the machine and the port.

      Notification Sending Address (URL) | http://158.160.6.161:8080 (example)

    • Enter the following commands in the virtual machine terminal:

      sudo apt-get update
      
      sudo apt-get install \
          ca-certificates \
          curl \
          gnupg \
          lsb-release
      
      curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
      
      echo \
      "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
      $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
      
      sudo apt-get update
      
      sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin 
      
      sudo apt install docker-compose
      

    • Obtaining the docker-compose File:

      wget https://raw.githubusercontent.com/green-api/whatsapp-api-webhook-server-python/master/docker-compose.yml
      
      sudo docker-compose build
      

    • Running the container in the background:

      sudo docker-compose up -d --force-recreate
      

  5. Viewing received messages

    • View running containers
      sudo docker ps
      
    • Viewing the logs of a running container

      sudo docker logs [OPTIONS] CONTAINER - show container logs

      CONTAINER - id or name of the running container

      OPTIONS - -f, --follow - continuous log output option

      (Ctrl + C - exit mode)

      For our example, the command will look like this:

      sudo docker logs --follow 29a6012fb262