equivalent of copy command to init the database via entry point - docker

What is the equivalent of specifying the schema of a database in docker-compose.yml file.
database/Dockerfile
FROM mysql:5.7
COPY ./schema.sql /docker-entrypoint-initdb.d/
docker-compose.yml
services:
database:
image: "mysql:5.7"
container_name: "mysql"
ports:
- "6603:3306"
Attempts
I've attempted with the following. Is it possible?
version: '3'
services:
database:
image: "mysql:5.7"
container_name: "mysql"
ports:
- "6603:3306"
command: --init-file /database/schema.sql
volumes:
- ./init.sql:/database/schema.sql

So you have schema.sql somewhere on your host filesystem, let's assume it's on ./database/schema.sql. Then you should have such a compose file:
version: '3'
services:
database:
image: "mysql:5.7"
container_name: "mysql"
ports:
- "6603:3306"
volumes:
- ./database/schema.sql:/docker-entrypoint-initdb.d/init.sql
This image does not support any --init-file command. Instead, it accepts init scripts put under directory /docker-entrypoint-initdb.d.

Related

Why are docker volumes not created?

I am running Flask and MySQL using docker-compose.yaml.
Although I have specified a volume, the volume has not been created.
Why is this?
docker-compose.yaml
version: '3'
services:
api:
build: python
container_name: api_server_flask
ports:
- "5000:5000"
tty: true
environment:
TZ: Asia/Tokyo
FLASK_APP: app.py
depends_on:
- db
networks:
- app_flask
db:
build: mysql
container_name: db_server_flask
ports:
- '3306:3306'
environment:
MYSQL_ROOT_PASSWORD: pass
MYSQL_DATABASE: testdb
TZ: Asia/Tokyo
volumes:
- ./db-flask-data/:/var/lib/mysql
command: mysqld
networks:
- app_flask
volumes:
db-flask-data:
networks:
app_flask:
driver: bridge
You need to remove the relative path as follows:
volumes:
- db-flask-data/:/var/lib/mysql
It is explained here: https://docs.docker.com/storage/volumes/#use-a-volume-with-docker-compose
Actually, you are creating a named volume under /var/lib/docker/volumes/db-flask-data when you are specifying the following command:
volumes:
db-flask-data:

services.mysql Additional property website is not allowed

version: '3.8'
volumes:
datafiles:
services:
mysql:
image: mysql:latest
command: --default-authentication-plugin=mysql_native_password
container_name: mysql_ecommerce
environment:
- MYSQL_ROOT_PASSWORD=12345
- MYSQL_TCP_PORT=3308:3306
volumes:
- datafiles:/var/lib/mysql
restart: always
website:
container_name: web_ecommerce
build:
context: .
dockerfile: Dockerfile
enviroment:
- MYSQL_DBHOST=mysql
- MYSQL_DBPORT=3306
- MYSQL_DBUSER=root
- MYSQL_DBPASS=12345
- MYSQL_DBNAME=
ports:
- 8082:80
- 8083:443
depends_on:
- mysql
You may need to check your docker-compose file and ensure it follows the correct indentation.
version: '3.8'
services:
mysql:
website:

Docker-Compose build image and use its .env file which are stored in another directory

I have a docker-compose file which contains a bunch of services. To that docker-compose file, I want to add another service now. The other service files (including its .env) are stored in another folder. I tried to build it like I show you below, but it isnt working. Where do I go wrong?
The docker-compose.yml is contained in the directory nft-trading-service, the other dockerfile which I am trying to include in this docker-compose.yaml is in its own folder nft-asset-updater.
So the structure looks like this
root/nft-trading-server (holding docker-compose.yml)
root/nft-asset-updater (holding its own Dockerfile and .env)
version: "3"
services:
nftapi:
env_file:
- .env
build:
context: .
ports:
- '5000:5000'
depends_on:
- postgres
networks:
- postgres
extra_hosts:
- "host.docker.internal:host-gateway"
restart: always
asset_update_service:
env_file:
- .env
build:
context: ../nft-asset-updater
dockerfile: .
ports:
- '9000:9000'
depends_on:
- postgres
networks:
- postgres
extra_hosts:
- "host.docker.internal:host-gateway"
restart: always
postgres:
container_name: postgres
image: postgres:latest
ports:
- "5432:5432"
volumes:
- /data/postgres:/var/lib/postgresql/data
env_file:
- docker.env
networks:
- postgres
pgadmin:
links:
- postgres:postgres
container_name: pgadmin
image: dpage/pgadmin4
ports:
- "8080:80"
env_file:
- docker.env
networks:
- postgres
networks:
postgres:
driver: bridge

Need to up my docker file in ubuntu "docker-compose up -d"

services:
postgres:
container_name: 'lh-postgres'
image: 'postgres:13'
environment:
POSTGRES_PASSWORD: root
redis:
container_name: 'lh-redis'
image: 'redis:6'
nginx:
container_name: 'lh-nginx'
build: ./nginx
depends_on:
- php-fpm
volumes:
- ./src/lh-app:/var/www/html/app
- ./src/lh-api:/var/www/html/api
ports:
- "80:80"
- "443:443"
php-fpm:
container_name: 'lh-php'
image: docker.io/bitnami/php-fpm:8.0
user: '1000:1000'
build:
context: ./php-fpm
args:
- PHP_ENV= development
depends_on:
- postgres
- redis
volumes:
- ./src/lh-app:/var/www/html/app
- ./src/lh-api:/var/www/html/api
ERROR: The Compose file './docker-compose.yml' is invalid because:
Unsupported config option for services: 'postgres'
getting this error
I think you are missing some ENV vars.
This is our docker-compose.yml for Postgres
version: '3.9'
services:
db:
image: postgres:latest
restart: "no"
container_name: db
volumes:
- ./database:/var/lib/postgresql/data
ports:
- "8002:5432"
environment:
POSTGRES_PASSWORD: verySecurePassword34058
POSTGRES_USER: root
POSTGRES_DB: myDatabase
networks:
default:
external: true
name: our-network
Other parts of the application, (like Redis, the NodeJS App, etc) are in other docker-compose.yml files, But since they share the same network, they talk to each other.
You have not mentioned version in docker-composer.yml
version: '2'
services:
postgres:
container_name: 'lh-postgres'
image: 'postgres:13'
environment:
POSTGRES_PASSWORD: root
redis:
container_name: 'lh-redis'
image: 'redis:6'
You should include your docker and docker-compose version in the querstion to help us answer you.
It would also be wise to define the version: 'x' element at the top of your compose file.
You may be suffering from an old version of the cli, akin to this question:
docker-compose : Unsupported config option for services service: 'web'

Docker Compose merge arrays for YAML aliases and anchors

I have the following broken docker-compose file
version: '3.4'
x-vols1: &vols-1
- /home/:/home/
x-vols2: &vols-2
- /tmp/:/tmp/
services:
app1:
container_name: app1
image: app1
volumes:
<<: *vols-1
app2:
container_name: app2
image: app2
volumes:
<<: *vols-1
<<: *vols-2
This fails with the following error
$ docker-compose -f test.yaml config
ERROR: yaml.constructor.ConstructorError: while constructing a mapping
in "./test.yaml", line 14, column 13
expected a mapping for merging, but found scalar
in "./test.yaml", line 4, column 7
Question 1: How can I merge arrays in docker-compose? The syntax that I am trying to use is the one for merging dicts
Question 2: If there is no way to merge arrays, is there a workaround?
Use case: I have multiple services, some of them map some volumes, others map other volumes, others map all volumes. I would like to not repeat myself.
Thank you!
The Yaml merge syntax is for merging mappings, not for arrays. For more on that, see this issue. However, if you are just adding single volumes, you don't need to merge anything. Just insert the alias as an array entry:
version: '3.4'
x-vols1: &vols-1
"/home/:/home/"
x-vols2: &vols-2
"/tmp/:/tmp/"
services:
app1:
container_name: app1
image: app1
volumes:
- *vols-1
app2:
container_name: app2
image: app2
volumes:
- *vols-1
- *vols-2
The desired behavior can be achieved by using multiple docker-compose files, one for each volume. Note that the anchors and aliases are not required, but keeping them in to align with the question.
base.yaml
version: '3.4'
services:
app1:
container_name: app1
image: app1
app2:
container_name: app2
image: app2
vol1.yaml
version: '3.4'
x-vols1: &vols-1
volumes:
- /home/:/home/
services:
app1:
container_name: app1
image: app1
<<: *vols-1
app2:
container_name: app2
image: app2
<<: *vols-1
vol2.yaml
version: '3.4'
x-vols2: &vols-2
volumes:
- /tmp/:/tmp/
services:
app2:
container_name: app2
image: app2
<<: *vols-2
Verify as
$ docker-compose -f base.yaml -f vol1.yaml -f vol2.yaml config
Result
services:
app1:
container_name: app1
image: app1
volumes:
- /home:/home:rw
app2:
container_name: app2
image: app2
volumes:
- /home:/home:rw
- /tmp:/tmp:rw
version: '3.4'
Additional documentation https://docs.docker.com/compose/extends/
This is a slight variation on the currently accepted answer, which doesn't require you to change your docker-compose command line to include other files:
docker-compose.yml
version: '3.9'
services:
app1:
container_name: app1
image: app1
extends:
file: common-volumes.yml
service: app1
app2:
container_name: app2
image: app2
extends:
file: common-volumes.yml
service: app2
volumes:
- /tmp/:/tmp/
common-volumes.yml
x-vols1: &vols-1
volumes:
- /home/:/home/
services:
app1:
<<: *vols-1
app2:
<<: *vols-1
To view the resulting config:
docker compose config

Resources