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:
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.Create a custom image: Run the
docker build
command to create a custom Docker image by using theDockerfile
.Start a container: Run the
docker run
command to load the custom image and run the Docker container.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 oneDockerfile
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
:
Step 2: Create a custom image
Run the following command in the directory in which the
Dockerfile
resides to create 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 aliyuncli
Notemycli
: 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.
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 aliyuncli /bin/sh
Run the
aliyun version
command in the container to view the version of Alibaba Cloud CLI.
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.