Preparing the Environment

Ensure you have Docker installed on the production server. You can verify Docker installation with:

docker --version

Cloning the Repository

Clone the docker/go-events repository from the official source:

git clone https://github.com/docker/go-events.git
cd go-events

Building the Docker Image

Navigate to the project directory and build the Docker image using the provided Dockerfile. Replace your_image_name with the desired name for your image.

docker build -t your_image_name .

Configuring Environment Variables

For production deployment, configure the required environment variables. These can be defined in a .env file or passed directly in the Docker command. Here’s an example .env file:

EVENTS_API_URL=https://api.yourdomain.com/events
DATABASE_URL=postgres://user:password@hostname:5432/dbname

Make sure to replace the placeholders with actual values relevant to your application.

Running the Docker Container

Run the container in detached mode while using the configured environment variables. Use the following command:

docker run -d \
  --name go-events \
  --env-file .env \
  -p 8080:8080 \
  your_image_name

In this command:

  • -d runs the container in detached mode.
  • --name specifies a name for your container.
  • --env-file allows Docker to read environment variable configurations.
  • -p maps port 8080 of the container to port 8080 on the host.

Configuring Networking and Load Balancers

In a production environment, it’s common to use load balancers to manage incoming traffic. Configure your load balancer to forward traffic to the internal address of your Docker container. When running multiple instances, keep the health checks in mind.

Monitoring and Logging

Set up logging and monitoring to track the health and performance of your application. You can use Docker logging drivers or external tools. For example, to use Fluentd for logging, configure the logging driver as follows:

docker run -d \
  --name go-events \
  --log-driver=fluentd \
  --env-file .env \
  -p 8080:8080 \
  your_image_name

Scalability

Consider deploying multiple instances of the container for scaling. Use Docker Compose or Kubernetes for orchestration. For example, in a Docker Compose file, you can define multiple replicas:

version: '3'

services:
  go-events:
    image: your_image_name
    deploy:
      replicas: 3
    environment:
      - EVENTS_API_URL=https://api.yourdomain.com/events
      - DATABASE_URL=postgres://user:password@hostname:5432/dbname
    ports:
      - "8080:8080"

To deploy, simply run:

docker-compose up -d

Database Migration

Before starting the application, ensure that the database schema is updated. If you are following a migration strategy, use a migration tool compatible with your database. This can typically involve running:

go run ./migrate/main.go

Replace the path to your migration tool as necessary.

Security Best Practices

  1. Network Security: Ensure that your Docker containers are running in an isolated network.
  2. Environment Variables: Keep sensitive environment variables secure and avoid hardcoding them into the image.
  3. Updates: Regularly update your Docker images to include security patches.

Conclusion

Following these steps ensures a robust production deployment for docker/go-events. Each command and configuration suggested should be adapted as necessary to suit specific production environments and requirements.

For further details, refer to the original sources of this documentation.