It's possible, but very subtle to debug, so make sure you follow the steps below.
- Use
dockerfile
agent in jenkins pipeline (You can name it Dockerfile.jenkins
or something else you prefer) and install amazon ecr credential helper in it to get a clean and stable building environment.
FROM ubuntu:rolling
RUN apt-get update && apt-get install -y amazon-ecr-credential-helper
- Create a
config.json
file in your git repo, like .docker/config.json
.
{
"credHelpers": {
"[YOUR_ACCOUNT_ID].dkr.ecr.[YOUR_REGION].amazonaws.com": "ecr-login"
}
}
- Test
docker pull
in your Jenkinsfile
, make sure your access key's user is enabled with the right policy (probably AmazonEC2ContainerRegistryFullAccess
).
pipeline {
agent {
dockerfile {
filename 'Dockerfile.jenkins'
}
}
stages {
stage('TEST ECR') {
steps {
script {
sh "DOCKER_CONFIG=.docker AWS_ACCESS_KEY_ID=[YOUR_ACCESS_KEY_ID] AWS_SECRET_ACCESS_KEY=[YOUR_SECRET_KEY] docker pull [YOUR PRIVATE IMAGE]"
// docker.build("${tag}", "${DOCKER_BUILD_ARGS} -f Dockerfile .")
// sh "docker push ${tag}"
}
}
}
}
}
If it's okay to pull, then you can just change DOCKER_CONFIG=.docker AWS_ACCESS_KEY_ID=[YOUR_ACCESS_KEY_ID] AWS_SECRET_ACCESS_KEY=[YOUR_SECRET_KEY] docker pull [YOUR PRIVATE IMAGE]
to docker push [YOUR IMAGE]
under correct environment variable settings.
Your repo would seem:
.
├── .docker
│ └── config.json
├── Dockerfile
└── Dockerfile.jenkins