Example Deployment Workflow Using Docker



Set Up the Project

Define Dockerfiles for each service in your application (e.g., web app, database).

Write Docker Compose files (e.g., 'docker-compose.yml') if youre working with multiple services that need to be managed together.

Example Dockerfile for a Spring Boot application

# Start with an official Java runtime as the base image
FROM openjdk:17-jdk-alpine

# Set the working directory
WORKDIR /app

# Copy the application jar file into the image
COPY target/myapp.jar myapp.jar

# Expose the port the app will run on
EXPOSE 8080

# Run the application
ENTRYPOINT ["java", "-jar", "myapp.jar"]

Build Docker Images

docker build -t myapp:latest .

Tag images properly to manage different versions in registries (e.g., 'myapp:v1.0').

Run Local Tests in Containers

Use Docker Compose or individual Docker containers to run tests in an isolated environment.

docker-compose up -d
docker-compose exec webapp ./run-tests.sh

Ensure all services are running and passing their tests.

Push Images to a Docker Registry

Push your Docker images to a container registry (e.g., Docker Hub, Amazon ECR, or a private registry) to make them accessible to the deployment environment.

Log in and push images−

docker login -u username -p password
docker tag myapp:latest username/myapp:v1.0
docker push username/myapp:v1.0

Deploy to a Staging Environment

Pull images from the registry onto the staging server−

docker pull username/myapp:v1.0

Deploy using Docker Compose or Kubernetes, depending on the staging environment setup.

Run any additional integration or acceptance tests in staging.

Monitor Logs and Metrics

Use Docker commands to check logs and application health.

docker logs -f container_name

If using monitoring tools (e.g., Grafana, Prometheus), confirm that the application metrics are within acceptable thresholds.

Approve and Deploy to Production

Once tests pass in staging, deploy the images to the production environment by pulling them from the registry.

Run Docker Compose or Kubernetes commands to start services in production−

docker pull username/myapp:v1.0
docker run -d -p 8080:8080 username/myapp:v1.0

Implement Rollback Mechanism

Ensure that you have the previous version's image stored. If issues arise, re-deploy the last stable version−

docker pull username/myapp:v0.9
docker run -d -p 8080:8080 username/myapp:v0.9

Automate Workflow with CI/CD Pipeline

Set up a CI/CD pipeline (e.g., GitHub Actions, GitLab CI, Jenkins) that automates this workflow, including build, test, push, and deployment stages.

Example of a CI/CD pipeline script (GitHub Actions)−

name: Docker CI/CD

on:
  push:
    branches:
      - main

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      
      - name: Build Docker image
        run: docker build -t username/myapp:${{ github.sha }} .
      
      - name: Log in to Docker Hub
        run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin

Summary

This workflow automates the build, test, and deployment stages for a Dockerized application, providing both local and remote testing, a rollback strategy, and an automated CI/CD pipeline to streamline the process.

Advertisements