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.
After the installation is complete, run the
docker --versioncommand to verify that Docker is installed correctly.
Overview
To run Alibaba Cloud CLI in a Docker container, perform the following steps:
Create a
Dockerfile: ADockerfileis a plaintext file that instructs the system to automatically create an image. A Dockerfile contains a set of commands and parameters.Build a custom image: Run the
docker buildcommand to create a custom Docker image from theDockerfile.Start a container: Run the
docker runcommand to load the custom image and run the Docker container.Connect to the container: Run the
docker execcommand 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/Note
A Dockerfile must be named
Dockerfile, which starts with the uppercase letter D and has no file name extension. Only oneDockerfilecan 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.The example uses CentOS as the base image. If you use Alpine Linux, use the following sample
Dockerfileinstead:
Step 2: Build a custom image
Run the following command in the directory in which the
Dockerfileresides to build a custom Docker image namedaliyuncli:docker build --tag aliyuncli .The following figure shows the expected output.

Step 3: Start a container
After the custom Docker image is created, run the following command to start a Docker container:
docker run -it -d --name mycli aliyunclimycli: 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: Build a custom image.
Wait until the container ID is returned.

Step 4: Connect to the container
After the container is started, you can run the following command to connect to the container:
docker exec -it mycli /bin/shRun the
aliyun versioncommand in the container to view the version of Alibaba Cloud CLI.
Related operations
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.