You are on page 1of 15

17/10/2023, 12:12 Building and Publishing a Docker Image to Amazon ECR using Bitbucket Pipelines | by Gabriel Pulga | AWS

riel Pulga | AWS in Plain English

Open in app

Search

Building and Publishing a Docker Image to


Amazon ECR using Bitbucket Pipelines
Gabriel Pulga · Follow
Published in AWS in Plain English
5 min read · May 5, 2021

Listen Share More

Introduction

The original vision of cloud computing was automated, on-demand services that
scaled dynamically to meet demand.

While this vision is now a reality, it doesn’t happen on its own. Cloud automation is
complex and requires specialized tools, expertise, and hard work.

In this post, we continue our exploration further.

https://aws.plainenglish.io/building-and-publishing-a-docker-image-to-amazon-ecr-using-bitbucket-pipelines-718e2f8bb3dd 1/15
17/10/2023, 12:12 Building and Publishing a Docker Image to Amazon ECR using Bitbucket Pipelines | by Gabriel Pulga | AWS in Plain English

The plan is to build a container and push it to a container registry, all from within
Bitbucket Pipelines.

What are Bitbucket Pipelines?

The definition of Bitbucket Pipelines according to their documentation:

An integrated CI/CD service built into Bitbucket. It allows you to automatically build, test,
and even deploy your code based on a configuration file in your repository.

Essentially, we create containers in the cloud for you. Inside these containers, you can run
commands (like you might on a local machine) but with all the advantages of a fresh
system, customized and configured for your needs.

So what does this mean exactly?

It’s a service that Bitbucket offers that allows for code building, testing, and
deployment all based on a configuration file from within the repository.

This file is called a YAML file called bitbucket-pipelines.yml that is located inside the
root of the repository.

What is Amazon Elastic Container Registry (ECR)?


Amazon Elastic Container Registry (Amazon ECR) is an AWS-managed container
image registry service that supports private container image repositories with
resource-based permissions using AWS IAM.

https://aws.plainenglish.io/building-and-publishing-a-docker-image-to-amazon-ecr-using-bitbucket-pipelines-718e2f8bb3dd 2/15
17/10/2023, 12:12 Building and Publishing a Docker Image to Amazon ECR using Bitbucket Pipelines | by Gabriel Pulga | AWS in Plain English

This is so that specified users or Amazon EC2 instances can access your container
repositories and images.

You can use your preferred CLI to push, pull, and manage Docker images, Open
Container Initiative (OCI) images, and OCI compatible artifacts.

Amazon ECR contains the following components :

Registry

An Amazon ECR registry is provided to each AWS account; you can create image
repositories in your registry and store images in them.

For more information, see Amazon ECR private registries.

Authorization token

Your client must authenticate to Amazon ECR registries as an AWS user before it can
push and pull images.

For more information, see Private registry authentication.

Repository

An Amazon ECR image repository contains your Docker images, Open Container
Initiative (OCI) images, and OCI compatible artifacts.

For more information, see Amazon ECR private repositories.

Repository policy

https://aws.plainenglish.io/building-and-publishing-a-docker-image-to-amazon-ecr-using-bitbucket-pipelines-718e2f8bb3dd 3/15
17/10/2023, 12:12 Building and Publishing a Docker Image to Amazon ECR using Bitbucket Pipelines | by Gabriel Pulga | AWS in Plain English

You can control access to your repositories and the images within them with
repository policies.

For more information, see Repository policies.

Image

You can push and pull container images to your repositories. You can use these
images locally on your development system, or you can use them in Amazon ECS
task definitions and Amazon EKS pod specifications.

For more information, see Using Amazon ECR images with Amazon ECS and Using
Amazon ECR Images with Amazon EKS.

What is Docker?
Docker is a tool designed to make it easier to create, deploy, and run applications by
using containers.

Containers allow a developer to package up an application with all of the parts it


needs, such as libraries and other dependencies, and deploy it as one package.

By doing so, thanks to the container, the developer can rest assured that the
application will run on any other Linux machine regardless of any customized
settings that machine might have that could differ from the machine used for
writing and testing the code.

https://aws.plainenglish.io/building-and-publishing-a-docker-image-to-amazon-ecr-using-bitbucket-pipelines-718e2f8bb3dd 4/15
17/10/2023, 12:12 Building and Publishing a Docker Image to Amazon ECR using Bitbucket Pipelines | by Gabriel Pulga | AWS in Plain English

What is a Docker Image?


Docker images are instructions written in a special file called a Dockerfile . It has its
own syntax and defines what steps Docker will take to build your container.

Since containers are only layers upon layers of changes, each new command you
create in a Docker image will create a new layer in the container.

When you run docker build . on the same directory as the Dockerfile, Docker
daemon will start building the image and packaging it so you can use it. Then you
can run docker run <image-name> to start a new container.

Configuring the Pipeline To Build and Deploy


There are two ways to configure a Bitbucket pipeline :

Writing the YAML file directly.

Use the UI wizard provided by Bitbucket.

An advantage of using the Bitbucket configuration wizard, is that it provides some


templates that will override any configuration content.

https://aws.plainenglish.io/building-and-publishing-a-docker-image-to-amazon-ecr-using-bitbucket-pipelines-718e2f8bb3dd 5/15
17/10/2023, 12:12 Building and Publishing a Docker Image to Amazon ECR using Bitbucket Pipelines | by Gabriel Pulga | AWS in Plain English

On this tutorial, we’ll be pushing a docker image to the AWS Elastic Container
Registry (ECR).

Building the application and configuring our AWS credentials is done by simply
calling for a docker build command and creating a pipe to push our image to ECR.

To use the pipe you should have a IAM user configured with programmatic access
or Web Identity Provider (OIDC) role, with the necessary permissions to push
docker images to your ECR repository.

You also need to set up a ECR container registry if you don’t already have on.

Here is a AWS ECR Getting started guide from AWS on how to set up a new registry.

image: atlassian/default-image:2
pipelines:
branches:
master:
- step:
name: Build and AWS Setup
https://aws.plainenglish.io/building-and-publishing-a-docker-image-to-amazon-ecr-using-bitbucket-pipelines-718e2f8bb3dd 6/15
17/10/2023, 12:12 Building and Publishing a Docker Image to Amazon ECR using Bitbucket Pipelines | by Gabriel Pulga | AWS in Plain English

services:
- docker
script:
# change directory to application folder
- cd application-folder
# creates variables (timestamp, image name and tag)
- export TIMESTAMP="$(date +%Y%m%d)"
- export IMAGE_NAME=application
- export TAG="$TIMESTAMP"
# builds docker image from a local dockerfile
- docker build -t application .
# tags image as a way to id it by the timestamp
- docker tag application application:$TIMESTAMP
# use pipe to push the image to AWS ECR
- pipe: atlassian/aws-ecr-push-image:1.3.0
variables:
AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
AWS_DEFAULT_REGION: us-east-2
IMAGE_NAME: application:$TIMESTAMP
TAGS: "$TIMESTAMP $BITBUCKET_BUILD_NUMBER"

The variables AWS_ACCESS_KEY_ID , AWS_SECRET_ACCESS_KEY and AWS_DEFAULT_REGION are


configured as repository variables, so there is no need to declare them in the pipe.

Once our pipeline’s been commited, a code insight is generated along with
annotations, metrics, and reports by Bitbucket.

If the operation was successful, an image of the application should be located inside
an Elastic Container Registry repository.

Hope you found this article useful. Thank you for reading.

More content at plainenglish.io

Docker AWS DevOps Cloud Computing Programming

https://aws.plainenglish.io/building-and-publishing-a-docker-image-to-amazon-ecr-using-bitbucket-pipelines-718e2f8bb3dd 7/15
17/10/2023, 12:12 Building and Publishing a Docker Image to Amazon ECR using Bitbucket Pipelines | by Gabriel Pulga | AWS in Plain English

Follow

Written by Gabriel Pulga


97 Followers · Writer for AWS in Plain English

DevOps/SRE Engineer from Brazil. Check out my github at @gabrielpulga

More from Gabriel Pulga and AWS in Plain English

Gabriel Pulga

Minikube, Kind ou K3s — Como começar com Kubernetes?


O que são Kubernetes? Como rodar um cluster local? Quais ferramentas utilizar? Nesse artigo
todas essas questões são abordadas.

4 min read · Jul 15, 2021

--

https://aws.plainenglish.io/building-and-publishing-a-docker-image-to-amazon-ecr-using-bitbucket-pipelines-718e2f8bb3dd 8/15
17/10/2023, 12:12 Building and Publishing a Docker Image to Amazon ECR using Bitbucket Pipelines | by Gabriel Pulga | AWS in Plain English

Ajay Kumar Yegireddi in AWS in Plain English

DevSecOps: Deploying the 2048 Game on Docker and Kubernetes with


Jenkins CI/CD
Hello friends, we will be deploying a React Js 2048 Game. We will be using Jenkins as a CICD
tool and deploying our application on a Docker…

11 min read · Sep 24

-- 1

Cloudmize in AWS in Plain English

https://aws.plainenglish.io/building-and-publishing-a-docker-image-to-amazon-ecr-using-bitbucket-pipelines-718e2f8bb3dd 9/15
17/10/2023, 12:12 Building and Publishing a Docker Image to Amazon ECR using Bitbucket Pipelines | by Gabriel Pulga | AWS in Plain English

Cloud Architect vs. Cloud Engineer: Choosing the Right Career Path
Making an important career decision in cloud computing

4 min read · Sep 21

-- 7

Gabriel Pulga

Documenting a Spring Boot CRUD RESTful API/Web Service with


Swagger
Learn how to implement an user interface with swagger for your API with this in depth tutorial

3 min read · Jun 12, 2020

--

See all from Gabriel Pulga

See all from AWS in Plain English

https://aws.plainenglish.io/building-and-publishing-a-docker-image-to-amazon-ecr-using-bitbucket-pipelines-718e2f8bb3dd 10/15
17/10/2023, 12:12 Building and Publishing a Docker Image to Amazon ECR using Bitbucket Pipelines | by Gabriel Pulga | AWS in Plain English

Recommended from Medium

Nnyw in AWS Tip

Deploying Dockerised Applications to EC2 Using Bitbucket Pipelines and


ECR
Bitbucket Pipelines is a robust CI/CD tool that allows developers to automate the build, test,
and deployment processes. In this article…

· 5 min read · Jun 28

-- 5

https://aws.plainenglish.io/building-and-publishing-a-docker-image-to-amazon-ecr-using-bitbucket-pipelines-718e2f8bb3dd 11/15
17/10/2023, 12:12 Building and Publishing a Docker Image to Amazon ECR using Bitbucket Pipelines | by Gabriel Pulga | AWS in Plain English

Matteo Bianchi

2023 DevOps is terrible.


My analysis of modern DevOps evolution into Platform Engineering. Just a new trend or a
revolution in the IT industry?

7 min read · Sep 21

-- 27

Lists

General Coding Knowledge


20 stories · 443 saves

It's never too late or early to start something


15 stories · 166 saves

Coding & Development


11 stories · 218 saves

Stories to Help You Grow as a Software Developer


19 stories · 465 saves

https://aws.plainenglish.io/building-and-publishing-a-docker-image-to-amazon-ecr-using-bitbucket-pipelines-718e2f8bb3dd 12/15
17/10/2023, 12:12 Building and Publishing a Docker Image to Amazon ECR using Bitbucket Pipelines | by Gabriel Pulga | AWS in Plain English

Eric Anicet

Spring Boot API deployment using GitLab CI/CD and Docker


In this story, we’ll explain step by step how to use GitLab CI/CD to build, test and deploy Spring
boot API using Docker.

· 5 min read · May 15

--

Subhan Naeem in ITNEXT

https://aws.plainenglish.io/building-and-publishing-a-docker-image-to-amazon-ecr-using-bitbucket-pipelines-718e2f8bb3dd 13/15
17/10/2023, 12:12 Building and Publishing a Docker Image to Amazon ECR using Bitbucket Pipelines | by Gabriel Pulga | AWS in Plain English

CI/CD: An introduction using Bitbucket pipelines


A prerequisite for stable productive apps

10 min read · Jul 9

-- 1

Shazab Tanveer

AWS Devops using CodeCommit, CodeBuild, CodeDeploy CodePipeline


to ECS ECR
We will deal with following :

7 min read · Jun 17

--

https://aws.plainenglish.io/building-and-publishing-a-docker-image-to-amazon-ecr-using-bitbucket-pipelines-718e2f8bb3dd 14/15
17/10/2023, 12:12 Building and Publishing a Docker Image to Amazon ECR using Bitbucket Pipelines | by Gabriel Pulga | AWS in Plain English

Ritesh Thakur

Testing Docker images using Bitbucket Pipelines


I had recently come across a very interesting problem where we wanted to test our Docker
images before deploying them in AWS ECR. Even…

4 min read · Sep 8

--

See more recommendations

https://aws.plainenglish.io/building-and-publishing-a-docker-image-to-amazon-ecr-using-bitbucket-pipelines-718e2f8bb3dd 15/15

You might also like