To set up Docker within a development environment, you will leverage the docker/build-push-action GitHub Action combined with Docker Compose and a Dockerfile. Below are the steps with detailed code examples for different configurations and setups.
Dockerfile
The Dockerfile specifies the image that will be built and is crucial for defining the environment your application will run in. Below is an example Dockerfile created using TypeScript.
# syntax=docker/dockerfile:1
FROM alpine
# Install necessary packages for the application
RUN apk add --no-cache nodejs npm
# Add application code
COPY . /app
# Set working directory
WORKDIR /app
# Install dependencies
RUN npm install
# Start the application
CMD ["npm", "start"]
This example uses Alpine Linux as a lightweight base image and installs Node.js and npm. Replace the npm
commands with those corresponding to your TypeScript application as necessary.
Docker Compose Configuration
Using Docker Compose allows for defining and running multi-container Docker applications easily. Below is an example of a docker-compose.yml
file that includes a service for a Nexus repository.
version: '3.8'
services:
nexus:
image: sonatype/nexus3:${NEXUS_VERSION:-latest}
volumes:
- "./data:/nexus-data"
ports:
- "8081:8081"
- "8082:8082"
In this configuration:
- The service
nexus
uses thesonatype/nexus3
image. - It mounts a local directory
./data
to the container’s data directory, enabling persistent storage of the Nexus data. - Ports
8081
and8082
are exposed for accessing the Nexus repository.
Building and Pushing Docker Images with GitHub Actions
You may want to automate the building and pushing of Docker images as part of your CI/CD workflow. Below is a sample configuration utilizing the docker/build-push-action in a GitHub Actions workflow file.
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: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Build and push
uses: docker/build-push-action@v2
with:
context: .
file: ./Dockerfile
push: true
tags: yourdockerhubusername/your-image-name:latest
In this workflow:
- The Docker context is set to the root of the repository, where the Dockerfile exists.
- The built image will be tagged and pushed to Docker Hub upon every push to the main branch.
Using Go in the Development Environment
When incorporating Go into your development process, the following snippet shows how to build a Go application. Here’s an example Dockerfile that builds a Go application specifically using the Go module named “github.com/docker/build-push-action/test/go”.
# syntax=docker/dockerfile:1
FROM golang:1.17 AS builder
WORKDIR /app
# Copy the Go module files and download dependencies
COPY go.mod go.sum ./
RUN go mod download
# Copy the rest of the application code
COPY . .
# Build the application
RUN go build -o myapp
# Create a smaller image to run the application
FROM alpine
WORKDIR /root/
# Copy the compiled binary from the builder stage
COPY --from=builder /app/myapp .
# Run the application
CMD ["./myapp"]
In this Dockerfile:
- A multi-stage build is used to create a smaller final image.
- The Go application is built in a build stage, and then the binary is copied to a minimal Alpine Linux image to keep the final image lightweight.
All examples presented focus on achieving an optimized environment for development purposes, enabling efficient workflows while utilizing Docker effectively.
Source: The code is written in TypeScript, Dockerfile, HCL, Go. The Golang Module name is “github.com/docker/build-push-action/test/go” which impacts the Go build output. Never use “-mod=vendor”.