Updated on 2023-02-27 GMT+08:00

Configuring and Using KooCLI in Docker

Configure and use KooCLI in Docker by performing the following steps. This procedure uses a Docker container running Ubuntu Linux as an example.

Before performing the following steps, ensure that you have installed Docker. For details about how to install Docker, visit the Docker website. To check whether Docker has been installed, run the following command:

docker --version

  1. Create a Dockerfile.

    Create a directory and create a text file named Dockerfile in the directory. The file content is as follows:

    FROM ubuntu:latest
    RUN apt-get update -y && apt-get install curl -y
    # Install KooCLI with one click.
    RUN curl -sSL https://eu-west-101-apiexplorer-cli.obs.eu-west-101.myhuaweicloud.eu/cli/latest/hcloud_install.sh -o ./hcloud_install.sh && bash ./hcloud_install.sh -y
    WORKDIR hcloud

    The name of the Dockerfile contains an uppercase letter D and does not have an extension. Only one Docker file can be saved in each directory.

    Add the following content to the preceding Dockerfile to specify KooCLI as the program to run when the container is started:

    ENTRYPOINT ["/usr/local/bin/hcloud"]

    The container started by the Docker image built using this file (see Method 2) supports only a single KooCLI command.

  2. Build an image.

    Run the following command in the directory to create a Docker image named hcloudcli:

    docker build --no-cache -t hcloudcli .

    ...

    The period (.) at the end of the command cannot be omitted. It indicates that the Docker image is built in the current directory.

    After you add ENTRYPOINT ["/usr/local/bin/hcloud"] to the Dockerfile, the following information is displayed when you build an image:

    After the image is built successfully, run the following command to view the image:

    docker images

  3. Use the image.

    • Method 1: Create a background container using the hcloudcli image and run commands in the container.
      docker run -it -d --name hcloudcli hcloudcli

      Run the following command to view the started Docker container:

      docker ps

      Run the following command to enter the Docker container. Then you can use KooCLI in the same way as you use it on the host.

      docker exec -it hcloudcli /bin/bash

      Run the following command to exit the hcloudcli container:

      exit

      To stop the hcloudcli container, run the following command:

      docker stop hcloudcli
    • Method 2: Create a temporary container using the hcloudcli image:
      1. Run the following command to create a temporary container:
        docker run --rm -it hcloudcli ${command}
        • If the Docker image is created using a Dockerfile that does not contain ENTRYPOINT ["/usr/local/bin/hcloud"], specify hcloud as the program to run in the docker run command, as shown in the following figure.

        • If the Docker image is created using a Dockerfile that contains ENTRYPOINT ["/usr/local/bin/hcloud"], you do not need to specify a program to run. In this case, running the docker run --rm -it hcloudcli command is equivalent to running the hcloud command on the host, as shown in the following figure.

          If the Docker image is created using a Dockerfile that contains ENTRYPOINT ["/usr/local/bin/hcloud"], docker run only supports KooCLI commands.

      2. Create a temporary container and share files of a host (for example, a host running Linux) with the container.

        Mount a host directory to a container directory to share files of the host with the container.

        Example 1: Mount the /root/.hcloud/ directory of the host to the /root/.hcloud/ directory of the container to share the host configuration file with the container.

        docker run --rm -it -v /root/.hcloud/:/root/.hcloud/ hcloudcli ${command}

        Example 2: Mount the /cli directory of the host to the current directory of the container to share the files of the host with the container.

        docker run --rm -it -v /root/.hcloud/:/root/.hcloud/ -v /cli:$(pwd) hcloudcli ${command}
      3. Create a temporary container and share an environment variable of a host (for example, a host running Linux) with the container.

        Use -e to mark the environment variable to be shared with the container.

        docker run --rm -it -e ${envName} hcloudcli ${command}

    Set an alias for the command (a host running Linux is used as an example). For example, a Docker image has been created using a Dockerfile that contains ENTRYPOINT ["/usr/local/bin/hcloud"]. Run the following command to set the alias hcloud for the original command:

    alias hcloud='docker run --rm -it hcloudcli'

    Then you can run the original command using the alias. See the following figure.

  4. Update the image.

    The KooCLI version in the image is the latest version when the image is created. To ensure that the image uses the latest version, rebuild the image.

  5. Remove the image.

    Run the following command to remove the hcloudcli image:

    docker rmi hcloudcli