Docker
0
Вклад в тег
version: '3'
services:
web:
volumes:
- ./web:/app
env_file:
- ./web/.env
build: ./web
ports:
- '8000:8000'
networks:
- my_network
other_service:
image: some_image
networks:
- my_network
networks:
my_network:
driver: bridge
environment:
- API_URL=http://external.api.com
version: '3'
services:
web:
volumes:
- ./web:/app
env_file:
- ./web/.env
build: ./web
ports:
- '8000:8000'
networks:
- my_network
db:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: example
networks:
- my_network
networks:
my_network:
driver: bridge
public class DockerContext: DbContext
{
private bool InDocker { get { return Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER") == "true"; } }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!InDocker)
{
// Здесь переопределяем ConnectionString для миграций
}
}
public static bool InDocker { get { return Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER") == "true"; } }
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<DockerContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString(InDocker ? "DockerContext" : "MigrationContext") ?? throw new InvalidOperationException("Connection string 'DockerContext' not found.")));
}