Docker Apache Mounts and Permissions - docker

I've just started dabbling with Docker. I notice that when directly pulling the PHP-Apache image and mounting a volume, this works fine:
docker run -d -p 80:80 --name php-try -v C:\Users\me\Desktop\php-try:/var/www/html php:7.2-apache
But when I build an image with a Dockerfile, copying the contents of the working directory to the usual /var/www/html folder, and then try to run a container with a volume or bind mount docker run -d -p 80:80 php-try -v C:\Users\me\Desktop\php-try:/var/www/html the container simply exits/doesn't run. Without the attempt to mount a volume, it runs fine.
The Dockerfile is simply:
FROM php:7.2-apache
COPY . /var/www/html/
Grateful for any help with this.

Related

How to copy HTML onto index.html in docker

Here is my docker file I was wondering how can I copy the local html code which is in the same directory as my dockerfile. This command somehow did not work as when I ran a curl on my docker IP
it was not my html code it was the default code.
FROM httpd
EXPOSE 80
COPY public-html.html /usr/local/apache2/htdocs/
The problem is that you built the image, but did not run it.
Example to run it by mounting the file as a volume into the container:
docker run -it --rm -d -p 8080:80 --name web -v ./public-html.html:/usr/share/nginx/html nginx
Now you can do a curl http://localhost:8080.
Example for building it:
FROM nginx:latest
COPY ./public-html.html /usr/share/nginx/html/index.html
More information here

Docker file in host machine not available in container using bind volume

I am facing an issue where after runnig the container and using bind mount to mount the directory on host to container I am not able to see new files created in host machine inside container.Below is my project structure.
The python code creates a file inside the container which should be available inside the host machine too however this does happen when I start the container with below command. However updates to python code and html is available inside the container.
sudo docker container run -p 5000:5000 --name flaskapp --volume feedback1:/app/feedback/ --volume /home/deepak/PycharmProjects/NewDockerProject/sampleapp:/app flask_image
However after starting the container using below command, everything seems to work fine. I can see all the files from container to host and vice versa(new created , edited).I git this command from docker in the month of lunches book.
sudo docker container run --mount type=bind,source=/home/deepak/PycharmProjects/NewDockerProject/sampleapp,target=/app -p 5000:5000 --name flaskapp
Below is the content of my dockerfile
FROM python:3.8-alpine
WORKDIR /app
COPY ./requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["python","main.py"]
Could someone please help me in figuring out the difference between the two commands ? I am using ubuntu. Thank you
In my case i got working volumes using following docker run args (but i am running without --mount type=bind):
docker run -it ... -v mysql_data:/var/lib/mysql -v storage:/usr/shared/app_storage
where:
mysql_data is a volume name
/var/lib/mysql path inside container machine
you could list volumes as:
docker volume ls
and inspect them to see where it points on your system (usually /var/lib/docker/volumes/{volume_nanme}/_data):
docker volume inspect mysql_data
to create volume use following command:
docker volume create {volume_name}

Copy files from host to container when container starts on any host

I want to copy files from host to Docker container when I run the container on any host.
here is my Dockerfile
FROM tomcat:9
EXPOSE 8080
ADD ./target/app.war /tmp/myapp.war
RUN unzip /tmp/myapp.war -d /usr/local/tomcat/webapps/myapp
ENTRYPOINT ["cp", "-r", "/data/*", "/usr/local/tomcat/webapps/myapp/data"]
After building the docker image
docker build -t myappimage .
I am running it with:
docker run --mount type=bind,source=d:/data,destination=/data --rm -it -p 8081:8080 myappimage
but this throws error cp: cannot stat '/data/*': No such file or directory
I am not sure why mounting is not working, it should copy all files from my host directory d:/data to Docker container directory /data when a container starts.
This command in ENTRYPOINT is run in Docker container.
You can try:
FROM tomcat:9
EXPOSE 8080
ADD ./target/app.war /tmp/myapp.war
RUN unzip /tmp/myapp.war -d /usr/local/tomcat/webapps/myapp
COPY /data /usr/local/tomcat/webapps/myapp/data/
I hope /usr/local/tomcat/webapps/myapp/data directory exist in image prior to copying. The command seems to working fine on my machine (Mac). Not sure if it's the d:/ that is causing the issue.
Also you can try using the -v option with a z flag (It solved the same issue for me), assuming you are inside d: directory
docker run -v "$(pwd)"/data:/data:z --rm -it -p 8081:8080 myappimage
With -v it will create an endpoint for you. You can read here

Mount a local data directory into jupyterhub

I build and run jupyterhub a docker image. https://hub.docker.com/r/joergklein/jupyterhub
Is it a good idea to mount in the Dockerfile
# Create a mountpoint
VOLUME /data
or is it better to mount to
# Create a mountpoint
VOLUME /home/data
I have a local data dir on my computer. I will mount the data dir into the container /data or /home/data.
At first I download and install the image
docker run -p 8000:8000 -d --name jupyterhub joergklein/jupyterhub jupyterhub
Second I will mount the datasets dir. into the /data in the container. In the dataset dir are a lot of csv files.
docker run -v /home/user/datasets:/data -t jupyterhub /bin/bash
I want run the JupyterHub in a sub domain in a team.
We want share the data. How can all team member work in this directory?
How we can add new data in this directory?
Which is the right docker run commnd?
That works fine for me.
docker run -p 8000:8000 -d --name jupyterhub --volume $(pwd)/datasets:/data joergklein/jupyterhub jupyterhub

How to copy files from Docker container to bind mount directory on docker run?

In my Dockerfile, I copy a config file over like so:
VOLUME ["/root/.litecoin"]
WORKDIR $HOME/.litecoin
COPY litecoin.conf .
I'm able to start the docker container with the config file if I use a named or unanamed volume:
docker run -d --name litecoind -v litecoin-blockchain:/root/.litecoin -t test
or
docker run -d --name litecoind -v /root/.litecoin -t test
However, if I try with a bind mount (empty directory), it overwrites the contents of /root/.litecoin and I lose my config. E.g:
docker run --name litecoind -v /mnt/LV_LTC:/root/.litecoin -t test
What is the best non-hacky way to get config files copied from the host to the container using bind mounts? It's annoying that my self-contained Docker app is breaking because I need to use bind mounts (block storage).

Resources