I have the following in my docker-compose.yml file:
version: '3.9'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
- db_import:/import
restart: always
ports:
- "3338:3306"
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
volumes:
db_data: {}
db_import: {}
dist: {}
The problem is that even though I have a dump.sql file in my db_import folder (inside the same folder where docker-compose.yml resides), I find that docker-compose exec db ls -al /import shows an empty directory. I've tried restarting all docker containers without any improvement.
Why is this directory not passing my content through?
By writing:
volumes:
- db_data:/var/lib/mysql
- db_import:/import
(...)
volumes:
db_data: {}
db_import: {}
dist: {}
You created so called Named Volumes with names db_data, db_import and dist. Docker doesn't tell us where those volumes are stored (and we shouldn't care about that). That kind of volumes is used to share data between containers and they do not have access to anything from your host machine.
If you want to share files beetween your host and container you must use Mount Binds instead - syntax is almost identical, you just need to replace db_data and db_import with absolute paths to that directories:
version: '3.9'
services:
db:
image: mysql:5.7
volumes:
- /PATH/TO/db_data:/var/lib/mysql
- /PATH/TO/db_import:/import
restart: always
ports:
- "3338:3306"
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
#volumes: That part isn't needed unless you use "dist" volume somewhere
# db_data: {}
# db_import: {}
# dist: {}
Related
I am trying to setup a WordPress project on my machine using Docker. This is my docker-compose.yml file code:
version: "3"
services:
# Database
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
networks:
- wp
# Web Server
wordpress:
ports:
- "4000:80"
depends_on:
- db
image: wordpress:latest
restart: always
volumes:
- "./html/:/var/www/html/"
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
networks:
- wp
networks:
wp:
volumes:
db_data:
This works fine but what it does is, that the files that are mounted inside ./html folder has user and group permission assigned as www-data:www-data. I am working on Ubuntu desktop OS. So every time I try to update any code inside ./html folder, I get permission denied message.
Is there any way I can fix this issue?
I have tried this command to add my user to www-data group but that didn't work aswell.
sudo usermod -aG www-data aslam
Try to match the user's id on the host machine to match that of www-data inside the container or vice-versa. May be read this for more info and howto.
To be clear for everyone else, adding user: 0:0 to the docker-compose.yml file solved this issue for me too.
There isn't a problem with file permissions when you mount the files on windows... but I feel like mounting to Windows isn't a good idea because the response time to the application is very slow that way.
Here's the full docker-compose.yml
Running this in your WSL of choice will prevent the permissions issues.
version: '3.3'
services:
wptoolkit:
# add user declaration to avoid windows permission issues when editing files and folders through your favorite editor
user: 0:0
# Ensure DB is up
depends_on:
- db
# We'll use the most recent Wordpress install
image: wordpress:latest
# Map files from container back to linux at this directory location
volumes:
- ./:/var/www/html
ports:
# We'll access our site on http://localhost:8999
- "80:80"
restart: always
environment:
# We'll provide some SQL credentials -- these match what's
# in our db section
- WORDPRESS_DB_HOST=db
- WORDPRESS_DB_USER=wordpress
- WORDPRESS_DB_PASSWORD=wordpress
- WORDPRESS_DB_NAME=wordpress
# This is our MySQL database server
db:
# We use a mariadb image which supports both amd64 & arm64 architecture
image: mariadb:10.6.4-focal
# If you really want to use MySQL, uncomment the following line
#image: mysql:8.0.27
command: '--default-authentication-plugin=mysql_native_password'
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
- MYSQL_ROOT_PASSWORD=wordpress
- MYSQL_DATABASE=wordpress
- MYSQL_USER=wordpress
- MYSQL_PASSWORD=wordpress
expose:
- 3306
- 33060
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: pma
links:
- db
environment:
PMA_HOST: db
PMA_PORT: 3306
PMA_ARBITRARY: 1
restart: always
ports:
- 8081:80
volumes:
db_data:
when I run this docker compose file I have a strange error. that you can see under the text. I'm new with dock so can anyone help me with this.
The error:
ERROR: The compose file './docker-compose.yaml is invalid because:
services.wordpress.build contains an invalid type, it should be a string, or an object
My docker-compose file:
version: "3.7"
services:
db:
build: ./db
container_name: db
ports:
- "3306:3306"
volumes:
- db_data:/var/lib.mysql
environment:
MYSQL_ROOT_PASSWORD: Test123
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: Test123
networks:
website_network:
aliases:
- wordpress
wordpress:
build:
container_name: wordpress_new
ports:
- "80:80"
networks:
website_network:
aliases:
- wordpress
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: Test123
WORDPRESS_DB_NAME: wordpress
networks:
website_networks:
name: website_network
volumes:
db_data:
driver: local
name: db_data
Your services.wordpress.build config option needs a value after it like you did in services.db.build: ./db. This value represents where to find your Dockerfile and the context where Docker will try to build your image.
Check the docs on how you can specify the build option.
This is what your file should look like in the wordpress section:
wordpress:
build: BUILD_PATH_HERE # you need a value here if you're building a new docker image
Per the docs, note that if your Dockerfile has an alternate name or you need a special build context, you'll need to specify those values as well as an object. For example:
wordpress:
build:
context: ./some/context/path # can be a relative or absolute path
dockerfile: Dockerfile-alternate-name
Alternatively, if you're using a prebuilt image, you can specify:
wordpress:
image: IMAGE_NAME
You are missing the build property value.
wordpress:
build: SOMETHING NEEDS TO BE HERE
container_name: wordpress_new
A path to where the build runs or a more complex object. Probably just the name of the folder where the container sources are.
This is how it can look like: object if it is an object. And this is how it can look like if it is just a path: path
More info: https://docs.docker.com/compose/compose-file/compose-file-v3/#build
I'm new to Docker and created myself a Container using a Compose File.
Now I came to a point where I wanted to use my development result in production.
Is there any way to backup the whole content so that I can use it in a production environment ?
The compose file that I used to spin up my Container:
version: '3.3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: myRootPassword
MYSQL_DATABASE: wordpress_oxygen
MYSQL_USER: wordpress_oxygen
MYSQL_PASSWORD: myDBPassword
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {}
Docker compose is not for production. For production try docker swarm.
Also you should add volume for wordpress, see example
https://hub.docker.com/_/wordpress
How backup volumes please see https://docs.docker.com/storage/volumes/#backup-restore-or-migrate-data-volumes
I followed multiple guides and tutorials.
This is my docker-compose file.
It gives the following error,
services.mysql.environment.volumes contains ["mysql_data:/var/lib/mysql"], which is an invalid type, it should be a string, number, or a null
And it creates a volume with the name _volumename?
I just don't seem to get the mistake i made.
version : '3'
services:
mysql:
container_name: auth_db
restart: always
image: mysql:latest
environment:
MYSQL_ROOT_PASSWORD: supersecret
MYSQL_DATABASE: name
MYSQL_USER: user
MYSQL_PASS: supersecret2
volumes:
- mysql_data:/var/lib/mysql
ports:
- 3306:3306
auth-service:
build: ./authservice
volumes:
- ./authservice:/usr/src/app
ports:
- 5000:5000
volumes:
mysql_data:
Some more information.
If i change the volume line to
volumes: "- mysql_data:/var/lib/mysql"
The docker-compose file will compile but
docker volume ls gives this output
local 4fa81a11596c2b67c2bb799d54afc6009ebcd82fcd10acae53a5aeefd005fd36
local 8636df909155569e8ebf0649f4c192616d0b6778d5eb7932b1f9542db55a07d8
local 832739c89f3b33ad0a1974ad7dc2ee9342373f904af3b2be5934331bae50b5e6
local e5bb4a869f54ce3200d5a1fe129bc1f8ee46515cf03d9dd2ff327430d792117b
local e35a38127fcb07702a58133883a021aa56c4aad6c439d254f32a119ad380d808
local hell_mysql_data
My current dir where the docker-compose file is located is HELL that why i think it got the prefix from
Your issue is regarding indentation, volumes and ports must be top level first inside your service definition, as follows:
version : '3'
services:
mysql:
container_name: auth_db
restart: always
image: mysql:latest
environment:
MYSQL_ROOT_PASSWORD: supersecret
MYSQL_DATABASE: name
MYSQL_USER: user
MYSQL_PASS: supersecret2
volumes:
- mysql_data:/var/lib/mysql
ports:
- 3306:3306
auth-service:
build: ./authservice
volumes:
- ./authservice:/usr/src/app
ports:
- 5000:5000
volumes:
mysql_data:
I can't seem to get MySQL data to persist if I run $ docker-compose down with the following .yml
version: '2'
services:
# other services
data:
container_name: flask_data
image: mysql:latest
volumes:
- /var/lib/mysql
command: "true"
mysql:
container_name: flask_mysql
restart: always
image: mysql:latest
environment:
MYSQL_ROOT_PASSWORD: 'test_pass' # TODO: Change this
MYSQL_USER: 'test'
MYSQL_PASS: 'pass'
volumes_from:
- data
ports:
- "3306:3306"
My understanding is that in my data container using volumes: - /var/lib/mysql maps it to my local machines directory where mysql stores data to the container and because of this mapping the data should persist even if the containers are destroyed. And the mysql container is just a client interface into the db and can see the local directory because of volumes_from: - data
Attempted this answer and it did not work. Docker-Compose Persistent Data Trouble
EDIT
Changed my .yml as shown below and created a the dir ./data but now when I run docker-compose up --build the mysql container wont start throws error saying
data:
container_name: flask_data
image: mysql:latest
volumes:
- ./data:/var/lib/mysql
command: "true"
mysql:
container_name: flask_mysql
restart: always
image: mysql:latest
environment:
MYSQL_ROOT_PASSWORD: 'test_pass' # TODO: Change this
MYSQL_USER: 'test'
MYSQL_PASS: 'pass'
volumes_from:
- data
ports:
- "3306:3306"
flask_mysql | mysqld: Can't create/write to file '/var/lib/mysql/is_writable' (Errcode: 13 - Permission denied)
flask_mysql | 2016-08-26T22:29:21.182144Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
flask_mysql | 2016-08-26T22:29:21.185392Z 0 [ERROR] --initialize specified but the data directory exists and is not writable. Aborting.
The data container is a superfluous workaround. Data-volumes would do the trick for you. Alter your docker-compose.yml to:
version: '2'
services:
mysql:
container_name: flask_mysql
restart: always
image: mysql:latest
environment:
MYSQL_ROOT_PASSWORD: 'test_pass' # TODO: Change this
MYSQL_USER: 'test'
MYSQL_PASS: 'pass'
volumes:
- my-datavolume:/var/lib/mysql
volumes:
my-datavolume:
Docker will create the volume for you in the /var/lib/docker/volumes folder. This volume persist as long as you are not typing docker-compose down -v
There are 3 ways:
First way
You need specify the directory to store mysql data on your host machine. You can then remove the data container. Your mysql data will be saved on you local filesystem.
Mysql container definition must look like this:
mysql:
container_name: flask_mysql
restart: always
image: mysql:latest
environment:
MYSQL_ROOT_PASSWORD: 'test_pass' # TODO: Change this
MYSQL_USER: 'test'
MYSQL_PASS: 'pass'
volumes:
- /opt/mysql_data:/var/lib/mysql
ports:
- "3306:3306"
Second way
Would be to commit the data container before typing docker-compose down:
docker commit my_data_container
docker-compose down
Third way
Also you can use docker-compose stop instead of docker-compose down (then you don't need to commit the container)
first, you need to delete all old mysql data using
docker-compose down -v
after that add two lines in your docker-compose.yml
volumes:
- mysql-data:/var/lib/mysql
and
volumes:
mysql-data:
your final docker-compose.yml will looks like
version: '3.1'
services:
php:
build:
context: .
dockerfile: Dockerfile
ports:
- 80:80
volumes:
- ./src:/var/www/html/
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: example
volumes:
- mysql-data:/var/lib/mysql
adminer:
image: adminer
restart: always
ports:
- 8080:8080
volumes:
mysql-data:
after that use this command
docker-compose up -d
now your data will persistent and will not be deleted even after using this command
docker-compose down
extra:- but if you want to delete all data then you will use
docker-compose down -v
You have to create a separate volume for mysql data.
So it will look like this:
volumes_from:
- data
volumes:
- ./mysql-data:/var/lib/mysql
And no, /var/lib/mysql is a path inside your mysql container and has nothing to do with a path on your host machine. Your host machine may even have no mysql at all. So the goal is to persist an internal folder from a mysql container.
Adding on to the answer from #Ohmen, you could also add an external flag to create the data volume outside of docker compose. This way docker compose would not attempt to create it. Also you wouldn't have to worry about losing the data inside the data-volume in the event of $ docker-compose down -v.
The below example is from the official page.
version: "3.8"
services:
db:
image: postgres
volumes:
- data:/var/lib/postgresql/data
volumes:
data:
external: true
Actually this is the path and you should mention a valid path for this to work. If your data directory is in current directory then instead of my-data you should mention ./my-data, otherwise it will give you that error in mysql and mariadb also.
volumes:
./my-data:/var/lib/mysql
Feasible bind mount solution:
mariadb:
image: mariadb:latest
restart: unless-stopped
environment:
- MARIADB_ROOT_PASSWORD=${MARIADB_ROOT_PASSWORD}
volumes:
- type: bind
source: /host/dir
target: /var/lib/mysql