Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"az acr build" fails with UnicodeEncodeError #19042

Closed
CCostaT opened this issue Jul 29, 2021 · 7 comments
Closed

"az acr build" fails with UnicodeEncodeError #19042

CCostaT opened this issue Jul 29, 2021 · 7 comments
Labels
Container Registry az acr Service Attention This issue is responsible by Azure service team.

Comments

@CCostaT
Copy link

CCostaT commented Jul 29, 2021

Describe the bug
Running az acr build task in a Microsoft-hosted agent succeeds but when running in a self-hosted agent it fails with UnicodeEncodeError: 'ascii' codec can't encode character '\u0151' in position 834: ordinal not in range(128)

To Reproduce
az acr build --registry {} --file {} --build-arg nocache=true --build-arg {} --image agent:tag {}

Expected behavior
A new docker image is built and pushed to ACR

Environment summary

Install method:

azure-cli is intalled using the command curl -sL https://aka.ms/InstallAzureCLIDeb | bash inside a script that runs inside docker

CLI version (az --version):

azure-cli 2.26.1

core 2.26.1
telemetry 1.0.6

Python location '/opt/az/bin/python3'
Extensions directory '/root/.azure/cliextensions'

Python (Linux) 3.6.10 (default, Jul 14 2021, 08:09:30)
[GCC 9.3.0]

OS Version:

Linux agent 5.4.0-1051-azure #53~18.04.1-Ubuntu SMP Fri Jun 18 22:32:58 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

Shell:

bash

Additional context
For my project, I need to run various instances of the same self-hosted agent in different agent pools.
We use docker to create the self-hosted agent images that are then deployed to AKS. All self-hosted agents share the same code and dockerfile (they just differ in the agent name and the pool they connect to) .
As part of an upgrade, I need to use ACR build tasks to replace the docker commands when creating the self-hosted agent images.

In order to reduce costs, the first self-hosted agent instance (lets call it agent-instance-1) is built using a Microsoft-hosted agent and is built and deployed without any issues.
The second instance (agent-instance-2) is built using the self-hosted agent agent-instance-1 and it fails when running the acr build task.

Part of the agent creation consists in installing the package openjdk-8-jdk using apt. During installation, the package ca-certificates-java (20190405ubuntu1) is also installed and this is the point where the error occurs.

ERROR: The command failed with an unexpected error. Here is the traceback:
ERROR: 'ascii' codec can't encode character '\u0151' in position 2381: ordinal not in range(128)
Traceback (most recent call last):
File "/opt/az/lib/python3.6/site-packages/knack/cli.py", line 231, in invoke
cmd_result = self.invocation.execute(args)
File "/opt/az/lib/python3.6/site-packages/azure/cli/core/commands/init.py", line 657, in execute
raise ex
File "/opt/az/lib/python3.6/site-packages/azure/cli/core/commands/init.py", line 720, in _run_jobs_serially
results.append(self._run_job(expanded_arg, cmd_copy))
File "/opt/az/lib/python3.6/site-packages/azure/cli/core/commands/init.py", line 691, in _run_job
result = cmd_copy(params)
File "/opt/az/lib/python3.6/site-packages/azure/cli/core/commands/init.py", line 328, in call
return self.handler(*args, **kwargs)
File "/opt/az/lib/python3.6/site-packages/azure/cli/core/commands/command_operation.py", line 121, in handler
return op(**command_args)
File "/opt/az/lib/python3.6/site-packages/azure/cli/command_modules/acr/build.py", line 143, in acr_build
return stream_logs(cmd, client, run_id, registry_name, resource_group_name, no_format, True)
File "/opt/az/lib/python3.6/site-packages/azure/cli/command_modules/acr/_stream_utils.py", line 64, in stream_logs
raise_error_on_failure)
File "/opt/az/lib/python3.6/site-packages/azure/cli/command_modules/acr/_stream_utils.py", line 140, in _stream_logs
print(flush.decode('utf-8', errors='ignore'))
File "/opt/az/lib/python3.6/site-packages/colorama/ansitowin32.py", line 41, in write
self.__convertor.write(text)
File "/opt/az/lib/python3.6/site-packages/colorama/ansitowin32.py", line 162, in write
self.write_and_convert(text)
File "/opt/az/lib/python3.6/site-packages/colorama/ansitowin32.py", line 190, in write_and_convert
self.write_plain_text(text, cursor, len(text))
File "/opt/az/lib/python3.6/site-packages/colorama/ansitowin32.py", line 195, in write_plain_text
self.wrapped.write(text[start:end])

Observations

From what I can tell, the error occurs when parsing the docker output coming from ACR. I've tried setting up the env variables PYTHONIOENCODING=utf8 and PYTHONUTF8=1 but to no avail.
When running the pipeline using Microsoft-hosted agents no such error occurs.

@ghost ghost added needs-triage This is a new issue that needs to be triaged to the appropriate team. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that customer-reported Issues that are reported by GitHub users external to the Azure organization. labels Jul 29, 2021
@ghost ghost removed the needs-triage This is a new issue that needs to be triaged to the appropriate team. label Jul 29, 2021
@yonzhan yonzhan added Service Attention This issue is responsible by Azure service team. and removed customer-reported Issues that are reported by GitHub users external to the Azure organization. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that labels Jul 29, 2021
@ghost
Copy link

ghost commented Jul 29, 2021

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @toddysm, @luisdlp, @northtyphoon.

Issue Details

Describe the bug
Running az acr build task in a Microsoft-hosted agent succeeds but when running in a self-hosted agent it fails with UnicodeEncodeError: 'ascii' codec can't encode character '\u0151' in position 834: ordinal not in range(128)

To Reproduce
az acr build --registry {} --file {} --build-arg nocache=true --build-arg {} --image agent:tag {}

Expected behavior
A new docker image is built and pushed to ACR

Environment summary

Install method:

azure-cli is intalled using the command curl -sL https://aka.ms/InstallAzureCLIDeb | bash inside a script that runs inside docker

CLI version (az --version):

azure-cli 2.26.1

core 2.26.1
telemetry 1.0.6

Python location '/opt/az/bin/python3'
Extensions directory '/root/.azure/cliextensions'

Python (Linux) 3.6.10 (default, Jul 14 2021, 08:09:30)
[GCC 9.3.0]

OS Version:

Linux agent 5.4.0-1051-azure #53~18.04.1-Ubuntu SMP Fri Jun 18 22:32:58 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

Shell:

bash

Additional context
For my project, I need to run various instances of the same self-hosted agent in different agent pools.
We use docker to create the self-hosted agent images that are then deployed to AKS. All self-hosted agents share the same code and dockerfile (they just differ in the agent name and the pool they connect to) .
As part of an upgrade, I need to use ACR build tasks to replace the docker commands when creating the self-hosted agent images.

In order to reduce costs, the first self-hosted agent instance (lets call it agent-instance-1) is built using a Microsoft-hosted agent and is built and deployed without any issues.
The second instance (agent-instance-2) is built using the self-hosted agent agent-instance-1 and it fails when running the acr build task.

Part of the agent creation consists in installing the package openjdk-8-jdk using apt. During installation, the package ca-certificates-java (20190405ubuntu1) is also installed and this is the point where the error occurs.

ERROR: The command failed with an unexpected error. Here is the traceback:
ERROR: 'ascii' codec can't encode character '\u0151' in position 2381: ordinal not in range(128)
Traceback (most recent call last):
File "/opt/az/lib/python3.6/site-packages/knack/cli.py", line 231, in invoke
cmd_result = self.invocation.execute(args)
File "/opt/az/lib/python3.6/site-packages/azure/cli/core/commands/init.py", line 657, in execute
raise ex
File "/opt/az/lib/python3.6/site-packages/azure/cli/core/commands/init.py", line 720, in _run_jobs_serially
results.append(self._run_job(expanded_arg, cmd_copy))
File "/opt/az/lib/python3.6/site-packages/azure/cli/core/commands/init.py", line 691, in _run_job
result = cmd_copy(params)
File "/opt/az/lib/python3.6/site-packages/azure/cli/core/commands/init.py", line 328, in call
return self.handler(*args, **kwargs)
File "/opt/az/lib/python3.6/site-packages/azure/cli/core/commands/command_operation.py", line 121, in handler
return op(**command_args)
File "/opt/az/lib/python3.6/site-packages/azure/cli/command_modules/acr/build.py", line 143, in acr_build
return stream_logs(cmd, client, run_id, registry_name, resource_group_name, no_format, True)
File "/opt/az/lib/python3.6/site-packages/azure/cli/command_modules/acr/_stream_utils.py", line 64, in stream_logs
raise_error_on_failure)
File "/opt/az/lib/python3.6/site-packages/azure/cli/command_modules/acr/_stream_utils.py", line 140, in _stream_logs
print(flush.decode('utf-8', errors='ignore'))
File "/opt/az/lib/python3.6/site-packages/colorama/ansitowin32.py", line 41, in write
self.__convertor.write(text)
File "/opt/az/lib/python3.6/site-packages/colorama/ansitowin32.py", line 162, in write
self.write_and_convert(text)
File "/opt/az/lib/python3.6/site-packages/colorama/ansitowin32.py", line 190, in write_and_convert
self.write_plain_text(text, cursor, len(text))
File "/opt/az/lib/python3.6/site-packages/colorama/ansitowin32.py", line 195, in write_plain_text
self.wrapped.write(text[start:end])

Observations

From what I can tell, the error occurs when parsing the docker output coming from ACR. I've tried setting up the env variables PYTHONIOENCODING=utf8 and PYTHONUTF8=1 but to no avail.
When running the pipeline using Microsoft-hosted agents no such error occurs.

Author: CCostaT
Assignees: -
Labels:

Container Registry, Service Attention

Milestone: -

@yonzhan
Copy link
Collaborator

yonzhan commented Jul 29, 2021

route to service team

@shizhMSFT
Copy link
Member

@northtyphoon It is another occurrence of #14774.

@shizhMSFT
Copy link
Member

@CCostaT As mentioned by #14774 (comment), could you try to configure the correct locale by configuring system-widely or configuring for a single user? or upgrade your python version from 3.6 to 3.7 or higher?

@CCostaT
Copy link
Author

CCostaT commented Aug 4, 2021

Hi @shizhMSFT, that link didn't work for me (localectl doesn't work in ubuntu docker images) but it pointed me to the right solution! Thank you.

I got it working now by adding the following to my dockerfile:

RUN locale-gen en_US.UTF-8  
ENV LANG en_US.UTF-8  
ENV LANGUAGE en_US:en  
ENV LC_ALL en_US.UTF-8

Note that this requires the locales package to be installed. I'm not doing it this way but adding something like this to the docker file RUN apt install -y locales before the commands above should be enough

PS: This was based on the solution provided here

@v-mohithgc
Copy link

Hi @shizhMSFT, that link didn't work for me (localectl doesn't work in ubuntu docker images) but it pointed me to the right solution! Thank you.

I got it working now by adding the following to my dockerfile:

RUN locale-gen en_US.UTF-8  
ENV LANG en_US.UTF-8  
ENV LANGUAGE en_US:en  
ENV LC_ALL en_US.UTF-8

Note that this requires the locales package to be installed. I'm not doing it this way but adding something like this to the docker file RUN apt install -y locales before the commands above should be enough

PS: This was based on the solution provided here

Hi @CCostaT , i am facing the same issue in my Azure CLI task, tried using Microsoft hosted agent, didn't work, also tried using the above commands in my docker file, its showing the same error.

Repro:

  1. Create a ACR named XYZ in azure portal
  2. Use the powershell script type “az acr build -r XYZ -f $(dockerfile) .” command in Azure CLI task(v1 and v2)
  3. added dockerfile path in variable settings
  4. Queue the build with Microsoft-hosted agent and self-hosted agent
  5. It will show the same error: 'charmap' codec can't encode character '\u0151'

Docker file:
FROM ubuntu:20.04

#To make it easier for build and release pipelines to run apt-get,
#configure apt to not require confirmation (assume the -y argument by default)

ENV DEBIAN_FRONTEND=noninteractive
RUN echo "APT::Get::Assume-Yes "true";" > /etc/apt/apt.conf.d/90assumeyes
RUN apt-get update && apt-get install -y locales && rm -rf /var/lib/apt/lists/*
&& locale-gen "en_US.UTF-8"
ENV LANG=en_US.UTF-8
LANGUAGE=en_US:en
LC_ALL=en_US.UTF-8
#todo must check base img compatibility of some of these indexed libs (e.g. apt search libicu ). Automate this process

RUN apt-get update && apt-get install -y \

ca-certificates \
curl \
jq \
git \
iputils-ping \
libcurl4 \
libicu66 \
libunwind8 \
netcat \
libssl1.1 \
maven \
unzip \
docker.io \

&& rm -rf /var/lib/apt/lists/*

#no-install-recommends

#installl helm
RUN curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3
&& chmod 700 get_helm.sh
&&./get_helm.sh

RUN curl -LsS https://aka.ms/InstallAzureCLIDeb | bash
&& rm -rf /var/lib/apt/lists/*

ARG TARGETARCH=amd64
ARG AGENT_VERSION=2.185.1

WORKDIR /azp
RUN if [ "$TARGETARCH" = "amd64" ]; then
AZP_AGENTPACKAGE_URL=https://vstsagentpackage.azureedge.net/agent/${AGENT_VERSION}/vsts-agent-linux-x64-${AGENT_VERSION}.tar.gz;
else
AZP_AGENTPACKAGE_URL=https://vstsagentpackage.azureedge.net/agent/${AGENT_VERSION}/vsts-agent-linux-${TARGETARCH}-${AGENT_VERSION}.tar.gz;
fi;
curl -LsS "$AZP_AGENTPACKAGE_URL" | tar -xz

COPY start.sh .
RUN chmod +x start.sh

#install jdk
RUN apt-get update && apt-get install -y
openjdk-8-jdk
openjdk-11-jdk
&& rm -rf /var/lib/apt/lists/*

ENV JAVA_HOME_11_X64=/usr/lib/jvm/java-11-openjdk-amd64
ENV JAVA_HOME_8_X64=/usr/lib/jvm/java-1.8.0-openjdk-amd64

#install kubectl
RUN curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
&& install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl

#install azure devops cli
RUN az config set extension.use_dynamic_install=yes_without_prompt
&& az extension add --name azure-devops
&& az devops -h

#/bin/java
ENTRYPOINT [ "./start.sh" ]

@CCostaT
Copy link
Author

CCostaT commented Feb 28, 2022

Hi @v-mohithgc . I'm not entirely sure what could be wrong but It seems you are installing and cleaning stuff in the wrong order.

Your docker file installs the locales package and imediately deletes the downloaded files

...
RUN apt-get update && apt-get install -y locales && rm -rf /var/lib/apt/lists/* && locale-gen "en_US.UTF-8"...

and only afterwards do you install all the other required packages:

RUN apt-get update && apt-get install -y \
ca-certificates \
curl \
jq \
git \
iputils-ping \
libcurl4 \
libicu66 \
libunwind8 \
netcat \
libssl1.1 \
maven \
unzip \
docker.io \

&& rm -rf /var/lib/apt/lists/*

What I did was install everything using one apt-get install -y <packages>, then clean all cached/downloaded packages and finally generate the locale. Something like:

FROM ubuntu:20.04

# Install all required packages
RUN apt-get update && apt-get install -y \
locales\
ca-certificates \
curl \
jq \
git \
openjdk-8-jdk \
openjdk-11-jdk \
<all other required packages>

# Cleanup
RUN apt-get -q autoremove \
      && apt-get -q clean -y \
      && rm -rf /var/lib/apt/lists/* /var/cache/apt/*.bin

# Setup correct locale
RUN locale-gen en_US.UTF-8  
ENV LANG en_US.UTF-8  
ENV LANGUAGE en_US:en  
ENV LC_ALL en_US.UTF-8

# Rest of the docker file goes here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Container Registry az acr Service Attention This issue is responsible by Azure service team.
Projects
None yet
Development

No branches or pull requests

4 participants