# Deploying a .NET Application to DigitalOcean Using Docker Containers and Container Registry

Deploying a .NET application to a cloud environment like DigitalOcean is a powerful way to ensure scalability, reliability, and ease of management. In this post, we’ll walk through deploying a real-world .NET application—a simple ASP.NET Core MVC app—to DigitalOcean using Docker containers and its container registry.

---

### Prerequisites

Before we begin, ensure you have the following:

1. **DigitalOcean Account**: Sign up at [DigitalOcean](https://www.digitalocean.com/).
    
2. **Docker Installed**: Ensure Docker is installed and running on your local machine. Download it [here](https://www.docker.com/get-started).
    
3. **dotnet CLI**: Install the .NET SDK from [dotnet.microsoft.com](https://dotnet.microsoft.com/).
    
4. **DigitalOcean CLI (doctl)**: Install `doctl` for managing DigitalOcean resources from your terminal. Follow the [installation guide](https://docs.digitalocean.com/reference/doctl/how-to/install/).
    
5. **A Sample .NET Project**: Clone the sample app:
    
    ```bash
    git clone https://github.com/dotnet-architecture/eShopOnWeb.git
    cd eShopOnWeb
    ```
    

---

### Step 1: Build and Containerize the .NET Application

#### **1.1 Create a Dockerfile**

Navigate to the root directory of the sample app and create a `Dockerfile`:

```dockerfile
# Use the official ASP.NET runtime image
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 5000

# Use the .NET SDK image for building the app
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["src/Web/Web.csproj", "src/Web/"]
RUN dotnet restore "src/Web/Web.csproj"
COPY . .
WORKDIR "/src/src/Web"
RUN dotnet build "Web.csproj" -c Release -o /app/build

# Publish the app
FROM build AS publish
RUN dotnet publish "Web.csproj" -c Release -o /app/publish /p:UseAppHost=false

# Create the final runtime image
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Web.dll"]
```

#### **1.2 Build the Docker Image**

Build the Docker image for your app:

```bash
docker build -t eshoponweb:latest .
```

#### **1.3 Test Locally**

Run the container locally to ensure it works:

```bash
docker run -p 5000:5000 eshoponweb:latest
```

Visit `http://localhost:5000` in your browser to verify the app is running.

---

### Step 2: Push the Image to DigitalOcean Container Registry

#### **2.1 Authenticate with DigitalOcean**

Log in to your DigitalOcean account using the `doctl` CLI:

```bash
doctl auth init
```

#### **2.2 Create a Container Registry**

Create a new container registry in DigitalOcean:

```bash
doctl registry create my-registry
```

Retrieve the registry URL:

```bash
doctl registry get
```

Example output:

```plaintext
Name:              my-registry
Endpoint:          registry.digitalocean.com/my-registry
```

#### **2.3 Tag and Push the Docker Image**

Tag your image for the DigitalOcean registry:

```bash
docker tag eshoponweb:latest registry.digitalocean.com/my-registry/eshoponweb:latest
```

Push the image:

```bash
docker push registry.digitalocean.com/my-registry/eshoponweb:latest
```

Verify the image was pushed successfully:

```bash
doctl registry repository list my-registry
```

---

### Step 3: Deploy the App to a DigitalOcean Droplet

#### **3.1 Create a Droplet**

Spin up a new DigitalOcean droplet using the Docker image:

```bash
doctl compute droplet create \
  my-droplet \
  --region nyc3 \
  --image docker-20-04 \
  --size s-1vcpu-1gb \
  --ssh-keys <your-ssh-key-id>
```

Retrieve the droplet’s IP address:

```bash
doctl compute droplet list
```

#### **3.2 SSH Into the Droplet**

SSH into the newly created droplet:

```bash
ssh root@<droplet-ip>
```

#### **3.3 Pull and Run the Docker Container**

1. Log in to the DigitalOcean registry from your droplet:
    
    ```bash
    docker login registry.digitalocean.com
    ```
    
    Enter your DigitalOcean personal access token when prompted.
    
2. Pull the container image:
    
    ```bash
    docker pull registry.digitalocean.com/my-registry/eshoponweb:latest
    ```
    
3. Run the container:
    
    ```bash
    docker run -d -p 80:5000 registry.digitalocean.com/my-registry/eshoponweb:latest
    ```
    

The app will now be accessible at `http://<droplet-ip>`.

---

### Step 4: Automate Deployment with Docker Compose (Optional)

To manage your app and any dependencies, use a `docker-compose.yml` file:

```yaml
version: '3.8'

services:
  eshoponweb:
    image: registry.digitalocean.com/my-registry/eshoponweb:latest
    ports:
      - "80:5000"
    environment:
      - DOTNET_ENVIRONMENT=Production
```

Deploy with Docker Compose:

```bash
docker-compose up -d
```

---

### Conclusion

In this tutorial, we containerized a .NET app, pushed it to DigitalOcean’s container registry, and deployed it on a droplet. This setup ensures your app is production-ready and easily scalable. For advanced setups, consider adding CI/CD pipelines and monitoring tools.

Happy deploying!
