How to create a MySQL database with Docker for Windows? - docker-compose

I'm using Docker for Windows Version 17.12.0-ce-win47 (15139) with Hyper-V.
I'm trying to restore an existing schema into a Docker container, but keep getting errors.
docker-compoer.yml:
version: '3'
services:
mysql:
image: mysql
environment:
MYSQL_USER: test
MYSQL_PASSWORD: testtest
MYSQL_RANDOM_ROOT_PASSWORD: 1
volumes:
- ./Schema/:/docker-entrypoint-initdb.d/
Schema/test.sql:
CREATE DATABASE IF NOT EXISTS test;
USE test;
CREATE TABLE IF NOT EXISTS data (
Id INT PRIMARY KEY AUTO_INCREMENT
);
When I run:
docker-compose up
I get this error:
ERROR: for workspace_mysql_1 Cannot create container for service mysql: invalid volume specification: 'C:\Users\Kyle\Workspace\Schema:/docker-entrypoint-initdb.d/:rw'
ERROR: for mysql Cannot create container for service mysql: invalid volume specification: 'C:\Users\Kyle\Workspace\Schema:/docker-entrypoint-initdb.d/:rw'
Encountered errors while bringing up the project.
I've also tested on Docker for Mac and it appears to work just fine.

You need specify the volume name as well in docker-compose.yml , In parallel to services add this
volumes:
./Schema/:

Related

Can't connect with docker-compose to Postgres database

I'm trying to build a docker-compose file that will spin up my EF Core web api project, connecting to my Postgres database.
I'm having a hard time getting the EF project connecting to the database.
This is what I currently have for my docker-compose.yml:
version: '3.8'
services:
web:
container_name: 'mybackendcontainer'
image: 'myuser/mybackend:0.0.6'
build:
context: .
dockerfile: backend.dockerfile
ports:
- 8080:80
depends_on:
- postgres
networks:
- mybackend-network
postgres:
container_name: 'postgres'
image: 'postgres:latest'
environment:
- POSTGRES_USER=username
- POSTGRES_PASSWORD=MySuperSecurePassword!
- POSTGRES_DB=MyDatabase
networks:
- mybackend-network
expose:
- 5432
volumes:
- ./db-data/:/var/lib/postgresql/data/
pgadmin:
image: dpage/pgadmin4
ports:
- 15433:80
env_file:
- .env
depends_on:
- postgres
networks:
- mybackend-network
volumes:
- ./pgadmin-data/:/var/lib/pgadmin/
networks:
mybackend-network:
driver: bridge
And my web project docker file looks like this:
# Get base DSK Image from Microsoft
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
WORKDIR /app
# Copy the CSPROJ file and restore any dependencies (via NUGET)
COPY *.csproj ./
RUN dotnet restore
# Copy the project files and build our release
COPY . ./
RUN dotnet publish -c Release -o out
# Generate runtime image - do not include the whole SDK to save image space
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
EXPOSE 80
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "MyBackend.dll"]
And my connection string looks like this:
User ID =bootcampdb;Password=MySuperSecurePassword!;Server=postgres;Port=5432;Database=MyDatabase; Integrated Security=true;Pooling=true;
Currently I have two problems:
I'm getting Npgsql.PostgresException (0x80004005): 57P03: the database system is starting up when I do docker-compose -up. I tried to add the healthcheck to my postgress db but that did not work. When I go to my Docker desktop app, and start my backend again, that message goes away and I get my second problem...
Secondly after the DB started it's saying: FATAL: password authentication failed for user "username". It looks like it's not creating my user for the database. I even changed not to use .env files but have the value in my docker-compose file, but its still not working. I've tried to do docker-compose down -v to ensure my volumes gets deleted.
Sorry these might be silly questions, I'm still new to containerization and trying to get this to work.
Any help will be appreciated!
Problem 1: Having depends_on only means that docker-compose will wait until your postgres container is started before it starts the web container. The postgres container needs some time to get ready to accept connections and if you attempt to connect before it's ready, you get the error you're seeing. You need to code your backend in a way that it'll wait until Postgres is ready by retrying the connection with a delay.
Problem 2: Postgres only creates the user and database if no database already exists. You probably have an existing database in ./db-data/ on the host. Try deleting ./db-data/ and Postgres should create the user and database using the environment variables you've set.

java.io.EOFException: null while running Postgres via docker-compose

I'm new to docker-compose, wanted to run a container with Postgres db in it. For this I've created a docker-compose.yml with the following content:
services:
postgresql:
image: postgres
container_name: postgres_db
ports:
- "5433:5433"
environment:
- POSTGRES_PASSWORD=admin
- POSTGRES_USER=postgres
- POSTGRES_DB=testdb
So far I didn't pack my Spring Boot app in a container as well, as I'd like to do it step by step and for now run only the database in container.
I start it with
docker-compose -f docker-compose.yml up, then I run my app in Intellij and see that in logs:
org.postgresql.util.PSQLException: The connection attempt failed.
Caused by: java.io.EOFException: null
Via
docker-exec I was able to connect to my testdb. However, in Intellij I can't do the same via Database tool, I keep getting authentication error (username/password are wrong).
I'm struggling with these new concepts. What am I missing here?

Postgresql version in docker container is not compatible with its data

Mac OS 10.13.6
docker 9.03.5, build 633a0ea
I create two docker containers, web and db using docker-compose.yml. It worked for several months. Recently I decided to rebuild containers from scratch, so I actually removed the existing ones and started over:
$ docker-compose -f docker-compose-dev.yml build --no-cache
$ docker-compose -f docker-compose-dev.yml up -d
Duiring the "build" run, building db container yields this:
DETAIL: The data directory was initialised by PostgreSQL version 9.6,
which is not compatible with this version 11.2.
exited with code 1
The db container does not start so I can not check what it's got inside.
My containers
are defined like this:
version: '3'
services:
web:
restart: unless-stopped
container_name: web
build:
context: .
dockerfile: Dockerfile.dev
ports:
- "8000:8000"
environment:
DJANGO_SETTINGS_MODULE: '<my_app>.settings.postgres'
DB_NAME: 'my_db'
DB_USER: 'my_db_user'
DB_PASS: 'my_db_user'
DB_HOST: 'my_db_host'
PRODUCTION: 'false'
DEBUG: 'True'
depends_on:
- db
volumes:
- ./:/usr/src/app/
db:
image: postgres:11.2-alpine
volumes:
- myapp-db-dev:/var/lib/postgresql/data
environment:
- POSTGRES_DB=<my_db>
- POSTGRES_USER=<my_db_user>
- POSTGRES_PASSWORD=<my_db_password>
volumes:
myapp-db-dev:
My local postgresql is 11.3 (which should be irrelevant):
$ psql --version
psql (PostgreSQL) 11.3
and my local postgresql data directory was removed completely
$ rm -rf /usr/local/var/postgres
However, it's up-to-date:
$ brew postgresql-upgrade-database
Error: postgresql data already upgraded!
I read Stack Overflow 17822974 and Stack Overflow 19076980, those advices did not help.
How to fix this data incompatibility? If possible, I would like to avoid downgrading postgres. I don't even get what data it's talking about at that point, all the data is migrated later in a separate step.
It seems like on the first run Postgres 9.6 was specified as an image. So, the container was initialized and the data was put to the myapp-db-dev named volume. Then someone changed the version and you've got the error. The possible solution would be:
Temporary downgrade the version to the Postgres 9.6, e.g. specify postgres:9.6.
Go to the container and dump the data with pg_dump utility.
Change version to 11.2 and specify new volume (it's a good advice to use host volume).
Restore the data.

Unable to start postgres docker container from docker-compose

I am trying to start a postgresql docker container which is of version 10.5.
But before that I have used 9.6 version in the same docker-compose.yml file and there is no data populated in the database.
And now after changing the version of postgres container, I'm not able to run the docker-compose up. It is throwing the below error.
FATAL: database files are incompatible with server
DETAIL: The data directory was initialized by PostgreSQL version 9.6,
which is not compatible with this version 10.5 (Debian
10.5-2.pgdg90+1)
This is how the docker-compose.yml file looks like.
version: '2'
services:
postgres_service:
container_name: postgresql_container
restart: always
image: postgres:10.5
volumes:
- postgres-data:/var/lib/postgresql/data
- ./postgresql/init:/docker-entrypoint-initdb.d
ports:
- "5432:5432"
environment:
- POSTGRES_USER=admin
- POSTGRES_PASSWORD=password
volumes:
postgres-data:
driver: local
Can someone please let me know where the issue is. Where am I making mistake?
Do I need to delete any volumes before proceeding with the new postgres version?
I also have postgresql installed in my local.
postgres=# select version();
version
-------------------------------------------------------------------------------------------------------------------------------------
PostgreSQL 10.10 (Ubuntu 10.10-1.pgdg18.04+1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0, 64-bit
(1 row)
Will this cause any issue?
The problem caused because the volume which your compose created to store your database still keep old data which initiated by PostgreSQL 9.6. That volume name is postgres-data which created when you use named volume on your docker-compose.yml. So simply to get rid of this, you can use some ways below:
Using docker-compose command:
Run docker-compose down -v, this will stop all your container inside that compose and remove all named volume inside that compose.
You could take a look at docker-compose down command
Using docker volume command:
Run docker volume ls to get list of current volumes on your machine, I think you will see your volume on that list too:
DRIVER VOLUME NAME
local postgres-data
Run docker volume rm postgres-data to remove that volume, if your container still running and you couldn't remove it then you can use -f to force remove it
Hope that helps!
What worked for me was deleting pgdata folder inside the root of my project and running docker-compose build -d. It then showed me
/usr/local/bin/docker-entrypoint.sh: /docker-entrypoint-initdb.d/create-multiple-postgres-databases.sh: /bin/bash: bad interpreter: Permission denied
To fix it, I ran
chmod +x pg-init-scripts/create-multiple-postgresql-databases.sh
Notice that the .sh file name should match the one you have. And finally, docker-compose up -d.

.sql file in docker-entrypoint-initdb not being run

I am using docker-compose 3.7 to run Postgresql but I can't get my init.sql to run through the docker-entrypoint-initdb.d entry point.
I understand that the volume directory on my machine needs to be empty but I am sure that I am doing that correctly. I must be missing something fairly simple/obvious.
version: '3.7'
services:
postgresdb:
environment:
POSTGRES_DB: appdb
POSTGRES_USER: appdb
POSTGRES_PASSWORD: ########
image: bitnami/postgresql:latest
ports:
- "5432:5432"
volumes:
- ./db/postgres_volume:/bitnami:rw
- ./db/init.sql:/docker-entrypoint-initdb.d/init.sql
restart: always
CREATE TABLE user (
id SERIAL NOT NULL PRIMARY KEY,
name VARCHAR(255) NOT NULL
);
My volume directory is empty.
$ ls db/postgres_volume/
$
The container starts with no issues but from the logs, I can't tell if the init.sql script was executed. Scripts seemed to be loaded but I am not sure about execution.
$ docker-compose -f docker-compose.yml up -d postgresdb
Creating network "app_default" with the default driver
Creating app_postgresdb_1 ... done
$
postgresdb_1 | INFO ==> Loading custom scripts...
postgresdb_1 | INFO ==> Loading user's custom files from /docker-entrypoint-initdb.d ...
postgresdb_1 | INFO ==> Starting PostgreSQL in background...
Logging into the container and then into my database, I see no tables.
$ docker exec -it 737845344cf2 bash
I have no name!#737845344cf2:/$ psql -U appdb appdb
appdb=> \dt
Did not find any relations.
After the container is created, I see a "postgresql" folder db/postgres_volume but is has no contents.
$ ls db/postgres_volume/
postgresql
$ ls db/postgres_volume/postgresql/
$
First thing, It will never create a table because you are using reserve word user in table creation. change the user to "user"
CREATE TABLE "user"(
user_id serial PRIMARY KEY,
username VARCHAR (50) UNIQUE NOT NULL
)
Second thing, The above will work fine with initialization the DB but there is a mounting issue in the lastest image that might be the reason that the mount directory seemed empty.
Also in the documentation, the path is -v /path/to/postgresql-persistence:/bitnami/postgresql Persisting your database