My docker-compose.yml
version: "3.1"
services:
www:
build:
context: .
dockerfile: Dockerfile.lamp
ports:
- "${WEBSERVER_PORT}:80"
volumes:
- ./www:/var/www/html/
links:
- db
networks:
- default
db:
image: mysql:latest
ports:
- "3306:3306"
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
MYSQL_ROOT_PASSWORD: ${MYSQL_PASSWORD}
volumes:
- ./sql:/docker-entrypoint-initdb.d
- ./conf:/etc/mysql/conf.d
- mysql_vol:/var/lib/mysql
networks:
- default
phpmyadmin:
image: phpmyadmin/phpmyadmin
links:
- db:db
ports:
- ${PHPMYADMIN_PORT}:80
environment:
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
MYSQL_ROOT_PASSWORD: ${MYSQL_PASSWORD}
UPLOAD_LIMIT: 64M
volumes:
mysql_vol:
external: false
dockerfile:
FROM php:8.1-apache
RUN a2enmod rewrite
RUN docker-php-ext-install pdo pdo_mysql
# Install system dependencies
RUN apt-get update && apt-get install -y \
git \
libicu-dev \
zlib1g-dev \
g++\
libpq-dev \
libmcrypt-dev \
git \
zip \
unzip
# Install PHP extensions
RUN docker-php-ext-install pdo_mysql
RUN docker-php-ext-install mysqli
RUN docker-php-ext-configure intl \
&& docker-php-ext-install intl
However, after running "docker compose up -d",
the folder "mysql_vol" stays empty and the result of "docker volume inspect test_project_mysql_vol" yields:
[
{
"CreatedAt": "2023-02-15T16:07:08+01:00",
"Driver": "local",
"Labels": {
"com.docker.compose.project": "test_project",
"com.docker.compose.version": "2.15.1",
"com.docker.compose.volume": "mysql_vol"
},
"Mountpoint": "/var/lib/docker/volumes/test_project_mysql_vol/_data",
"Name": "test_project_mysql_vol",
"Options": null,
"Scope": "local"
}
]
How could it be that, while setting external: false, the mountpoint still is not in the folder as the docker-compose.yml file, but still in /var/lib/docker/volumes?
If you are trying to refer to a local directory, you need to write ./mysql_vol:
db:
image: mysql:latest
ports:
- "3306:3306"
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
MYSQL_ROOT_PASSWORD: ${MYSQL_PASSWORD}
volumes:
- ./sql:/docker-entrypoint-initdb.d
- ./conf:/etc/mysql/conf.d
- ./mysql_vol:/var/lib/mysql
networks:
- default
If you write mysql_vol without the leading ./, you are referring to a named Docker volume which will be allocated automatically by Docker from its own storage.
The external keyword is used when you want to refer to an existing named volume that is not managed by docker compose. Setting external: false is a no-op because that's the default.
Related
Here's what I've tried..
docker-compose.yml
version: '3'
services:
#PHP Service
app:
build:
context: .
dockerfile: Dockerfile
image: digitalocean.com/php
container_name: app
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
working_dir: /var/www
volumes:
- ./:/var/www
- ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
networks:
- app-network
#Nginx Service
webserver:
image: nginx:alpine
container_name: webserver
restart: unless-stopped
tty: true
ports:
- "8235:80"
- "455:443"
volumes:
- ./:/var/www
- ./nginx/conf.d/:/etc/nginx/conf.d/
- ./nginx/ssl:/etc/nginx/ssl
networks:
- app-network
# try this...
memcached:
container_name: memcached
image: memcached:latest
ports:
- "11212:11211"
networks:
- app-network
#Docker Networks
networks:
app-network:
driver: bridge
#Volumes
volumes:
dbdata:
driver: local
Dockerfile
# ... omitted for brevity
# try this..
RUN apt-get update && apt-get install -y \
memcached
# now try this...
RUN docker-php-ext-install libz-dev libmemcached-dev
RUN docker-php-ext-install pecl install memcached
RUN docker-php-ext-install docker-php-ext-enable memcached
# and this...
RUN docker-php-ext-install build-essential libmemcached-dev libz-dev
RUN pecl install memcached-2.2.0
RUN echo extension=memcached.so >> /usr/local/etc/php/conf.d/memcached.ini
The local site is up and serving files (Symfony) only I'm met with the memcached exception:
Any ideas?
Dropping this in my Dockerfile fixed it
RUN apt-get update && apt-get install -y libmemcached-dev \
&& pecl install memcached \
&& docker-php-ext-enable memcached
I'm trying to mount a docker-compose with its respective dockerfile to have a project with a /backend/ folder where an API with laravel will go and then a /frontend/ folder where a react frontend goes.
The goal is to build on it so I need to use volumes with persistent data.
The build does it perfectly and I can enter each of the dockers, the problems I have are:
The volumes are not persistent, that is, I make a change in the frontend directory (react) and I don't see the changes, I only see them if I do another build.
From the backend container I want to be able to access the DB to launch the Laravel migrations and I get an error that it can't find the schema.
Docker-compose.yaml
version: '3.7'
services:
frontend:
build:
context: ./app-frontend
ports:
- "81:80"
networks:
- app-network
backend:
build:
context: ./app-backend/
restart: unless-stopped
environment:
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_USER: ${DB_USERNAME}
working_dir: /var/www
volumes:
- ./app-backend:/var/www
ports:
- "8001:8000"
networks:
- app-network
- db
appdb:
image: mysql:5.7
restart: unless-stopped
environment:
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_USER: ${DB_USERNAME}
volumes:
- ./initdb:/docker-entrypoint-initdb.d
ports:
- "3307:3306"
networks:
- db
networks:
app-network:
driver: bridge
db:
Dockerfile backend
FROM php:7.4-apache
RUN a2enmod rewrite
RUN apt-get update && apt-get install -y \
libzip-dev \
zlib1g-dev \
libicu-dev \
libxml2-dev \
libpq-dev \
vim \
libpng-dev \
&& docker-php-ext-install pdo pdo_mysql zip intl xmlrpc soap opcache \
&& docker-php-ext-configure pdo_mysql --with-pdo-mysql=mysqlnd
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
COPY docker/php/https://php.ini /usr/local/etc/php
COPY docker/apache/https://apache2.conf /etc/apache2/https://apache2.conf
COPY docker/apache/https://vhost.conf /etc/apache2/sites-available/000-https://default.conf
ENV COMPOSER_ALLOW_SUPERUSER 1
COPY . /var/www
RUN chown -R www-data:www-data /var/www/html
WORKDIR /var/www
RUN composer install
Dockerfile frontend
FROM node:13.10.1-alpine
WORKDIR /app
COPY . .
RUN npm install
RUN npm run build
ENTRYPOINT npm start
I've got the following docker-compose
version: "3.9"
services:
php-apache:
image: php:7.3-apache
command:
- --default-authentication-plugin=mysql_native_password
- docker-php-ext-install
- mysqli pdo
- pdo_mysql
container_name: php-apache-container
depends_on:
- mysql
restart: always
volumes:
- ./api/:/var/www/html/
ports:
- 8010:80
links:
- mysql
mysql:
image: mysql:5.7
container_name: mysql-container
restart: always
environment:
TZ: "America/New_York"
MYSQL_ALLOW_EMPTY_PASSWORD: "no"
MYSQL_ROOT_PASSWORD: root_password
MYSQL_DATABASE: db_name
MYSQL_USER: db_user
MYSQL_PASSWORD: db_password
volumes:
- mysql-data:/var/lib/mysql
volumes:
mysql-data:
The php-apache-container keeps restarting.
But if I change this:
php-apache:
image: php:7.3-apache
command:
- --default-authentication-plugin=mysql_native_password
- docker-php-ext-install
- mysqli pdo
- pdo_mysql
into this:
php-apache:
build:
context: .
dockerfile: dockerfile
command: --default-authentication-plugin=mysql_native_password
And the dockerfile have the following content:
FROM php:7.3-apache
RUN docker-php-ext-install mysqli pdo pdo_mysql
It works, but I don't want to use a dockerfile, how can I make it work without using the dockerfile?
Thanks in advance!!!
I figured it out. I treated the command as a list but instead I added new lines with command and it works, looks like this:
php-apache:
image: php:7.3-apache
command: docker-php-ext-install
command: mysqli pdo
command: pdo_mysql
command: --default-authentication-plugin=mysql_native_password
container_name: php-apache-container
I'm trying to dockerize my Symfony project.
In this project, I have a folder under: public/fichiersflux/
"fichiersflux" is a folder with persistent data (img, pdf...)
Here is the docker-compose.yml:
version: '3.7'
services:
mariadb:
image: mariadb:10.4
restart: always
environment:
MYSQL_ROOT_PASSWORD: zfezZEFfz4e1589fze
MYSQL_DATABASE: 1c1t
MYSQL_USER: 1c1t
MYSQL_PASSWORD: fez45FZE1fez0fzefF!
ports:
- 3306:3306
php:
image: php:7.4
build:
context: .
dockerfile: docker/php/Dockerfile
restart: on-failure
user: 1000:1000
nginx:
image: nginx:1.17-alpine
restart: on-failure
volumes:
- './app/public/:/usr/src/app'
- './docker/nginx/default.conf:/etc/nginx/conf.d/default.conf:ro'
ports:
- 8080:80
depends_on:
- php
And my Dockerfile:
# ./docker/php/Dockerfile
FROM php:7.4-fpm
RUN docker-php-ext-install pdo_mysql
RUN pecl install apcu
RUN apt-get update && \
apt-get install -y \
zlib1g-dev
RUN apt-get install -y \
libzip-dev \
libicu-dev \
zip \
&& docker-php-ext-install zip
RUN docker-php-ext-enable apcu \
&& docker-php-ext-install intl
# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
WORKDIR /usr/src/app
COPY app/ /usr/src/app
RUN chown -R 1000:1000 /usr/src/app
RUN PATH=$PATH:/usr/src/apps/vendor/bin:bin
The problem is, when I build my docker containers, the folder /usr/src/app/ is apparently re-created.. and I loose all data inside public/fichiersflux
How can I persist public/fichiersflux folder ?
Best regards :)
You could just add volumes on php services, to mount your target folder.
version: '3.7'
services:
mariadb:
image: mariadb:10.4
restart: always
environment:
MYSQL_ROOT_PASSWORD: zfezZEFfz4e1589fze
MYSQL_DATABASE: 1c1t
MYSQL_USER: 1c1t
MYSQL_PASSWORD: fez45FZE1fez0fzefF!
ports:
- 3306:3306
php:
image: php:7.4
build:
context: .
dockerfile: docker/php/Dockerfile
volumes:
- '../app/public/fichiersflux:/usr/src/app/fichiersflux'
restart: on-failure
user: 1000:1000
nginx:
image: nginx:1.17-alpine
restart: on-failure
volumes:
- './app/public/:/usr/src/app'
- './docker/nginx/default.conf:/etc/nginx/conf.d/default.conf:ro'
ports:
- 8080:80
depends_on:
- php
My dockerfile
FROM php:7.0-apache
RUN pecl install -o -f redis
RUN pecl install -o -f xdebug
RUN docker-php-ext-enable redis
RUN docker-php-ext-enable xdebug
RUN docker-php-ext-install pdo pdo_mysql mysqli
RUN apt-get update
RUN apt-get install locales-all -y
My docker-compose file
version: "3.7"
services:
redis:
restart: unless-stopped
image: redis:alpine
container_name: redis_mp
volumes:
- redis-data:/data
# if we need a custom redis configuration
# - ./docker//usr/local/etc/redis/redis.conf:/usr/local/etc/redis/redis.conf
mysql:
restart: unless-stopped
image: mysql:5.6
container_name: mysql_mp
command: --default-authentication-plugin=mysql_native_password
ports:
# So you can use a database client on your host machine
- 3306:3306
environment:
- MYSQL_ROOT_PASSWORD=secret
volumes:
- mysql-data:/var/lib/mysql
# if we need a custom mysql configuration
# - ./docker/etc/mysql/conf.d:/etc/mysql/conf.d
apache:
restart: unless-stopped
# image: php:7.2-apache
build: ./docker/Dockerfiles/apache
container_name: apache_mp
# command: bash -c "docker-php-ext-install pdo pdo_mysql && service apache2 restart"
depends_on:
- mysql
- redis
- certbot
ports:
# Exposing both http and https
- 80:80
- 443:443
environment:
- APACHE_DOCUMENT_ROOT=/var/www/html/public/
volumes:
- .:/home/medpets/WWW/
- ./docker/etc/apache2:/etc/apache2/
- ./docker/usr/local/etc/php/php.ini:/usr/local/etc/php/php.ini
- apache-log:/home/medpets/LOG/
- ./docker/etc/letsencrypt:/etc/letsencrypt/
- ./docker/etc/ssl:/etc/ssl/
certbot:
restart: "no"
image: certbot/certbot
container_name: certbot_mp
volumes:
- ./docker/etc/letsencrypt:/etc/letsencrypt/
volumes:
mysql-data:
redis-data:
apache-log:
The problem