Overview

Deploying a Docker application in production using the docker/build-push-action involves creating necessary artifacts, configuring services, and ensuring a reliable build and deployment process. The following guide delineates these steps, focusing on TypeScript and Go projects.

Step 1: Create the Dockerfile

Start with crafting a Dockerfile to encapsulate the application logic. For illustration, below is a basic Dockerfile that builds an Alpine-based image:

# syntax=docker/dockerfile:1
FROM alpine
RUN echo "Hello world!"

This simple Dockerfile serves as the foundation for further enhancements.

Step 2: Define the Docker Compose File

To streamline the deployment of services, define a docker-compose.yml file. It helps in orchestrating multiple containers, setting up networking, and configuring volumes. Below is a sample configuration that includes a Nexus repository as a service:

services:
  nexus:
    image: sonatype/nexus3:${NEXUS_VERSION:-latest}
    volumes:
      - "./data:/nexus-data"
    ports:
      - "8081:8081"
      - "8082:8082"

In this file:

  • The Nexus service is defined with the option to use a specific version or default to the latest.
  • Persistent data is stored in the ./data directory.
  • Ports 8081 and 8082 are mapped for access.

Step 3: Build and Push the Docker Image

In your CI/CD pipeline, utilize the docker/build-push-action to build and push your Docker image. The following example demonstrates how to setup a GitHub Action for this purpose:

name: Build and Push Docker Image

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Build and push
        uses: docker/build-push-action@v2
        with:
          context: .
          file: ./Dockerfile
          push: true
          tags: user/app:${{ github.sha }}

Here, the action performs the following:

  • It triggers on push events to the main branch.
  • The docker/build-push-action builds the Docker image and pushes it to a specified registry, tagging it with the commit SHA for versioning.

Step 4: Build Go Project

For Go projects, ensure the Go module is properly configured. The following demonstrates how to build a Go application located in the specified module directory:

module github.com/docker/build-push-action/test/go

go 1.16

require (
    // Add any dependencies here
)

Use standard build commands without vendoring:

go build

This compiles the application inside the module without the use of -mod=vendor.

Step 5: Deploy in Production

Deploy the application using Docker Compose. Simply execute the following command within the directory containing the docker-compose.yml file:

docker-compose up -d

This command starts the Nexus service in detached mode. You can verify the deployment by checking the service logs:

docker-compose logs nexus

Through this sequence, the application is successfully built, pushed, and deployed in a production environment. Adjustments and refinements may be necessary based on specific requirements and environments, always ensuring adherence to best practices for security and reliability.

Reference

  • Source data and code snippets used in this guide stem from the configurations provided above.