docker-compose create a directory instead of mounting the file - docker

I have a very simple docker-compose.yml file where I use nginx and mounting a file as a volume.
But everytime I run the application, it is creating a directory .htpasswd without really mounting the .htpasswd file where I locally.
This is the docker-compose.yml.
version: '3'
services:
reverse:
container_name: reverse
hostname: reverse
restart: unless-stopped
image: nginx
ports:
- 80:80
- 443:443
volumes:
- ./nginx/.htpasswd:/etc/nginx/conf.d/.htpasswd
Can someone help me fix this?

by default if binded to a none existent path, docker will create a folder, the solution would be in your case to create the path before running your docker-compose

How are you running Docker? here's an answer ...
For Mac with Minikube/Hyperkit docker and Docker Compose
Since I'm not using Docker Desktop any longer, I've experienced numerous issues similar to "docker in docker (dind)" paradigm with minikube...
mount minikube
use absolute path
e.g., easiest way was to mount the exact home path...
minikube mount $HOME:/Users/<you>
... keeps running...
docker-compose.yaml
volumes:
- /Users/<you>/path/to/file.yaml:/somedir/file.yaml

Related

Command substitution in docker-compose.yml when scaling a service with multiple instances

The docker-compose.yml file I am using is following:
services:
web:
image: nginx:stable-alpine
ports:
- "8080-8130:80"
volumes:
- ${TEMPDIR}:/run
I launch 20 containers with the following command:
TEMPDIR=`mktemp -d` docker-compose up -d --scale web=20
All of the containers launch ok, but they all have the same temp volume mounted at /run, whereas I need each running container to have a unique temp volume. I understand that the problem is that the above yml file is doing Variable Substitution whereas what I need is Command Substitution but I could not find any way of doing that in the official reference, especially when launching multiple instances of the same docker image. Is there some way to fix this problem?
Maybe what you are looking for is tmpfs.
Mount a temporary file system inside the container. Can be a single value or a list.
You can use it simply like this
services:
web:
image: nginx:stable-alpine
ports:
- "8080-8130:80"
tmpfs: /run
Here are the references:
tmpfs on Docker
tmpfs on Docker compose

Docker docker-compose volumes delete sibling folders

This is docker compose file looks like
version: '3.3'
services:
portal:
ports:
- '8080:8080'
- '8000:8000'
environment:
- 'revcycle.portal.logger.root=C:/tomcat/logs/'
volumes:
- /src/main/webapp/sampleFiles:/usr/local/tomcat/webapps/portal/sampleFiles:rw
container_name: portal
image: 'portal:latest'
docker-compose up is creating container successfully by when i check the content of the tomcat webapp All the other sibling folder of the sampleFiles are deleted.
Am i missing something with the volumn commands
Same happen when I use Intellji Idea docker plugin Bind mounts in Configuration
It should be like this:
volumes:
- /src/main/webapp/sampleFiles:/usr/local/tomcat/webapps/portal/sampleFiles
as far as i know rw is for cases when you use drivers stuff...
and make sure that /src/main/webapp/sampleFiles is the host folder which have what you need in docker container. Because essentially it will be mapped into docker container. and will replace target folder.
this way siblings for /usr/local/tomcat/webapps/portal/sampleFiles should stay intact. If no, try starting without volumes part and verify that you see siblings.
don't forget to do docker-compose down and docker-compose up -d when you change anything in docker-compose.yaml file

Share dir between host and multiple containers using docker-compose

I have 2 containers in a compose files,that i want to serve app static files through nginx.
I have read this: https://stackoverflow.com/a/43560093/7522096 and want to use host dir to share between app container and nginx container, for some reason I dont want to use named volume.
===
Using a host directory Alternately you can use a directory on the host
and mount that into the containers. This has the advantage of you
being able to work directly on the files using your tools outside of
Docker (such as your GUI text editor and other tools).
It's the same, except you don't define a volume in Docker, instead
mounting the external directory.
version: '3'
services:
nginx:
volumes:
- ./assets:/var/lib/assets
asset:
volumes:
- ./assets:/var/lib/assets
===
My docker-compose file:
version: "3.7"
services:
app:
container_name: app
restart: always
ports:
- 8888:8888
env_file:
- ./env/app.env
image: registry.gitlab.com/app/development
volumes:
- ./public/app/:/usr/app/static/
- app-log:/root/.pm2
nginx:
container_name: nginx
image: 'nginx:1.16-alpine'
restart: always
ports:
- 80:80
- 443:443
volumes:
- /home/devops/config/:/etc/nginx/conf.d/:ro
- /home/devops/ssl:/etc/nginx/ssl:ro
- ./public/app/:/etc/nginx/public/app/
depends_on:
- app
volumes:
# app-public:
app-log:
Yet when i do this in my compose, the dir always come up empty on nginx, and the static files in my app container got disappear too.
Please help, I tried a lot of ways but can not figure it out.
Thanks.
During the initialization of the container docker will bind the ./public/app directory on the host with the /usr/app/static/ directory in the container.
If the ./public/app does not exist it will be created. The bind is from the host to the container, meaning that the content of ./public/app folder is
reflected (copied) into the container and not viceversa. That's why after the initialization the static app directory is empty.
If my understanding is correct your goal is to share the application files between the app container and nginx.
Taken into consideration the above the only solution is to create the files in the volume after the volume is created. Here is an example for the relevant parts:
version: "3"
services:
app:
image: ubuntu
volumes:
- ./public/app/:/usr/app/static_copy/
entrypoint: /bin/bash
command: >
-c "mkdir /usr/app/static;
touch /usr/app/static/shared_file;
mv /usr/app/static/* /usr/app/static_copy;
rm -r /usr/app/static;
ln -sfT /usr/app/static_copy/ /usr/app/static;
exec sleep infinity"
nginx:
image: 'nginx:1.16-alpine'
volumes:
- ./public/app/:/etc/nginx/public/app/
depends_on:
- app
This will move the static files to the static_copy directory and link back those files to /usr/app/static. Those files will be shared with the host (public/app director)
and nginx container (/etc/nginx/public/app/). Adapt it to fit your needs.
In alternative you can of course use named volumes.
Hope it helps

Docker compose problem with volume container attachment

I have a docker image "doc_image" and a docker volume "doc_volume". I want to spin up a container from the image where the volume is mounted into a specific point
If I do this with docker run like this:
docker run -d -p 5000:5000 -v doc_volume:/directory doc_image
then it runs flawlessly (I can see the expected files in /directory in interactive way). However, when I try to spin it up with docker-compose like with a docker-compose.yml like this:
version '3'
services:
my_service:
image: doc_image
volumes:
- doc_volume:/directory
volumes:
doc_volume:
there is nothing in /directory:
FileNotFoundError: [Errno 2] No such file or directory: '/directory/file.txt'
What went wrong here?
Add external property to volumes section:
version '3'
services:
my_service:
image: doc_image
volumes:
- doc_volume:/directory
volumes:
doc_volume:
external: true # << here we go
Your problem is that docker-compose creates another volume unless you explicitly tell him to use external one. External means creates not by means of docker-compose.

How do I read/write host files from Docker container on Linux

I'm using https://github.com/sagemathinc/cocalc-docker on Linux. I want to be able to edit my host files from within Cocalc. How do I make a folder that is a symbolic link to my host user's home?
If I understood you correctly you are looking for the -v flag when starting a new container.
So something like: docker run -v path:path -t myimage
You can mount your local dir on to your container using docker Volumes. Here's the doc on that https://docs.docker.com/engine/admin/volumes/volumes/#start-a-container-with-a-volume. Here's an example docker-compose.yml file:
version: "3.1"
services:
php-fpm:
build: docker/php-fpm
container_name: vendorapps-php-fpm
working_dir: /application
volumes:
- /localdirOnHost:/DestpathOnContainer

Resources