DEV Community

Elegberun Olugbenga
Elegberun Olugbenga

Posted on

How To Set Up An Asp.NetCore Api, and MySQL with Docker Compose

In this blog post, I am going to be going through how to containerize both your.NetCore Api and your MySql Database and connect both together using a docker-compose file. If you are new to Docker you can check out my two previous posts. containerize-an-aspnet-core-application-with-docker and demystifying-docker.

Building the API

The API for the tutorial can be found on GitHub here. If You clone the Url and run a dotnet restore you will have the API I built for this Tutorial. It is essentially the skeletal project when you create a.NetCore WebApi project with a few changes which I will be going through here.

I added the UserController and created two endpoints the GetAllUsers to retrieve users and the AddNewUser Endpoint to add a new user; I used Dapper as my ORM to communicate with the Database. For clarity, I implemented the Database queries in the controller class just to keep it simple. As you can see the UserController is nothing too fancy.

Image..........

I also added swagger to the startup class to help create a faster way to test the API directly from our browser.

CONFIGURATION
When connecting to any Database management system you need to need to specify a user, a password, the server address, and the database name and that's exactly what I am doing in this section here. The configuration environment variables will be supplied in our docker-compose when it is initialized, and if there are not config files from the docker-compose file it checks the app settings connection string and gets its config files.

Image............

  1. DOCKER

In order to follow the next steps, you need to have installed Docker on your PC. The instructions for the most popular OSs can be found here: Ubuntu, Windows, Mac.

CREATING THE DOCKERFILE
This is a text document that is used by Docker to build a custom image for your Web API. I touched a lot more on this [here].

IMAGE..........

first, it tells Docker to pull the asp.net image
which to /app as a working directory in docker and listen to traffic from port 80 on TCP.

The rest of the steps builds and publishes the app and then copy the published app to the /app/publish folder in the Docker container. If you have the Docker Extension Installed VSCode can automatically generate it for you.

Docker Compose File
The Compose file provides a way to document and configure all of the application's service dependencies (databases, web service APIs, etc). This is the Docker Compose file for this project. I have added comments on every line to explain what each line does. When it comes to docker-compose files lines and spacing is crucial; you have to watch out for that

IMAGE............

Version----Refers to the version of the docker-compose.
Volumes----Because containers are designed to be stateless, once a container stops running all data stored in that container is not saved and we definitely do not want that for our database container. In order for your data to be persisted after the container has stopped or been destroyed it is essential that you persist your data somewhere outside the container but provide a reference to that path to docker. We specify our volume name called datafile.
Services-----This is where we define the application dependencies. In this application, we have an API and a database.
DataBase service.
We define the database image version we want so docker fetches that for us.
port---then we tell docker to map port 3306 from the docker container to the host machine.
volumes----here we define our volumes.datafiles should be stored at this path but that path is actually a reference path to somewhere in our computer that docker has access to.
The second volume is our SQL script. Supposing you wanted to create a new Table or seed records to the database on initialization you could do it here. Here I created a new Table called Users on Usersdb.
IMAGE..............

this line can be interpreted as
code line...........
COPY the setup.sql file, located in sql-scripts, to the /docker-entrypoint-initdb.d/ folder located in MySQL Docker image that we’re using. By default, all scripts located in this folder will be automatically run during container startup.

environment variables.
IMAGE.......
Remember those config keys we talked about earlier...You specify them here.
MYSQL_ROOT_PASSWORD: assigns a root password to the root user
MYSQL_USER: created a new user
MYSQL_PASSWORD: New users password
MYSQL_DATABASE: creates a new database.
By default, the new user created will have admin rights to the database specified here.

To wrap up the Database service it pulls the MySQL from docker hub maps the ports create a folder to store volumes and then creates a new user for the database.

API Service
build: This means the build the docker file specified in this context.
depends_on: it depends on the database service defined above hereby linking the two containers.
ports: map port 5000 in the docker container to port 80 on the host machine.
restart: specifies how often containers should restart.

environment: You also specify that the DBHOST should be the database service.

Now that we have gone through the docker file and the docker-compose. All we need to do is cd into the project and run this command.
comand......
IMAGE......

As you can see all the steps we defined are being run.

If you want it detached you can add the -d file.

run a docker ps -a to see all the containers...See our two containers are running.

Navigate to this URL...Add a few users refresh the page...You will see that the users persisted into the db.

(IMAGE)
(IMAGE)

If you want to check the database container from the command line you can run this command
command...

Input your password and check out your data.

if you run a

command....docker container inspect testapi_database_1

Scroll down you could see all the volumes you created.

If you get this error when running the docker-compose file. It is because docker does not have access to that folder in your computer you can give it access by navigating to your docker desktop dashboard --------->Setting-------->Resources------>FileSharing and then add your own path.

Summary
In this tutorial, we have
Created our API.
Added a docker file to publish the API to its container. Created a docker-compose file to create our MySql container and then linked both containers.

Thank you for reading!!!

Discussion (0)