20210726のdockerに関する記事は4件です。

Docker上で動かしたASP.NET Coreアプリで"no such table: テーブル名"となる場合の対処法

はじめに ASP.NET Core + SQLiteで自作したアプリをDocker上で動かした際に"no such table:テーブル名"と出た際の対処方法を備忘録として記載します。 エラー内容 Dockerfileをビルドし、docker run ~ でコンテナを作成していざアクセスした時でした。 fail: Microsoft.EntityFrameworkCore.Database.Command[20102] Failed executing DbCommand (7ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] SELECT "t"."TestId", "t"."CreatedTime", "t"."Number", "t"."Title", "t"."UpdatedTime", "t"."UserId" FROM "Tests" AS "t" fail: Microsoft.EntityFrameworkCore.Query[10100] An exception occurred while iterating over the results of a query for context type 'DDD.Domain.Data.TestMakerContext'. Microsoft.Data.Sqlite.SqliteException (0x80004005): SQLite Error 1: 'no such table: Tests'. at Microsoft.Data.Sqlite.SqliteException.ThrowExceptionForRC(Int32 rc, sqlite3 db) at Microsoft.Data.Sqlite.SqliteCommand.PrepareAndEnumerateStatements(Stopwatch timer)+MoveNext() at Microsoft.Data.Sqlite.SqliteCommand.GetStatements(Stopwatch timer)+MoveNext() at Microsoft.Data.Sqlite.SqliteDataReader.NextResult() at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader(CommandBehavior behavior) at Microsoft.Data.Sqlite.SqliteCommand.ExecuteDbDataReader(CommandBehavior behavior) at System.Data.Common.DbCommand.ExecuteReader() ・・・以下いっぱいのエラーログ 「Microsoft.Data.Sqlite.SqliteException (0x80004005): SQLite Error 1: 'no such table: Tests'.」とありますので、どうやら対象のテーブルができていないようです。 対処法 以下のstackoverflowにも記載されていますが、DbContextのコンストラクタへ「Database.EnsureCreated();」を追記するとテーブルが作成されるとのことでした。 https://stackoverflow.com/questions/33455041/asp-net-5-ef-7-and-sqlite-sqlite-error-1-no-such-table-blog/47751630 ソースコードは以下のような感じになります。 TestMakerContext.cs public class TestMakerContext : DbContext { public TestMakerContext (DbContextOptions<TestMakerContext> options) : base(options) { Database.EnsureCreated(); // <-- 追記 } public DbSet<Test> Tests { get; set; } public DbSet<Question> Questions { get; set; } public DbSet<Choice> Choices { get; set; } public DbSet<User> Users { get; set; } } Database.EnsureCreated()とは? データベースが存在しない場合は、EnsureCreated によって作成され、データベーススキーマが初期化されます。 テーブルが存在する場合 (別の DbContext クラスのテーブルを含む)、スキーマは初期化されません。 データベースが存在しない場合に良い感じに作ってくれるみたいです。 https://docs.microsoft.com/ja-jp/ef/core/managing-schemas/ensure-created#ensurecreated デメリット この記事を書くにあたり、色々と調べていたらどうやらこの方法だとデータベースの移行(Migrate)には対応しないようですね・・・(;´∀`) https://docs.microsoft.com/ja-jp/dotnet/api/microsoft.entityframeworkcore.infrastructure.databasefacade.ensurecreated?view=efcore-5.0 移行を考慮している際は上記の記事によると「Database.Migrate ()」を使用することを推奨しているので、そちらもいずれ動作確認してみようと思います。
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

【Docker】MAMP配下のディレクトリをコンテナにマウントできなかったときの対処法

もともとMAMP環境で作成していたWebアプリのディレクトリをDockerコンテナにマウントしようとした時に出たエラーの対処をメモ的にまとめておきます。 やりたいこと phpのイメージをベースにしたコンテナに、Webアプリのコード類が入っているディレクトリをマウントしてコンテナを起動したい。 Dockerfile, docker-compose.ymlの中身はシンプルにこんな感じ。 Dockerfileとdocker-compose.ymlは同じディレクトリ階層に置いています。 Dockerfile FROM php:7.2-fpm WORKDIR /var/www/html/app docker-compose.yml version: '3' services: php: build: . volumes: - .:/var/www/html/app 後々複数コンテナを起動することを考えて、docker-composeでコンテナを起動させます。 出たエラー エラー内容はこんな感じ ERROR: for app_container Cannot start service web: Mounts denied: The path /Applications/MAMP/htdocs/app is not shared from the host and is not known to Docker. You can configure shared paths from Docker -> Preferences... -> Resources -> File Sharing. See https://docs.docker.com/docker-for-mac for more info. ERROR: for web Cannot start service web: Mounts denied: The path /Applications/MAMP/htdocs/app is not shared from the host and is not known to Docker. You can configure shared paths from Docker -> Preferences... -> Resources -> File Sharing. See https://docs.docker.com/docker-for-mac for more info. ERROR: Encountered errors while bringing up the project. 内容としては「/Applications/MAMP/htdocs/appのパスがホスト(このPC)から共有されていません。Dockerに知らされていません」といった感じ。 対処 エラー文に対処法が書かれているので、そのまま実行します。 MACであれば画面上のDockerアイコンからPreferencesを選択。 ResourcesからFile Sharingを選択。 ここでDockerコンテナにマウントできるディレクトリが設定されているので、MAMP配下にあるディレクトリを指定します。 /Applications/MAMP/htdocs このパスを追加することでMAMP配下のディレクトリでもDockerコンテナへのマウントが可能になります。
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

DockerでSwagger環境簡単構築!

概要 Docker上で動くSwagger環境の構築方法をご紹介します。 成果物 Editor 画面左側がエディター、右側がSwagger UIとなっておりリアルタイムで記法のチェックや定義書を確認できます。 後述するサンプルを左側にコピペすると、右側に結果が表示されますので試してみてください。 UI API アクセスURL: http://localhost:8003/users [{"id":100,"name":"Taro","status":"pending"}] 環境 macOS Big Sur 11.4 Docker 20.10.7 Docker Compose version v2.0.0-beta.6 構成 今回用意するのは以下の2ファイルだけです! . ├── api │   └── openapi.yaml └── docker-compose.yml 手順 1. docker-compose.yml作成 今回は、swaggerのエディタ・UI・APIモックのコンテナをそれぞれ用意します。 docker-compose.yml version: '3.9' services: swagger-editor: image: swaggerapi/swagger-editor container_name: "swagger-editor" ports: - "8001:8080" swagger-ui: image: swaggerapi/swagger-ui container_name: "swagger-ui" ports: - "8002:8080" volumes: - ./api/openapi.yaml:/openapi.yaml environment: SWAGGER_JSON: /openapi.yaml swagger-api: image: stoplight/prism:3 container_name: "swagger-api" ports: - "8003:4010" command: mock -h 0.0.0.0 /openapi.yaml volumes: - ./api/openapi.yaml:/openapi.yaml 2. openapi.yaml作成 サンプル用のAPI設計書を用意します。 こちらをもとに openapi.yaml openapi: 3.0.0 info: version: 1.0.0 title: Sample API description: >- A sample API that uses a sample-site as an example to demonstrate features in the OpenAPI 3.0 specification servers: - url: 'http://localhost:8003' paths: /users: get: description: > Returns all users operationId: findUsers parameters: - name: tags in: query description: tags to filter by required: false style: form schema: type: array items: type: string - name: limit in: query description: maximum number of results to return required: false schema: type: integer format: int32 responses: '200': description: user response content: application/json: schema: type: array items: $ref: '#/components/schemas/User' default: description: unexpected error content: application/json: schema: $ref: '#/components/schemas/Error' components: schemas: User: type: "object" required: - "name" properties: id: type: "integer" format: "int64" example: 100 name: type: "string" example: "Taro" status: type: "string" description: "user status" enum: - "pending" - "active" - "inactive" Error: type: "object" properties: code: type: "integer" format: "int32" type: type: "string" message: type: "string" externalDocs: description: "Find out more about Swagger" url: "http://swagger.io" 3. 起動 docker-compose up -d 確認用URL 名称 URL Swagger Editor http://localhost:8001/ Swagger UI http://localhost:8002/ Swagger API mock http://localhost:8003/users 参考 OpenAPI (Swagger) 超入門 Swagger OpenAPIでAPI Referenceを書く
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

【Kubernetes】VMのDockerのShellにアクセスする (自分用メモ) no.11

VM上にあるdocker-cliのシェルを使いたい時 minikube docker-env とコマンドを打つと、minikubeのdocker のシェルを使うために必要なコマンドが表示される 使うターミナルが違うとVMのdocker-cliのターミナルにアクセスするためのコマンドも違う
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む