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
- Before we begin, make sure you have docker installed -> docker
The application
The focus is not the application, so you may download the complete source code and I'll explain the container files.
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:
- The application
- A docker file
- 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
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
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.
Top comments (0)