Docker compose copying SQL to volume but not importing into DB - database

I spent most morning trying to figure out not only how to copy an initial SQL dump into the container, but also how to auto-import (execute) the dump into the DB. I have read countless other posts, none of which seem to work. I have the following docker compose file:
version: '3.8'
services:
db:
image: mariadb:10.5.8
restart: always
container_name: database
environment:
MYSQL_ROOT_PASSWORD: default
volumes:
- db-data:/var/lib/mysql
- ./db-init:/docker-entrypoint-initdb.d
volumes:
db-data:
The SQL dump is found in the db-init folder. I got the docker-entrypoint-initdb.d from the official docs on DockerHub.
After docker-compose up, the SQL is correctly copied into the docker-entrypoint-initdb.d but is never ran against the DB, aka the dump is never imported and the DB remains empty.
I have tried placing the volumes directive around in the docker compose file as this was suggested in another post. From what I've read, the SQL dump should be imported automatically when mounting the volume.
Is there no way to accomplish this via the docker-compose.yml only?
Edit: Switching the version to 2.x did not work
EDIT2: Container logs:
2021-02-10 17:53:09+00:00 [Note] [Entrypoint]: /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/wordpress.sql
ERROR 1046 (3D000) at line 10: No database selected

From your logs, a quick google search pointed to this post. Adding MYSQL_DATABASE to the environment should solve the issue and the .sql should then be imported correctly on startup.
Final docker-compose should look like this:
services:
db:
image: mariadb:10.5.8
restart: always
container_name: database
environment:
MYSQL_DATABASE: wordpress
MYSQL_ROOT_PASSWORD: default
volumes:
- db-data:/var/lib/mysql
- ./db-init:/docker-entrypoint-initdb.d/
Maybe not worded as strongly as it should be, but the docs mention this: SQL files will be imported by default to the database specified by the MYSQL_DATABASE variable.

Related

Docker SQL Server relative data-path

I have a docker-compose.yml having this lines.
version: "3.9"
services:
mssql:
image: localhost/local_mssql_server:mssqlserver
ports:
- "1433:1433"
volumes:
- sqlfolder1234:/var/opt/mssql
The Docker is starting up successfully and serving data.
But I seldom work with windows and I like to know where is the host-folder sqlfolder1234?
I tried the windows-explorer to search for that folder. Since an hour he not finished yet.
Where is that folder sqlfolder1234 on my host system?
This volume type is called Named volume Here is some description from the official document. Short syntax
In the absence of having named volumes with specified sources, Docker creates an anonymous volume for each task backing a service. Anonymous volumes do not persist after
the associated containers are removed.
volumes:
# Just specify a path and let the Engine create a volume
- /var/lib/mysql
# Specify an absolute path mapping
- /opt/data:/var/lib/mysql
# Path on the host, relative to the Compose file
- ./cache:/tmp/cache
# User-relative path
- ~/configs:/etc/configs/:ro
# Named volume
- datavolume:/var/lib/mysql
If you want to keep persistent data in your host computer, I would use another volume way to do that. this sample provides Path on the host, relative to the Compose file that help us can see the volumn folder in the docker-compose file level.
volumes:
- ./sqlfolder1234:/var/opt/mssql

Hot reload on React with docker-compose failing on Windows

I'm trying to get React to change content of the site when it's file is being saved.
I'm using VS code which doesn't have safe write. I'm using docker-compose on Windows via Docker Desktop.
Dockerfile:
FROM node:17-alpine
WORKDIR /front
ARG FRONT_CMD
ARG API_HOSTNAME
ENV REACT_APP_API_HOSTNAME=$API_HOSTNAME
COPY . .
RUN npm i #emotion/react #emotion/styled
CMD $FRONT_CMD
relevant part of docker-compose.yml:
frontend:
volumes:
- ./frontend/src:/front/src
- /front/node_modules
build:
context: ./frontend
dockerfile: Dockerfile
args:
- FRONT_CMD=${FRONT_CMD}
- API_HOSTNAME=${API_HOSTNAME}
env_file:
- .env.dev
networks:
- internal
environment:
- CHOKIDAR_USEPOLLING=true
- FAST_REFRESH=false
- NODE_ENV=development
Everything is running behind traefik. CHOKIDAR_USEPOLLING and FAST_REFRESH seem to make no difference, I start with ' docker-compose --env-file ..env.dev up' - within the file FRONT_CMD="npm start" which behaves just fine. Env.dev should be clear indication of dev build (and is, works the same without the addition) to React, but I added NODE_ENV just be safe. I tried adding all of them into build envs just be super sure, but nothing changes. React files lay in 'frontend' folder, which is in the same location as docker-compose.yml.
Every time React says it compiled successfully and warns me that it's a development build.
Only suspicion I have left is that there's some issue with updating files with Windows locally while docker uses Linux, but I have no idea where to go from there even if that's the case.
Shortest way I found was to start from the other side, that is attach editor to container instead of updating container based on changes in system files. I followed the guide here: https://code.visualstudio.com/docs/remote/attach-container

Running ddev without database

I want to set up a ddev (v1.5.2) project without a database. When I try to overwrite the image in a Docker-Compose YAML it stops with an error.
As suggested for the dba I have overwritten the db image in an additional docker-compose.database.yaml in the .ddev folder.
version: '3.6'
services:
db:
image: "busybox"
I expected it to start without the database and it does, but it seems to do a health check for the database which fails.
Failed to start sitzplan: db container failed: log=, err=container exited, please use 'ddev logs -s db` to find out why it failed
The project is running, but it's not working, because it won't run my post-start hooks, which are necessary. In that means I can't even ignore the error.
First, note that there's now explicit support for just turning off the dba/phpmyadmin container, omit_containers: dba (can also be done in the global ddev config, ~/.ddev/global_config.yaml).
And of course I would recommend just letting the regular db container run and not using it.
But here's a docker-compose.database.yaml that does what you ask for:
version: '3.6'
services:
db:
image: "busybox:latest"
command: sh -c "while true; do sleep 1000; done"
healthcheck:
test: ["CMD", "true"]

Visual Studio 2017 15.3 Dockerize Database Project

Have Visual Studio 2017 (15.3) solution with two projects:
An API written in ASP.NET Core 2 MVC
Database Project
I was able to "dockerize" the MVC project easily (right click, add Docker support) but while trying to dockerize the Database project keep getting the error: Value cannot be null. Parameter name: stream. My Google-fu is failing me; the closest resource found is for Visual Studio 15.2.
How I've Setup Database Project So Far
Added Dockerfile to root:
FROM microsoft/mssql-server-linux:latest
EXPOSE 1433
ENV ACCEPT_EULA=Y
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8
ENV MSSQL_TCP_PORT=1433
# Add Database project output from VS build process
RUN mkdir --parents /_scripts/generated
COPY ./_scripts /_scripts/
COPY ./_scripts/generated/*.sql /_scripts/generated/
# Add shell script that starts MSSQL server, waits 60 seconds, then executes script to build out DB (script generated from VS build process)
CMD /bin/bash /_scripts/entrypoint.sh
Modified docker-compose.yml file to include new project
version: '3'
services:
webapp-api-service:
image: webapp-api
build:
context: ./src/API
dockerfile: Dockerfile
webapp-db-service:
image: webapp-db
build:
context: ./src/Database
dockerfile: Dockerfile
Modified docker-composeoverride.yml file to expose port for dev SSMS access
version: '3'
services:
webapp-api-service:
environment:
- ASPNETCORE_ENVIRONMENT=Development
ports:
- "80"
webapp-db-service:
ports:
- "1433"
Here's the build output
2>C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Sdks\Microsoft.Docker.Sdk\build\Microsoft.VisualStudio.Docker.Compose.targets(279,5): error : Value cannot be null.
2>C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Sdks\Microsoft.Docker.Sdk\build\Microsoft.VisualStudio.Docker.Compose.targets(279,5): error : Parameter name: stream
2>Done building project "docker-compose.dcproj" -- FAILED.
Thanks in advance!
I ran into this same issue yesterday. I just solved it by removing the build portion of the database service. I'll just have to build the database project manually for now.
You can add a file named AppType.cache to /obj/Docker with content AspNetCore as a workaround.

Centralised configuration of docker-compose services

Imagine a non-trivial docker compose app, with nginx in front of a webapp, and a few linked data stores:
web:
build: my-django-app
volumes:
- .:/code
ports:
- "8000:8000"
links:
- redis
- mysql
- mongodb
nginx:
image: nginx
links:
- web
redis:
image: redis
expose:
- "6379"
mysql:
image: mysql
volumes:
- /var/lib/mysql
environment:
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
- MYSQL_DATABASE=myproject
mongodb:
image: mongo
The databases are pretty easy to configure (for now), the containers expose pretty nice environmental variables to control them (see the mysql container), but what of nginx? We'll need to template a vhost file for that, right?
I don't want to roll my own image, that'll need rebuilding for each changed config, from different devs' setups, to test, through staging and production. And what if we want to, in a lightweight manner, do A/B testing by flipping a config option?
Some centralised config management is needed here, maybe something controlled by docker-compose that can write out config files to a shared volume?
This will only get more important as new services are added (imagine a microservice cloud, rather than, as in this example, a monolithic web app)
What is the correct way to manage configuration in a docker-compose project?
In general you'll find that most containers use entrypoint scripts to configure applications by populating configuration files using environment variables. For an advanced example of this approach see the entrypoint script for the Wordpress official image.
Because this is a common pattern, Jason Wilder created the dockerize project to help automate the process.

Resources