Module 7 Lesson 2: Pushing Images to Registries
·DevOps

Module 7 Lesson 2: Pushing Images to Registries

The full lifecycle. Learn how to log in to Docker Hub or AWS ECR and push your built images so they can be used by your production servers.

Module 7 Lesson 2: Pushing Images to Registries

Building an image is only half the job. To deploy it, you must "Push" it to a Registry (like Docker Hub, AWS ECR, or GitLab's own registry).

1. The "Login" Step

Before you can push, you must prove who you are. DO NOT type your password in the script. Use variables!

build-and-push:
  image: docker:latest
  services: [docker:dind]
  script:
    # 1. Login
    - echo "$DOCKER_HUB_PASSWORD" | docker login -u "$DOCKER_HUB_USER" --password-stdin
    # 2. Build
    - docker build -t my-user/my-app:latest .
    # 3. Push
    - docker push my-user/my-app:latest

2. Smart Tagging (The Versioning Secret)

Don't just use :latest. If you push a broken build to :latest, your production server will pull it and crash. Use the Git Commit Hash.

  script:
    - IMAGE_NAME="my-user/my-app:$CI_COMMIT_SHORT_SHA"
    - docker build -t $IMAGE_NAME .
    - docker push $IMAGE_NAME

Benefit: You can now go back to any specific version of your app in 5 seconds.


Visualizing the Process

graph TD
    Start[Input] --> Process[Processing]
    Process --> Decision{Check}
    Decision -->|Success| End[Complete]
    Decision -->|Retry| Process

3. Handling Multi-Platform Builds

If your production server is an "ARM" processor (like AWS Graviton) but your runner is "AMD64," you need docker buildx.

  • Buildx allows you to build a single image that works on both architectures!

4. Why Use a Private Registry?

For business code, you don't want your images to be public on Docker Hub. You use a Private Registry. This ensures that only your trusted servers can pull the code.


Exercise: The Registry Relay

  1. Create a free Docker Hub account.
  2. Add your credentials to your GitLab project variables.
  3. Write a pipeline that builds an image and tags it with $CI_COMMIT_BRANCH.
  4. Push it to Docker Hub. Verify the image appears on the Docker Hub website.
  5. What happens if you try to docker push WITHOUT doing a docker login first?
  6. Research: What is the "Registry 429 Too Many Requests" error? How does authentication help avoid it?

Summary

Pushing images is the "Delivery" part of Continuous Delivery. By automating the login, build, and push process, you ensure that your production infrastructure always has access to the most recent, tested, and secure versions of your application.

Next Lesson: Home grown: Using the GitLab Container Registry.

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn