You may need to build container images directly within your ACS cluster. This topic describes two methods for doing so: Docker in Docker (DinD), which requires privileged mode, and Buildah, which does not.
Prerequisites
This document applies to the PPU PIP service from PTG (which requires explicit authentication) and the passwordless PPU PIP service from Alibaba Cloud. For more information, see Use the PPU PIP Service.
You have created an ACS cluster.
You have obtained the cluster kubeconfig and connected to the cluster by using kubectl.
To use Docker in Docker, you must enable privileged mode for the container, which requires you to submit a ticket.
Develop custom images with Docker in Docker (DinD)
The data for
dockerd(specified by the--data-rootparameter) is stored in/var/lib/dockerby default. To use theoverlay2storage driver, you must mount/var/lib/dockeron a separate file system that supports it. We recommend using anemptyDirvolume for this purpose.If you use
buildkitas the build engine and want to useoverlayfs, you must mount/var/lib/buildkitas a separate file system that supportsoverlayfs. We recommend using anemptyDirvolume for this purpose.To use the
registry.cn-hangzhou.aliyuncs.com/acs-demo-ns/docker:27-dindcontainer image, ensure the Pod has access to the public network.When building a container image from a Dockerfile using the DinD method, you may not be able to mount PPU devices. We recommend using Buildah to build images instead.
Create a DinD Pod
Save the following YAML content as acs-dind-demo.yaml.
ImportantIn pay-as-you-go scenarios, specifications may vary by card type. For details on resource limitations, see P16EN. This limitation does not apply to capacity reservation scenarios.
Pay-as-you-go
apiVersion: v1 kind: Pod metadata: labels: alibabacloud.com/compute-class: gpu alibabacloud.com/gpu-model-series: PPU810E alibabacloud.com/compute-qos: default alibabacloud.com/hpn-type: "rdma" # Optional name: acs-dind-demo spec: containers: - image: registry.cn-hangzhou.aliyuncs.com/acs-demo-ns/docker:27-dind name: main resources: # Adjust resources as needed. limits: cpu: "16" ephemeral-storage: 256Gi memory: 128Gi alibabacloud.com/ppu: "2" requests: cpu: "16" ephemeral-storage: 256Gi memory: 128Gi alibabacloud.com/ppu: "2" volumeMounts: - mountPath: /var/lib/docker name: docker # Optional. Required if you use buildkit. - mountPath: /var/lib/buildkit name: buildkit securityContext: # Declare this as a privileged container. You must submit a ticket to request this capability. privileged: true volumes: - emptyDir: {} name: docker # Optional. Required if you use buildkit. - emptyDir: {} name: buildkitCapacity reservation
apiVersion: v1 kind: Pod metadata: labels: alibabacloud.com/compute-class: gpu-hpn alibabacloud.com/gpu-model-series: PPU810E alibabacloud.com/compute-qos: default alibabacloud.com/hpn-type: "rdma" name: acs-dind-demo spec: containers: - image: registry.cn-hangzhou.aliyuncs.com/acs-demo-ns/docker:27-dind name: main resources: # Adjust resources as needed. limits: cpu: "16" ephemeral-storage: 256Gi memory: 128Gi alibabacloud.com/ppu: "2" requests: cpu: "16" ephemeral-storage: 256Gi memory: 128Gi alibabacloud.com/ppu: "2" volumeMounts: - mountPath: /var/lib/docker name: docker # Optional. Required if you use buildkit. - mountPath: /var/lib/buildkit name: buildkit securityContext: # Declare this as a privileged container. You must submit a ticket to request this capability. privileged: true volumes: - emptyDir: {} name: docker # Optional. Required if you use buildkit. - emptyDir: {} name: buildkitRun the following command to create the Pod.
kubectl apply -f acs-dind-demo.yamlRun the following command to check the Pod status.
kubectl get pod acs-dind-demoExpected output:
NAME READY STATUS RESTARTS AGE acs-dind-demo 1/1 Running 0 7m41s
Develop a PPU image in DinD
You can obtain PPU container images from the ACS Container Image Release Notes. Note that you must use public network image addresses in this scenario.
To launch a PPU container within the DinD environment, you must use privileged mode by starting the container with the
docker run --privilegedcommand.The new image must be pushed to your own image repository. The image repositories provided by Alibaba Cloud support pull operations only and do not support push operations.
The workflow is to start a privileged PPU container inside the DinD Pod, install your software, and then commit the container as a new custom image from the outer Pod.
Pull a PPU container image into the DinD container to use as the base image.
In the DinD container, run the
id=$(docker run -tid --privileged {ppu-image-uri} bash)command to start an inner PPU container, and run thedocker exec -it $id bashcommand to enter the inner PPU container.Inside the inner PPU container, you can install Linux packages and Python PIP packages as you would on a standard Linux host. You can either convert the installation commands from your Dockerfile into a shell script or run them manually.
After the installation is complete and temporary files are cleaned up, run
exitin the inner PPU container to exit to the outer DinD container.Run the
docker commit $id {new-image}:{new-tag}command in the DinD container to commit the internal PPU container environment to a new container image. For detailed steps on how to push the new image to your image repository, see Push and pull images by using an Enterprise Edition instance.After you finish developing the new image, run
docker stop $id && docker rm $idto stop and remove the inner PPU container.
Build custom images with Buildah
To use the
registry-cn-hangzhou.ack.aliyuncs.com/ack-demo/buildah:v1.40.1container image, ensure the Pod has access to the public network.This example shows how to run the container image build steps manually. For a complete YAML file that includes the build steps in an automated workflow, see Run an image build task on an ACS instance. Note that you must mount your Dockerfile and any dependent files into the Pod by using a volume. This step is not covered in this example. For more information, see Storage overview.
We recommend that you use the ACS pay-as-you-go mode with Buildah. If you want to use capacity reservation, you can replace
metadata.labels.
Create a Buildah Pod
Save the following YAML content as acs-buildah-demo.yaml.
ImportantIn pay-as-you-go scenarios, specifications may vary by card type. For details on resource limitations, see P16EN. This limitation does not apply to capacity reservation scenarios.
kind: Pod apiVersion: v1 metadata: name: acs-buildah-demo labels: alibabacloud.com/compute-class: gpu alibabacloud.com/gpu-model-series: PPU810E alibabacloud.com/compute-qos: default spec: restartPolicy: OnFailure volumes: - emptyDir: {} name: buildah containers: - name: builder image: registry-cn-hangzhou.ack.aliyuncs.com/ack-demo/buildah:v1.40.1 command: - sh - -c - sleep infinity imagePullPolicy: Always volumeMounts: - mountPath: /var/lib/containers/storage name: buildah resources: # Adjust resources as needed. limits: cpu: "16" memory: "128Gi" ephemeral-storage: 500Gi alibabacloud.com/ppu: 2 requests: cpu: "16" memory: "128Gi" ephemeral-storage: 500Gi alibabacloud.com/ppu: 2Run the following command to create the Pod.
kubectl apply -f acs-buildah-demo.yamlRun the following command to check the Pod status.
kubectl get pod acs-buildah-demoExpected output:
NAME READY STATUS RESTARTS AGE acs-buildah-demo 1/1 Running 0 7m41s
Build a custom image in Buildah
You can obtain PPU container images from the ACS Container Image Release Notes. Note that you must use public network image addresses in this scenario.
The new image must be pushed to your own image repository. The image repositories provided by Alibaba Cloud support pull operations only and do not support push operations.
The procedure is as follows:
Run the
cdcommand to change to the Dockerfile directory.The following is an example of a Dockerfile that uses a PPU container image as its base image:
# If you use a standard image provided by ACS # FROM egslingjun-registry.cn-wulanchabu.cr.aliyuncs.com/egslingjun/{image:tag} # You can also use a PPU image that you have copied to your own repository # FROM {customers-docker-registery}/{image:tag} # Other Dockerfile commands are omittedRun the
buildah --storage-driver=overlay build -t test:v1 .command to build the image.(Optional) Check the built image:
buildah --storage-driver=overlay images.Push the new image to your own image repository:
Log in to the repository:
buildah login -u "{LOGIN_USERNAME}" -p "{LOGIN_PASSWORD}" {target-registry-domain}.Push the image to the repository:
buildah --storage-driver=overlay push test:v1 docker://{target-registry-prefix}/{target-image}.
FAQ
If the ACS VPC CIDR block is set to 172.17.0.0/16, the DinD Pod cannot be accessed remotely. How do I fix this?
Analysis: This problem occurs because the default Docker CIDR block in the ACS Pod, 172.17.0.0/16, conflicts with the VPC CIDR block.
Solution: You can specify the
bipparameter fordockerdto change its default CIDR block. To do this:Create a ConfigMap where the
bipfield can be customized to a different, non-conflicting address block, such as192.168.5.1/24.kubectl create configmap docker-daemon-config \ --from-literal=daemon.json='{ "bip": "192.168.5.1/24" }' \ -n <your-namespace> # If you do not specify the -n flag, the ConfigMap is created in the default namespace.Add the following volume mount configuration to the DinD Pod's YAML file (from the Create a DinD Pod section) to mount the ConfigMap.
volumeMounts: - name: docker-config-volume mountPath: /etc/docker/daemon.json subPath: daemon.json volumes: - name: docker-config-volume configMap: name: docker-daemon-config items: - key: daemon.json path: daemon.json