All Products
Search
Document Center

Alibaba Cloud CLI:Run Alibaba Cloud CLI in a Docker container

Last Updated:Mar 28, 2025

You can use Docker to create an isolated environment to run Alibaba Cloud CLI. This increases the security of the runtime environment. This topic describes how to run Alibaba Cloud CLI in a Docker container.

Prerequisites

  • Docker 18.09 or later is installed. For more information, see Get Docker.

  • The installation information of Docker is verified. You can run the docker --version command to verify the installation information of Docker.

  • A custom image source and image repository of Docker are configured. You can configure a custom image source and image repository to prevent the impact of limits such as network fluctuation and improve the efficiency of image deployment and update.

Overview

To run Alibaba Cloud CLI in a Docker container, perform the following steps:

  1. Create a Dockerfile: ADockerfile is a plaintext file that instructs the system to automatically create an image. A Dockerfile contains a set of commands and parameters.

  2. Create a custom image: Run the docker build command to create a custom Docker image by using the Dockerfile.

  3. Start a container: Run the docker run command to load the custom image and run the Docker container.

  4. Connect to the container: Run the docker exec command to access the container that is started. You can use Alibaba Cloud CLI in the container.

Step 1: Create a Dockerfile

Procedure

Create a directory on the desktop or in any place. Save the following code to a plaintext 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/

Usage notes

  • A Dockerfile must be named Dockerfile, which starts with the uppercase letter D and has no file name extension. Only one Dockerfile can be saved in each directory.

  • 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.

  • In this example, CentOS is used. If you want to use Alpine Linux, replace the previous code with the following code in the Dockerfile:

    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: Create a custom image

  1. Run the following command in the directory in which the Dockerfile resides to create a custom Docker image named aliyuncli:

    docker build --tag aliyuncli .
  2. The following figure shows the expected output.

    image

Step 3: Start a container

  1. After the custom Docker image is created, run the following command to start a Docker container:

    docker run -it -d --name mycli aliyuncli
    Note
    • mycli: the name of the container. You can customize the container name.

    • aliyuncli: the name of the custom image. The name of the image must be the same as that of the image that you create in Step 2: Create a custom image.

  2. Wait until the container ID is returned.

    image

Step 4: Connect to the container

  1. After the container is started, you can run the following command to connect to the container:

    docker exec -it aliyuncli /bin/sh
  2. Run the aliyun version command in the container to view the version of Alibaba Cloud CLI.

    image

What to do next

After you connect to the Docker container, you must configure profiles for Alibaba Cloud CLI. You can use Alibaba Cloud CLI to interact with Alibaba Cloud services and manage Alibaba Cloud services in Shell. For more information, see Configure profiles and Generate and run commands.