# Deploy The Portainer Agent

There are multiple ways to deploy the Portainer Agent. [Deploying the Agent as a stack](/portainer/the-portainer-agent/the-portainer-agent.md#deploy-as-a-stack) is the simplest method, however you can also [install the Agent manually](/portainer/the-portainer-agent/the-portainer-agent.md#deploy-manually) if desired. [Connecting your Portainer instance to the Agent](/portainer/the-portainer-agent/the-portainer-agent.md#connecting-your-portainer-instance-to-the-agent) is required in order to make use of the Agent's features.

## Deploy as a Stack

The **simplest** way to deploy the Agent is to use the Stacks feature of the the Portainer UI.&#x20;

**Within the web editor of Portainer's Stacks feature you can copy in the contents of the below stack file:**

```yaml
version: '3.2'

services:
  agent:
    image: portainer/agent
    environment:
      AGENT_CLUSTER_ADDR: tasks.agent
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - /var/lib/docker/volumes:/var/lib/docker/volumes
    ports:
      - target: 9001
        published: 9001
        protocol: tcp
        mode: host
    networks:
      - portainer_agent
    deploy:
      mode: global
      placement:
        constraints: [node.platform.os == linux]

networks:
  portainer_agent:
    driver: overlay
    attachable: true
```

**Alternatively, you can download this same stack file to your computer and upload it to Portainer when creating the Agent.**

{% file src="/files/-LaCrEfDqlCMmjqo8ioV" %}
Agent Stack File
{% endfile %}

The file can be named whatever you wish as long as it has the .yml file extension.

{% hint style="danger" %}
**Note:** Portainer needs to be [connected to the Agent](/portainer/the-portainer-agent/the-portainer-agent.md#connecting-your-portainer-instance-to-the-agent) before you can make use of the Agents features.
{% endhint %}

## Deploy Manually

{% hint style="warning" %}
**Note**: This tutorial requires that you execute all commands on a manager node in your swarm.
{% endhint %}

**Step 1:** Create a new overlay network in your swarm cluster for the Agent by entering the following command:

```
docker network create --driver overlay --attachable portainer_agent_network 
```

**Step 2:** Instructions for step 2 are **specific to your operating system.** Users running Windows Server follow the special instructions for Windows Server

{% tabs %}
{% tab title="Deploy Steps" %}
Enter the following command in your terminal to deploy the Agent as a global service in your cluster (connected to the overlay network you just created).

```
docker service create \
    --name portainer_agent \
    --network portainer_agent_network \
    -e AGENT_CLUSTER_ADDR=tasks.portainer_agent \
    --mode global \
    --constraint 'node.platform.os == linux' \
    --publish published=9001,target=9001,mode=host \
    --mount type=bind,src=//var/run/docker.sock,dst=/var/run/docker.sock \
    --mount type=bind,src=//var/lib/docker/volumes,dst=/var/lib/docker/volumes \
    portainer/agent
```

{% endtab %}

{% tab title="Special Instructions for Windows Server" %}
Because of a Docker limitation you need to deploy the Agent to all Windows Server nodes by running following command on each of the nodes in your cluster.

```
docker run -d --name portainer_agent --restart always \
    --network portainer_agent_network -e AGENT_CLUSTER_ADDR=tasks.portainer_agent \
    --mount type=npipe,source=\.\pipe\docker_engine,target=\.\pipe\docker_engine \
    portainer/agent:windows1803-amd64
```

{% hint style="warning" %}
**Note**: If you’re using Windows server 1803, you might need to open up DNS ports to support the DNS resolution of tasks.portainer\_agent. See: <https://success.docker.com/article/swarm-internal-dns-is-inaccessible-on-windows-server-1803>
{% endhint %}
{% endtab %}
{% endtabs %}

{% hint style="danger" %}
**Note:** Portainer needs to be [connected to the Agent](/portainer/the-portainer-agent/the-portainer-agent.md#connecting-your-portainer-instance-to-the-agent) before you can make use of the Agents features.
{% endhint %}

## Connecting Your Portainer Instance to The Agent

You need to add the Agent as an **endpoint** inside of Portainer before you can use the Agent's features.

**Step 1:** Navigate to the Endpoints view of the Portainer UI and click the Add Endpoint button.

![](/files/-LaDcjRISvFuCUzG2hm2)

**Step 2:** Click on the Agent Tab at the top of the page, fill in a name for your endpoint and the endpoint URL and click the Add Endpoint button.

{% hint style="danger" %}
**Note:** You will need to replace **10.0.7.10** with the IP of the node you have the Agent running on.
{% endhint %}

![](/files/-LaDeBg8I9Da6B2kc1oB)

**You should now see a green message such as the one below appear in the top right of the screen. This indicates the Agent has been successfully added as an endpoint.**

![](/files/-LaDe_x1w18t--0o_G1T)

**You should also now have your Agent endpoint available to select & manage in the Home view of Portainer such as below.**

![](/files/-LaDf6axPmE-hxnPgiYU)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://portainer.gitbook.io/portainer/the-portainer-agent/the-portainer-agent.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
