Deploy .net core API using docker compose - Part 1
Tags: dotnet, docker
Published: 2020-06-07 03:30:58

#### The main purpose of this post is to provide an easy example of how to deploy a docker image using docker compose.
#### This is part 1, we'll just build a docker image from a single application and deploy using docker compose.  

*You should know what is docker to understand this content, if you have no idea you can read [here](https://www.docker.com/resources/what-container)*

* Before we begin, make sure you have docker installed -> [docker](https://www.docker.com/products/docker-desktop)

### The application
The focus is not the application, so you may download the complete source code and I'll explain the container files.
* [Github project](https://github.com/rfa1989/DockerComposeProductApi/releases/tag/v1.0.0)

Alright!
You know what is a docker container and have the environment ready to test.  
So what's the deal with docker compose anyway?

Docker compose is the solution to deploy multiple containers.  
Example:

* You have an API that manage products.
* The API store products data into a database.
* You can use docker compose to deploy both the application and the database that the API depends on.

In order to deploy the application image using docker compose we need 3 things:
1. The application
2. A docker file
3. A docker compose file

# Docker file

This file is responsible for building and creating the application docker image.

```
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env
WORKDIR /app

RUN dotnet publish "DockerComposeProductApi.csproj" -c Release -o out
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env

COPY . ./
RUN dotnet restore "DockerComposeProductApi.csproj"
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1

WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "DockerComposeProductApi.dll"]
```

# Docker compose file

This file responsibility is to explain to docker which docker images we want to publish.  
We are also explaining docker that the image should be available in the port 5555.

```
version: '3.8'

services:
  dockercomposeproductapi:
    image: dockercomposeproductapi
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "5555:80"
    networks:
      - product-network

networks:
    product-network:
        driver: bridge
```

# Let's run the application  
To test this, open a terminal or command prompt and navigate to the application directory that contains the docker compose file (docker-compose.yml).

Just type:  
`docker-compose up -d`

Docker will build the image and start the application deployment.

To check where the application was deployed you can type:  
`docker-compose ps`

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/mr8606s4lgbvhpuqmr5r.png)  

You can see that the service is running and available in the port 5555.

To access the application browse to http://localhost:5555/api/products

The result:  
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/tfezhops5qq3i5wcldu7.png)

### Conclusion:  
Now you have the application running on docker, in the next part, we're going to connect our application to a database, and update the docker compose file to deploy both the application image and the database software image.