# Add a service

Source: https://openpanel.sh/docs/projects/add-service

Add a service to a project from a Docker image or a Git repository, choose its servers, and deploy it.

A service is made with its settings and nothing runs until its first deploy. You need deploy access to the project, and at least one [connected server](/docs/servers) for the service to run on.

## Find your server [#find-your-server]

A service runs only on the servers you choose for it. List yours to get their names and ids:

<Tabs items={["Console","CLI","API"]}>
<Tab value="Console">
In **New service**, **Servers it may run on** lists the organization's servers with the state of each one's agent. When the organization has one server and its agent is online, it is ticked for you.
</Tab>

<Tab value="CLI">
```sh
openpanel servers
```

The CLI takes a server's name, its address or its id.
</Tab>

<Tab value="API">
```sh
curl https://openpanel.sh/api/v1/servers \
  -H "Authorization: Bearer $OPENPANEL_TOKEN"
```

The API takes servers by id, in `serverIds`.
</Tab>
</Tabs>

## From a Docker image [#from-a-docker-image]

<Tabs items={["Console","CLI","API"]}>
<Tab value="Console">
Open the project and click **New service**. Choose **A Docker image**, type the image, such as `nginx:1.27`, set its ports under **Networking**, tick a server, and click **Create service**.
</Tab>

<Tab value="CLI">
```sh
openpanel services create web --project shop \
  --image nginx:1.27 --port http:80 --public --server my-server
```
</Tab>

<Tab value="API">
```sh
curl -X POST https://openpanel.sh/api/v1/projects/$PROJECT_ID/services \
  -H "Authorization: Bearer $OPENPANEL_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "web",
    "source": { "kind": "image", "image": "nginx", "tag": "1.27" },
    "exposure": "public",
    "ports": [{ "protocol": "http", "port": 80 }],
    "serverIds": ["'"$SERVER_ID"'"]
  }'
```
</Tab>
</Tabs>

An image without a registry in its name comes from Docker Hub, and one without a tag uses `latest`. See [Deploy an image](/docs/deploy/images).

## From a Git repository [#from-a-git-repository]

The repository must be reachable through one of your organization's [Git accounts](/docs/git). `openpanel git` lists them with their ids.

<Tabs items={["Console","CLI","API"]}>
<Tab value="Console">
Open the project and click **New service**. Choose **A Git repository**, then the **Git account**, the **Repository**, the **Branch** and, for a monorepo, the **Folder**. Under **Build and start**, keep **Auto** or choose **Dockerfile**. Tick a server and click **Create service**.
</Tab>

<Tab value="CLI">
```sh
openpanel services create api --project shop \
  --repo acme/shop --git-account $GIT_ACCOUNT_ID --branch main \
  --port http:3000 --public --server my-server
```

Add `--path apps/api` to build one folder of a monorepo, and `--dockerfile <path>` to build with a Dockerfile you name.
</Tab>

<Tab value="API">
```sh
curl -X POST https://openpanel.sh/api/v1/projects/$PROJECT_ID/services \
  -H "Authorization: Bearer $OPENPANEL_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "api",
    "source": { "kind": "git", "accountId": "'"$GIT_ACCOUNT_ID"'", "repo": "acme/shop", "branch": "main" },
    "exposure": "public",
    "ports": [{ "protocol": "http", "port": 3000 }],
    "serverIds": ["'"$SERVER_ID"'"]
  }'
```

Add `"path": "apps/api"` to `source` for one folder of a monorepo, and `"build": { "kind": "dockerfile", "dockerfile": "Dockerfile" }` to build with a Dockerfile you name.
</Tab>
</Tabs>

The branch defaults to `main`. At each deploy the repository is built on one of your servers. See [Deploy from Git](/docs/deploy/git) and [How builds work](/docs/builds).

A service from a template cannot be deployed yet: the template catalogue has not shipped.

## What a new service starts with [#what-a-new-service-starts-with]

Settings you leave out take these values. Each one can be changed later; see [Service settings](/docs/projects/settings).

| Setting | Default |
|---|---|
| Address | **None**: nobody from outside reaches it |
| Ports | None |
| Replicas | 1, with the load balancer off |
| Servers | None, so it cannot be deployed until you choose one |
| Start command | The image's own |
| Health check | None |
| Resources | No limits |
| Environment | The project's production environment |

A public service with an HTTP port gets its [opnl.app address](/docs/domains/opnl-app) as soon as it is saved. `openpanel domains --project shop` shows it.

## Deploy it [#deploy-it]

<Tabs items={["Console","CLI","API"]}>
<Tab value="Console">
Leave **Deploy after creating** ticked in **New service**, and the service deploys as soon as it is made. Later, click **Deploy** on the service's **Overview** tab.
</Tab>

<Tab value="CLI">
```sh
openpanel deploy web --project shop --wait
```

`--wait` follows the deployment until it runs or fails.
</Tab>

<Tab value="API">
```sh
curl -X POST https://openpanel.sh/api/v1/services/$SERVICE_ID/deployments \
  -H "Authorization: Bearer $OPENPANEL_TOKEN"
```
</Tab>
</Tabs>

See [Deploy](/docs/deploy) for what happens next.

## What can go wrong [#what-can-go-wrong]

| Message | What it means |
|---|---|
| "A service name is up to 32 lowercase letters, digits and hyphens, starting with a letter." | Choose another name, such as `web` or `api-v2`. |
| "A service named web is already in this environment." | Names are unique in an environment. |
| "A service named web is in production. A project's environments share one private network for now, …" | A name is used once in a project, across its environments; add the environment to it, such as `web-staging`. |
| "Enter an image such as nginx, bitnami/redis or ghcr.io/org/app." | The image name has characters Docker does not allow. Put the tag after a colon. |
| "That Git account was not found." | The account id is wrong, or it is another member's own Git account. |
| "Choose at least one server for this service before deploying." | Add a server with `--server` or `serverIds`. |

<Cards>
<Card href="/docs/projects/settings" title="Service settings" description="Every setting, its limits and the rules between them." />

<Card href="/docs/deploy" title="Deploy" description="Deploy, follow a deployment and roll back." />
</Cards>