1. Pre-knowledge
Before entering specific operations, we need to know some background knowledge:
- 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.
-
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.
- 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 called
one-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.
container_name: Set the container name to
one-api
, convenient to quickly identify and manage containers by name.image: Specifies the image used by the container.
one-api:latest
, the latest version of One API.restart: Set to
always
, 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.command: Pass the command
--log-dir /app/logs
, specify the log directory as/app/logs
。ports: Put the host's
3000
Port mapped to container3000
port, so we can passhttp://localhost:3000
Access the One API service.-
volumes: Define multiple data volumes to persist in storing data and logs:
-
./volumes/one-api/data:/data
: Put local./volumes/one-api/data
The directory mounted to the container/data
Directory, 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
。
-
-
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.
-
depends_on: This configuration is used to declare dependencies between services. For example, the One API relies on a MySQL database.
depends_on
Configure the MySQL database to be healthy before the One API is started.-
healthcheck: Defines the command and time interval for health checks:
-
test
: Health check command, usewget
Request the status interface of One API to detect whether the return result contains"success": true
。 -
interval
、timeout
andretries
Set 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 run
Command 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
- -d: means running the container in the background.
-
–name: The name of the specified container is
one-api
。 - –restart always: Set the container to restart automatically.
- -p 3000:3000: Map ports, consistent with Docker Compose configuration.
- -v: Mount the volume, consistent with the volume configuration in Docker Compose.
- -e: Set environment variables.
- –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!