This message to clear up a problem I am having with the use of docker with my php application.
Indeed, I execute locally my dockers images (nginx, phpmyadmin and php with my application) and everything works fine.
However, I use a volume mounted in my container app with php which allows me to be able to modify hot files (without need to build at each edit).
However when I push this image to a repository and I pull it on another desktop, the volume containing my application is not there.
Have you ever faced this concern?
Please find my docker-compose.yml and Dockerfile :
docker-compose.yml
version: "3.7"
services:
app:
build:
args:
user: web
uid: 1000
context: ./
dockerfile: Dockerfile
image: myblog
container_name: myblog-app
restart: unless-stopped
working_dir: /var/www/
volumes:
- ./:/var/www
networks:
- myblog
db:
image: mysql:5.7
container_name: myblog-db
restart: unless-stopped
environment:
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_USER: ${DB_USERNAME}
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- ./.docker/mysql/database.sql:/docker-entrypoint-initdb.d/init.sql
- ./.docker/mysql/data:/var/lib/mysql
networks:
- myblog
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
ports:
- 8002:80
environment:
PMA_HOST: db
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
networks:
- myblog
nginx:
image: nginx:alpine
container_name: myblog-nginx
restart: unless-stopped
ports:
- 8000:80
volumes:
- ./:/var/www
- ./.docker/nginx/conf.d:/etc/nginx/conf.d
networks:
- myblog
networks:
myblog:
driver: bridge
Dockerfile
FROM php:7.3-fpm
# Arguments defined in docker-compose.yml
ARG user
ARG uid
# Install system dependencies
RUN apt-get update && apt-get install -y
git
curl
libpng-dev
libonig-dev
libxml2-dev
libzip-dev
zip
unzip
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd
RUN docker-php-ext-install zip
RUN docker-php-ext-enable zip
# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
# Create system user to run Composer and Artisan Commands
RUN useradd -G www-data,root -u $uid -d /home/$user $user
RUN mkdir -p /home/$user/.composer &&
chown -R $user:$user /home/$user
# Set working directory
WORKDIR /var/www
USER $user
Thanks in advance
3
Answers
Many thanks for your clear reply.
Now i understand what is wrong with my configuration and what i need to do. For the people who search a solution to manage in the same Dockerfile the development environment and the production, you can use an Argument :)
This is expected behavior, because the data inside Volumes are not part of an image. Volumes are used to persist data generated in containers or to pass dynamic data into containers via bind-mounts e.g. configs, credentials or certificates.
https://docs.docker.com/storage/volumes/
Your
docker-compose.yml
and its services using volumes mounting your local directory via- .:/path/to/dir
are only good for local development, because you may see changes of your application instant and without having to rebuild images.If you want to see your code inside the images on another machine you need to use
COPY
in yourDockerfile
, rebuild the image and push every time you change your code !You will also need to change your
docker-compose.yml
by adding volumes.https://docs.docker.com/compose/compose-file/#volumes
Have you tried using a named volume instead of a path based volume?
This will let Docker manage the volume more for you and may give you the behavior you want.
https://nickjanetakis.com/blog/docker-tip-28-named-volumes-vs-path-based-volumes
Docker volume does not persist data