Environment variables are not updating after changes in docker-compose - docker

I try to deploy mysql server on docker and when I try to change enviroment variables in my docker-compose.yml file they just don't change when I do docker-compose up.
I have noticed, that when I will remove container using docker container remove <process ID> and again try to docker-compose up it will works. If I will change name of the service in docker-compose.yml it will work too. Do I have always to remove stopped containers to restart enviroments variables or am I doing something wrong?
My docker-compose.yml file:
version: '3.3'
services:
db:
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: 'db'
MYSQL_USER: 'user'
MYSQL_PASSWORD: 'password1'
MYSQL_ROOT_PASSWORD: 'password1'
ports:
- '3306:3306'
expose:
- '3306'

Related

How can I get a full Backup from a Docker Container

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

Lost volume when run docker-compose

I had running container with wordpress and other container with MySQL and its volume (see config below). Then I've run docker-compose up -d --build and then I see wordpress installation page, that means empty database. Also I could kill some docker process before that, don't remember exactly which by command ```sudo kill -9 [process_id].
Where can I find solution to restore my volume with all the information this has?
services:
wordpress:
depends_on:
- db
image: wordpress:latest
volumes:
- ./uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
- wordpress_files:/var/www/html
ports:
- "80:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: my_wordpress_db_password
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: my_db_root_password
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: my_wordpress_db_password
volumes:
wordpress_files:
db_data:
uploads.ini:
You should take a look at the output of "docker volume ls". If you were using the container only with the docker run command and now switched to compose, maybe you when you runned docker-compose up Docker created a new empty volume called db_db_data.

How to pass start args to docker compose file like docker run --opts?

I have write a docker compose file, I want to start compose use
docker-compose up -d
But I want to pass args to my images
It look like
docker run --security-opt=seccomp:unconfined mysql:8.0
But I find compose file only have basic config such as network, volume, environment
My compose file
db:
image: mysql:8.0
container_name: onlinecodedb
volumes:
- onlinecode-database:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: mysqlrootpassword
MYSQL_PASSWORD: mysqlpassword
MYSQL_USER: mysql
MYSQL_DATABASE: onlinecode
ports:
- "3300:3306"
networks:
- onlinecode-net
How I solve this problem?
Most of the docker run options have matching Docker Compose options. In particular, docker run --security-opt maps to a security_opt: field.
db:
image: 'mysql:8.0'
security_opt:
- 'seccomp:unconfined'
et: cetera

docker-compose not restarting correctly

i have the following docker-compose.yml
The restart of the mediawiki_db container is working.
The problem is, that the mediawiki container will not restart after reboot besides the fact that it also has the restart: always policy.
# MediaWiki with MariaDB
#
# Access via "http://localhost:8080"
# (or "http://$(docker-machine ip):8080" if using docker-machine)
version: '2'
services:
mediawiki_db:
image: mariadb
restart: always
container_name: mediawiki_db
volumes:
- ~/wiki/mysql:/var/lib/mysql
environment:
# #see https://phabricator.wikimedia.org/source/mediawiki/browse/master/includes/DefaultSettings.php
MYSQL_DATABASE: 'my_wiki'
MYSQL_USER: 'wikiuser'
MYSQL_PASSWORD: 'pw'
MYSQL_RANDOM_ROOT_PASSWORD: 'yes'
mediawiki:
image: mediawiki
restart: always
container_name: mediawiki
ports:
- 8080:80
volumes:
- ~/wiki/var/www/html/images:/var/www/html/images
# After initial setup, download LocalSettings.php to the same directory as
# this yaml and uncomment the following line and use compose to restart
# the mediawiki service
- ./LocalSettings.php:/var/www/html/LocalSettings.php
depends_on:
- mediawiki_db
What is it i am doing wrong?
As far as I can tell from the MediaWiki DockerFile, there is no CMD being run when you start up the container.
Someone correct me if I'm wrong, but I think the restart: always can only work if there is a command that docker-compose is monitoring. And therefore restart the container with.

Docker-Compose persistent data MySQL

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

Resources