How do I add a table in MySQL using docker-compose - docker

I am using docker-compose to create a web application using PHP and MySQL.The container that runs MySQL has the database installed however, I am not able to find the table in there. I have made use of a volume that contains the table .sql extension).
Below is my docker-compose.yml file
`version: '3'
services:
web_1:
container_name: web1_2
build: .
ports:
- "8081:80"
networks:
test:
ipv4_address: 172.28.0.2
web_2:
container_name: web2_2
build: .
ports:
- "8082:80"
networks:
test:
ipv4_address: 172.28.0.3
lb_2:
container_name: lb_2
build: ./lb
ports:
- "8080:80"
networks:
test:
ipv4_address: 172.28.0.4
db_2:
container_name: db_2
image: mysql:latest
restart: always
volumes:
- dbdata:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: cloud
MYSQL_USER: root
MYSQL_PASSWORD: password
ports:
- "3306:3306"
networks:
test:
ipv4_address: 172.28.0.5
networks:
test:
ipam:
driver: default
config:
- subnet: 172.28.0.0/16
volumes:
dbdata:
`
This is the sql file present on the hostmachine which I am mountig on
/var/lib/sql on the docker container.
`CREATE TABLE cloud.basic (name VARCHAR(20), age INT, location VARCHAR(20));
INSERT INTO TABLE cloud.basic VALUES ("Sampy", 23, "Boulder");
INSERT INTO TABLE cloud.basic VALUES ("Gian", 27, "Raton");`
Where doyou think I am going wrong? Is my approach wrong? Any help will be greatly appreciated

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:

How can I fix this issue with Yaml file?

Hi I am trying to run docker-compose build --no-cache but I keep getting errors.
yaml.parser.ParserError: while parsing a block mapping
in ".\docker-compose.yml", line 1, column 1
expected <block end>, but found '<block mapping start>'
in ".\docker-compose.yml", line 2, column 5
docker-compose.yml:
Please help me to to format this I looked online but I couldn't find any linter that could do this I keep getting errors.
Thanks.
This is my docker composer file:
s is my docker file docker-compose.yml
version: '3'
services:
#PHP-FPM service
app:
build:
context: .
dockerfile: Dockerfile
container_name: store
restart: unless-stopped
tty: true
working_dir: /var/www
volumes:
- ./:/var/www
- ./docker-files/php/local.ini:/usr/local/etc/php/conf.d/local.ini
networks:
-app-network
#Nginix service
webserver:
image: nginx-alpine
container_name: store-webserver
restart: unless-stopped
tty: true
ports:
- "8100:80"
- "8143:443"
volumes:
- ./:/var/www
- ./docker-files/nginx/conf.d:/etc/nginx/conf.d/
networks:
-app-network
#MariaDB service
db:
image: mariadb:10.5.6
container_name: store-mariadb
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: store
MYSQL_USER: store
MYSQL_PASSWORD: root
volumes:
- mariadbData:/var/lib/mysql
- ./docker-files/mariadb/my.cnf:/etc/mysql/my.cnf
networks:
-app-network
#Volumes
volumes:
mariadbData:
driver: local
#Networks
networks:
app-network:
driver: bridge
I have noticed some spacing issues on your YAML file, one of them is that the version and services sections are not on the same level, you have to know that docker-compose files and YAML have a strict syntax and a spacing might ruin everything for you here is a fix to your compose file, make sure to learn and read more about docker-compose documentation.
Not sure what's the behavior of your environment but here is a correct YAML syntax for your example:
version: '3'
services:
#PHP-FPM service
app:
build:
context: .
dockerfile: Dockerfile
container_name: store
restart: unless-stopped
tty: true
working_dir: /var/www
volumes:
- ./:/var/www
- ./docker-files/php/local.ini:/usr/local/etc/php/conf.d/local.ini
networks:
- app-network
#Nginix service
webserver:
image: nginx-alpine
container_name: store-webserver
restart: unless-stopped
tty: true
ports:
- "8100:80"
- "8143:443"
volumes:
- ./:/var/www
- ./docker-files/nginx/conf.d:/etc/nginx/conf.d/
networks:
- app-network
#MariaDB service
db:
image: mariadb:10.5.6
container_name: store-mariadb
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: store
MYSQL_USER: store
MYSQL_PASSWORD: root
volumes:
- mariadbData:/var/lib/mysql
- ./docker-files/mariadb/my.cnf:/etc/mysql/my.cnf
networks:
- app-network
#Volumes
volumes:
mariadbData:
driver: local
#Networks
networks:
app-network:
driver: bridge

docker-compose + symfony in windows env

i've installed docker (windows 10) with wsl2 (ubuntu distro) and added my docker-compose.yml
version: '3'
services:
web:
image: nginx:1.20.1
container_name: web
restart: always
ports:
- "80:80"
volumes:
- ./nginx.d.conf:/etc/nginx/conf.d/nginx.conf
- ./nginx.conf:/etc/nginx/nginx.conf
- ./www/my-app:/app
php:
build:
context: .
dockerfile: myphp.dockerFile
container_name: php
restart: always
depends_on:
- web
volumes:
- ./www/my-app:/app
mysql:
image: mariadb:10.3.28
container_name: mysql
restart: always
depends_on:
- php
environment:
MYSQL_ROOT_PASSWORD: '******'
MYSQL_USER: 'root'
MYSQL_PASSWORD: '******'
MYSQL_DATABASE: 'my-database'
command: ["--default-authentication-plugin=mysql_native_password"]
volumes:
- mysqldata:/var/lib/mysql
- ./my.cnf:/etc/mysql/my.cnf
ports:
- 3306:3306
cache:
image: redis:5.0.3
container_name: cache
restart: always
ports:
- 6379:6379
networks:
- my-network
volumes:
- ./cache:/cache
volumes:
mysqldata: {}
networks:
my-network:
driver: "bridge"
So my symfony code is in the /www/my-app window's folder. This includes the /www/my-app/vendor too.
My application is running extremely slow (50-70 seconds). If i'm correct it's because the vendor folder is huge (80MB) and docker creates an image of it every time. Other discussions mentioned that vendor folder sould be moved into a new volume, and here i'm stuck with it. How to move and mount that in this case, and how should the docker-compose.yml look like after it?

Docker shared network with multiple nginx instances, gives random nginx instance

I am setting up the following docker containers with the following 2 docker-compose files:
version: '3.7'
services:
mysql:
image: mysql:5.7
restart: on-failure
environment:
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
volumes:
- db_data:/var/lib/mysql:rw
ports:
- '${MYSQL_PORT}:3306'
networks:
- shared_mysql
php:
build:
context: .
dockerfile: docker/php/Dockerfile
restart: on-failure
volumes:
- '../:/usr/src/app'
user: ${LOCAL_USER}
networks:
- shared_mysql
api_nginx:
image: nginx:1.15.3-alpine
restart: on-failure
volumes:
- '../public/:/usr/src/app'
- './docker/nginx/default.conf:/etc/nginx/conf.d/default.conf:ro'
ports:
- '21180:80'
depends_on:
- php
networks:
- shared_mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin
restart: on-failure
ports:
- '${PHPMYADMIN_PORT}:80'
environment:
PMA_HOST: mysql
MYSQL_USERNAME: ${MYSQL_USER}
MYSQL_ROOT_PASSWORD: ${MYSQL_PASSWORD}
networks:
- shared_mysql
volumes:
db_data:
networks:
shared_mysql:
version: '3.7'
services:
php:
build:
context: .
dockerfile: docker/php/Dockerfile
restart: on-failure
volumes:
- '../:/usr/src/app'
user: ${LOCAL_USER}
networks:
- api_21s_shared_mysql
auth_nginx:
image: nginx:1.15.3-alpine
restart: on-failure
volumes:
- '../public/:/usr/src/app'
- './docker/nginx/default.conf:/etc/nginx/conf.d/default.conf:ro'
ports:
- '21181:80'
depends_on:
- php
networks:
- api_21s_shared_mysql
volumes:
db_data:
networks:
api_21s_shared_mysql:
external: true
When I visit http://localhost:21181/, I always get the correct website.
But when I visit http://localhost:21182/, I get http://localhost:21181/ or http://localhost:21182/ random.
I tried to set up the network sepperate.
I'd like it to work with the portnumbers, but I don't want them to be mixed up.
I am hoping someone can help me. Thank you in advance.
When services are started with docker-compose, they are discoverable within the docker network in different ways: by service name, by container name, by IP, etc.
In your case you use the discovery by service name, since in your nginx configuration you have the reference "php:9000".
At this point docker looks for a service named "php" and finds 2. It interprets them as replicas of the same service and sends traffic to them following a round-robin pattern (first request to first instance of the service, second to the second instance of the service, third to the first instance, forth to the second instance, etc.)
Solution
Name the services differently, just like you already do with your nginx services (auth_nginx and api_nginx).
Then in your default.conf for both services change the line referring to php:9000 accordingly.

Docker-Compose v3 - static ip

I am trying to set a static IP on my docker-compose v3 file but.. i can't.
Each time I set it, I can't connect to the webpage anymore.
I am getting ERR_ADDRESS_UNREACHABLE
Here is my config:
# docker-compose.yml
version: '3'
services:
web:
build: ./etc/nginx
ports:
- "90:80"
volumes:
- "./etc/ssl:/etc/ssl"
depends_on:
- php
- database
php:
build: ./etc/php
ports:
- 9000:9000
links:
- database:mysqldb
volumes:
- "./etc/php/php.ini:/usr/local/etc/php/conf.d/php.ini"
- ${APP_PATH}:/var/www/symfony
database:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
ports:
- 3300:3306
volumes:
- "./data/db/mysql:/var/lib/mysql"
and
# docker-compose.override.yml
version: '3'
services:
web:
networks:
test:
ipv4_address: '10.1.0.100'
networks:
test:
ipam:
driver: default
config:
- subnet: 10.1.0.0/24
It should be like this:
services:
web:
networks:
test:
ipv4_address: 10.1.0.100
networks:
test:
driver: bridge
ipam:
driver: default
config:
- subnet: 10.1.0.0/24
And in my case I placed networks section before services.
UPDATE:
eventually I've ended up using external network for like this:
docker network create --subnet 10.5.0.0/24 local_network_dev
and then in any docker-compose you can just use it like this:
version: '3.2'
networks:
default:
external:
name: local_network_dev
and inside of image:
web:
networks:
default:
ipv4_address: 10.5.0.11
# Use root/example as user/password credentials
version: '3.3'
networks:
netBackEnd:
ipam:
driver: default
config:
- subnet: 192.168.0.0/24
services:
mongo-db:
image: mongo
container_name: cnt_mongo
restart: always
environment:
MONGO_INITDB_DATABASE: dbArland
MONGO_INITDB_ROOT_USERNAME: maguilarac
MONGO_INITDB_ROOT_PASSWORD: pwdmaguilarac
ports:
- 27017:27017
volumes:
- ./script1_creacion_usuario.js:/docker-entrypoint-initdb.d/script1_creacion_usuario.js:ro
- ./script2_creacion_coleccion.js:/docker-entrypoint-initdb.d/script2_creacion_coleccion.js:ro
- ./script4_carga_productos.js:/docker-entrypoint-initdb.d/script4_carga_productos.js:ro
- ./productos_inicial.json:/docker-entrypoint-initdb.d/productos_inicial.json:ro
- ./mongo-volume:/data/db
networks:
netBackEnd:
ipv4_address: 192.168.0.4
mongo-express:
image: mongo-express
container_name: cnt_mongo-express
restart: always
ports:
- 9081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: maguilarac
ME_CONFIG_MONGODB_ADMINPASSWORD: pwdmaguilarac
networks:
netBackEnd:
ipv4_address: 192.168.0.6
Just a very important note that should all future users know.
If you are trying to edit already existing network, you will most likely get error
Cannot start service xxx: Invalid address xxx.xxx.xxx.xxx: It does not belong to any of this network's subnets
I have been struggling for about 2 hours with this problem. The solution is to set a different name for the network or propably use docker-compose down.
Networks settings is not much different between versions 2 and 3, but here is a good link to official doc (v3): https://docs.docker.com/compose/compose-file/compose-file-v3/#ipv4_address-ipv6_address

Resources