Run Alibaba Cloud CLI in a Docker container

更新时间:
复制 MD 格式

Use Docker to run Alibaba Cloud CLI in an isolated, secure environment.

Prerequisites

  • Docker 18.09 or later is installed. Get Docker.

  • Run docker --version to verify the installation.

  • ISP network issues in mainland China may cause slow or failed Docker Hub image pulls. Alibaba Cloud Container Registry (ACR) provides an image accelerator for faster downloads.

    For more information, see Accelerate the pulls of Docker official images.

Overview

The workflow involves the following steps:

  1. Create a Dockerfile: A Dockerfile is a plaintext file containing the commands to build an image.

  2. Build a custom image: Run docker build to create a Docker image from the Dockerfile.

  3. Start a container: Run docker run to launch a container from the image.

  4. Connect to the container: Run docker exec to access the running container and use Alibaba Cloud CLI.

Step 1: Create a Dockerfile

Procedure

Create a directory and save the following content to a file named Dockerfile:

FROM centos:latest

# Obtain and install Alibaba Cloud CLI. In this example, the latest version of Alibaba Cloud CLI is used.
# Download the installation package of Alibaba Cloud CLI.
RUN curl -SLO "https://aliyuncli.alicdn.com/aliyun-cli-linux-latest-amd64.tgz"
# Decompress the installation package.
RUN tar -xvzf aliyun-cli-linux-latest-amd64.tgz
# Delete the installation package.
RUN rm aliyun-cli-linux-latest-amd64.tgz
# Move the executable file aliyun to the /usr/local/bin directory.
RUN mv aliyun /usr/local/bin/

Note

  • The file must be named Dockerfile (uppercase D, no extension). Each directory can contain only one Dockerfile.

  • If you use an ARM processor such as Apple M1, change the download URL to https://aliyuncli.alicdn.com/aliyun-cli-linux-latest-arm64.tgz.

  • The preceding example uses CentOS. For Alpine Linux, use the following Dockerfile instead:

    Sample Dockerfile for Alpine Linux

    FROM alpine:latest
    
    # Install the jq tool to display the command output in the JSON format.
    RUN apk add --no-cache jq
    
    # Obtain and install Alibaba Cloud CLI.
    # Download the installation package of Alibaba Cloud CLI.
    RUN wget https://aliyuncli.alicdn.com/aliyun-cli-linux-latest-amd64.tgz
    # Decompress the installation package.
    RUN tar -xvzf aliyun-cli-linux-latest-amd64.tgz
    # Delete the installation package.
    RUN rm aliyun-cli-linux-latest-amd64.tgz
    # Move the executable file aliyun to the /usr/local/bin directory.
    RUN mv aliyun /usr/local/bin/
    
    # If you use Alpine Linux, you must run the following command to create a separate symbolic link that points to the lib64 dynamic library:
    RUN mkdir /lib64 && ln -s /lib/libc.musl-x86_64.so.1 /lib64/ld-linux-x86-64.so.2

Step 2: Build a custom image

  1. In the Dockerfile directory, build a Docker image named aliyuncli:

    docker build --tag aliyuncli .
  2. Expected output:

    image

Step 3: Start a container

  1. Start a Docker container from the custom image:

    docker run -it -d --name mycli aliyuncli
    • mycli: the container name. Customize as needed.

    • aliyuncli: the custom image name. Must match the image created in Step 2: Build a custom image.

  2. Wait until the container ID is returned.

    image

Step 4: Connect to the container

  1. Connect to the running container:

    docker exec -it mycli /bin/sh
  2. Run aliyun version in the container to verify the CLI installation.

    image

What to do next

After connecting to the container, Configure profiles for Alibaba Cloud CLI, then Generate and run commands to manage your cloud resources.