Run Alibaba Cloud CLI in a Docker container
Use Docker to run Alibaba Cloud CLI in an isolated, secure environment.
Prerequisites
-
Docker 18.09 or later is installed. Get Docker.
-
Run
docker --versionto 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:
-
Create a
Dockerfile: ADockerfileis a plaintext file containing the commands to build an image. -
Build a custom image: Run
docker buildto create a Docker image from theDockerfile. -
Start a container: Run
docker runto launch a container from the image. -
Connect to the container: Run
docker execto 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 oneDockerfile. -
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
Dockerfileinstead:
Step 2: Build a custom image
-
In the
Dockerfiledirectory, build a Docker image namedaliyuncli:docker build --tag aliyuncli . -
Expected output:

Step 3: Start a container
-
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.
-
-
Wait until the container ID is returned.

Step 4: Connect to the container
-
Connect to the running container:
docker exec -it mycli /bin/sh -
Run
aliyun versionin the container to verify the CLI installation.
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.