SoFunction
Updated on 2025-04-14

How to deploy and run One API in a Docker environment

1. Pre-knowledge

Before entering specific operations, we need to know some background knowledge:

  1. Docker:Docker is an open source containerized platform that allows developers to package applications and their dependencies into a lightweight container, allowing applications to run consistently anywhere.
  2. Docker Compose:Docker Compose is a tool for defining and running multi-container Docker applications. Through Docker Compose file (usually named), multiple services can be defined in a file and started with a command.
  3. One API:One API is an API gateway platform that helps developers more easily manage API requests, providing unified API interfaces, request routing, and security management.

2. Use Docker Compose to deploy One API

In Docker Compose, we can define One API services and their related configurations. Here is a typical oneConfiguration:

one-api:
  container_name: one-api
  image: one-api:latest
  restart: always
  command: --log-dir /app/logs
  ports:
    - "3000:3000"
  volumes:
    - ./volumes/one-api/data:/data
    - ./volumes/one-api/logs:/app/logs
    - ./volumes/data-gym-cache:/tmp/data-gym-cache
  environment:
    - SQL_DSN=root:test2024@tcp(111.18.9.111:3306)/oneapi
    - SESSION_SECRET=random_string
    - TZ=Asia/Shanghai
  depends_on:
    mysql:
      condition: service_healthy
  healthcheck:
    test:
      [
        "CMD-SHELL",
        "wget -q -O - http://localhost:3000/api/status | grep -o '\"success\":\\s*true' | awk -F: '{print $2}'",
      ]
    interval: 30s
    timeout: 10s
    retries: 3

aboveThe file defines a name calledone-api, and its dependencies, ports, environment variables and other configuration items are set. Below we parse the contents of each part of the configuration file in detail.

  1. container_name: Set the container name toone-api, convenient to quickly identify and manage containers by name.

  2. image: Specifies the image used by the container.one-api:latest, the latest version of One API.

  3. restart: Set toalways, which means that no matter the reason for the container exit, Docker will automatically restart the container to ensure the continuous availability of the service.

  4. command: Pass the command--log-dir /app/logs, specify the log directory as/app/logs

  5. ports: Put the host's3000Port mapped to container3000port, so we can passhttp://localhost:3000Access the One API service.

  6. volumes: Define multiple data volumes to persist in storing data and logs:

    • ./volumes/one-api/data:/data: Put local./volumes/one-api/dataThe directory mounted to the container/dataDirectory, used to store persistent data.
    • ./volumes/one-api/logs:/app/logs: Mount the log file locally./volumes/one-api/logs, facilitate log management and analysis.
    • ./volumes/data-gym-cache:/tmp/data-gym-cache: Store cached data in./volumes/data-gym-cache
  7. environment: Several environment variables are defined:

    • SQL_DSN: DSN configuration used to connect to the database, formatted inuser:password@tcp(host:port)/dbname
    • SESSION_SECRET: Set the session key to encrypt and authenticate sessions.
    • TZ: The time zone is set toAsia/Shanghai, ensure that log time and other information meet the local time zone.
  8. depends_on: This configuration is used to declare dependencies between services. For example, the One API relies on a MySQL database.depends_onConfigure the MySQL database to be healthy before the One API is started.

  9. healthcheck: Defines the command and time interval for health checks:

    • test: Health check command, usewgetRequest the status interface of One API to detect whether the return result contains"success": true
    • intervaltimeoutandretriesSet the interval, timeout and number of retry times for health checks respectively.

Start the service

Run the following command to start the service:

docker-compose up -d

This command will start the service in the background and automatically according toConfiguration in the file to complete the initialization and startup of the One API container.

3. Use the Docker Run command to directly run the One API container

If you don't want to use Docker Compose, we can also usedocker runCommand to directly start the One API container:

docker run -d \
  --name one-api \
  --restart always \
  -p 3000:3000 \
  -v $(pwd)/volumes/one-api/data:/data \
  -v $(pwd)/volumes/one-api/logs:/app/logs \
  -v $(pwd)/volumes/data-gym-cache:/tmp/data-gym-cache \
  -e SQL_DSN=root:test2024@tcp(111.18.9.111:3306)/oneapi \
  -e SESSION_SECRET=random_string \
  -e TZ=Asia/Shanghai \
  --health-cmd="wget -q -O - http://localhost:3000/api/status | grep -o '\"success\":\\s*true' | awk -F: '{print $2}'" \
  --health-interval=30s \
  --health-timeout=10s \
  --health-retries=3 \
  one-api:latest \
  --log-dir /app/logs

Command parameter analysis

  1. -d: means running the container in the background.
  2. –name: The name of the specified container isone-api
  3. –restart always: Set the container to restart automatically.
  4. -p 3000:3000: Map ports, consistent with Docker Compose configuration.
  5. -v: Mount the volume, consistent with the volume configuration in Docker Compose.
  6. -e: Set environment variables.
  7. –health-cmd–health-interval–health-timeoutand–health-retries: Health check configuration.

This is the article about how to deploy and run One API in a Docker environment. For more related content on Docker deployment and running One API, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!