Advanced
Тема интерфейса

Configuring and Using Cloud CLI in Docker

Configure and use Cloud CLI 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 Cloud CLI with one click.
    RUN curl -sSL https://sbc-cli.obs.ru-moscow-1.hc.sbercloud.ru/cli/latest/cloud_install.sh -o ./cloud_install.sh && bash ./cloud_install.sh -y
    WORKDIR cloud
    Note

    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 Cloud CLI as the program to run when the container is started:

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

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

  2. Build an image.

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

    docker build --no-cache -t cloudcli .
    Note

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

    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 cloudcli image and run commands in the container.
      docker run -it -d --name cloudcli cloudcli

      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 Cloud CLI in the same way as you use it on the host.

      docker exec -it cloudcli /bin/bash

      Run the following command to exit the cloudcli container:

      exit

      To stop the cloudcli container, run the following command:

      docker stop cloudcli
    • Method 2: Create a temporary container using the cloudcli image:
      1. Run the following command to create a temporary container:
        docker run --rm -it cloudcli ${command}
        • If the Docker image is created using a Dockerfile that does not contain ENTRYPOINT ["/usr/local/bin/cloud"], specify cloud as the program to run in the docker run command, for example, to query the current CLI version.
          docker run --rm -it cloudcli cloud version
        • If the Docker image is created using a Dockerfile that contains ENTRYPOINT ["/usr/local/bin/cloud"], you do not need to specify a program to run. In this case, running the docker run --rm -it cloudcli command is equivalent to running the cloud command on the host. For example:
          docker run --rm -it cloudcli version
          Note

          If the Docker image is created using a Dockerfile that contains ENTRYPOINT ["/usr/local/bin/cloud"], docker run only supports Cloud CLI 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/.cloud/ directory of the host to the /root/.cloud/ directory of the container to share the host configuration file with the container.

        docker run --rm -it -v /root/.cloud/:/root/.cloud/ cloudcli ${command}

        For example, a Docker image has been created using a Dockerfile that contains ENTRYPOINT ["/usr/local/bin/cloud"]. Run the following command to view the added profile:

        docker run --rm -it -v /root/.cloud/:/root/.cloud/ cloudcli configure list

        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/.cloud/:/root/.cloud/ -v /cli:$(pwd) cloudcli ${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} cloudcli ${command}
    Note

    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/cloud"]. Run the following command to set the alias cloud for the original command:

    alias cloud='docker run --rm -it cloudcli'

    Then you can run the original command using the alias.

  4. Update the image.

    The Cloud CLI 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 cloudcli image:

    docker rmi cloudcli