Hey I am fairly new to docker and am trying to create a C# api using aspnet core and mysql with several users when using docker compose up. To do this I have created a shell script that runs upon entry.
docker-compose.yaml
volumes:
webshopdata:
networks:
webshopnet:
services:
webshopdb:
image: mysql:latest
container_name: ${MYSQL_SERVER_NAME}
environment:
- MYSQL_USER=${MYSQL_USER}
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
- MYSQL_DATABASE=${MYSQL_DATABASE}
volumes:
- webshopdata:/var/lib/mysql
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
- ./grant_privileges.sql:/docker-entrypoint-initdb.d/grant_privileges.sql
- ./wait_until.sh:/docker-entrypoint-initdb.d/wait_until.sh
- ./init.sh:/docker-entrypoint-initdb.d/init.sh
entrypoint: ["/docker-entrypoint-initdb.d/init.sh", "${MYSQL_USER}", "${MYSQL_ROOT_PASSWORD}", "${MYSQL_DATABASE}"]
networks:
- webshopnet
webshopapi:
build: .
container_name: webshopapi
environment:
- MYSQL_SERVER_NAME=${MYSQL_SERVER_NAME}
env_file:
- ./secrets.env
- asp.env
- .env
ports:
- 8000:80
- 8001:443
networks:
- webshopnet
volumes:
- ./certs:/https
- ./webshop:/App
depends_on:
- webshopdb
Dockerfile
# Use the SDK image for building the application
FROM mcr.microsoft.com/dotnet/sdk AS build_env
WORKDIR /App
# Copy the entire source tree to the Docker image
COPY WebShop.csproj .
# Restore dependencies
RUN dotnet restore WebShop.csproj
COPY . .
# Publish the application
RUN dotnet publish WebShop.csproj -c Release -o out
# Use the ASP.NET runtime image for running the application
FROM mcr.microsoft.com/dotnet/aspnet
WORKDIR /App
EXPOSE 80
# Copy the published output from the build stage
COPY --from=build_env /App/out .
# Specify the entry point for the application
ENTRYPOINT ["dotnet", "WebShop.dll"]
Init.sh
#!/bin/bash
USERNAME=$1
PASSWORD=$2
DATABASE=$3
echo "${USERNAME}, ${PASSWORD}, ${DATABASE}"
echo "Waiting for MySQL to be ready..."
until mysql -u${USERNAME} -p${PASSWORD} -e "SELECT 1" &> /dev/null; do
echo "Waiting for MySQL..."
sleep 2
done
# Wait for MySQL to be ready
echo "Waiting for MySQL to be ready..."
/docker-entrypoint-initdb.d/wait_until.sh "mysql -u${USERNAME} -p${PASSWORD} -e 'SELECT 1'"
# Execute the initial SQL script
echo "Running initial SQL script..."
mysql -u${USERNAME} -p${PASSWORD} < /docker-entrypoint-initdb.d/init.sql
echo "Database is ready."
# Start the WebShop API to apply migrations and seed data
echo "Starting the WebShop API to apply migrations..."
dotnet /App/WebShop.dll &
API_PID=$!
# Declare all tables
declare -a tables=("AspNetUsers" "AspNetRoles" "AspNetUserRoles" "AspNetUserClaims" "AspNetUserLogins" "AspNetRoleClaims" "AspNetUserTokens" "Addresses" "Products" "Orders" "OrderedProducts")
# Wait for migrations to complete
echo "Waiting for migrations to complete..."
for table in "${tables[@]}"
do
/docker-entrypoint-initdb.d/wait_until.sh "mysql -u${USERNAME} -p${PASSWORD} -e 'SELECT 1 FROM ${DATABASE}.${table} LIMIT 1;'"
echo "${table} created."
done
echo "All tables created"
# Kill the API process
kill $API_PID
# Execute the post-migration SQL script
echo "Running post-migration SQL script to grant privileges..."
mysql -u${USERNAME} -p${PASSWORD} < /docker-entrypoint-initdb.d/grant_privileges.sql
echo "Privileges granted."
The wait until script is this wait-until script copy pasted.
init.sql
CREATE DATABASE IF NOT EXISTS ${MYSQL_DATABASE};
CREATE USER IF NOT EXISTS '${MYSQL_USER}'@'%' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}';
CREATE USER IF NOT EXISTS '${MYSQL_EMPLOYEE_USER}'@'%' IDENTIFIED BY '${MYSQL_EMPLOYEE_PASSWORD}';
CREATE USER IF NOT EXISTS '${MYSQL_CLIENT_USER}'@'%' IDENTIFIED BY '${MYSQL_CLIENT_PASSWORD}';
CREATE USER IF NOT EXISTS '${MYSQL_DEFAULT_USER}'@'%' IDENTIFIED BY '${MYSQL_DEFAULT_PASSWORD}';
GRANT ALL PRIVILEGES ON *.* TO '${MYSQL_USER}'@'%' WITH GRANT OPTION;
GRANT SELECT ON '${MYSQL_DATABASE}'.* TO ''@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
I run my c# program to create my tables and seed the data.
The grant_priviliges.sql script just grants the users the correct privileges for each of the tables.
My docker compose build works just fine but when trying to run docker compose up I get this output:
webshopdb | root, RootPwd1, WebShopDB
webshopdb | Waiting for MySQL to be ready...
webshopdb | mysql: [Warning] Using a password on the command line interface can be insecure.
webshopdb | ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
webshopapi | The command could not be loaded, possibly because:
webshopapi | * You intended to execute a .NET application:
webshopapi | The application 'WebShop.dll' does not exist.
webshopapi | * You intended to execute a .NET SDK command:
webshopapi | No .NET SDKs were found.
webshopapi |
webshopapi | Download a .NET SDK:
webshopapi | https://aka.ms/dotnet/download
webshopapi |
webshopapi | Learn about SDK resolution:
webshopapi | https://aka.ms/dotnet/sdk-not-found
webshopapi exited with code 145
webshopdb | mysql: [Warning] Using a password on the command line interface can be insecure.
webshopdb | ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
The last two lines keep repeating, I assume because something is wrong in my init.sh.
Docker version: 4.33.1
OS: Windows 11 version 10.0.22631
I can give additional info if requested.