How to copy HTML onto index.html in docker - 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

Related

Docker Apache Mounts and Permissions

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.

403 with nginx for file located in binded volume with docker

I am trying to use my nginx server on docker but I cannot use the files / folder if they belong to my volume. Problem, the goal of my test is to keep a volume between the file in my computer and the container.
I have searched during 3 days and tried a lot of solution but no effects...( useradd, chmod, chown, www_data, etc.....)
I don't understand how is it possible to use ngnix, a volume and docker?
The only solution actually for me is to copy the folder of my volume in another folder, and so I can chown the folder and use NGIX. There is no official solution on the web and I am surprised because for me using docker with a volume binded with his container would be the basic for a daily work.
If someone has managed to implement it, I would be very happy if you could share you code. I need to understand what I am missing.
FYI I am working with a VM.
Thanks !
I think you are not passing the right path in the volume option. There are a few ways to do it, you can pass the full path or you can use the $(pwd) if you are using a Linux machine. Let's say you are on /home/my-user/code/nginx/ and your HTML files are on html folder.
You can use:
$ docker run --name my-nginx -v /home/my-user/code/nginx/html/:/usr/share/nginx/html:ro -p 8080:80 -d nginx
or
$ docker run --name my-nginx -v ~/code/nginx/html/:/usr/share/nginx/html:ro -p 8080:80 -d nginx
or
$ docker run --name my-nginx -v $(pwd)/html/:/usr/share/nginx/html:ro -p 8080:80 -d nginx
I've created an index.html file inside the html folder, after the docker run, I was able to open it:
$ echo "hello world" >> html/index.html
$ docker run --name my-nginx -v $(pwd)/html/:/usr/share/nginx/html:ro -p 8080:80 -d nginx
$ curl localhost:8080
hello world
You can also create a Dockerfile, but you would need to use COPY command. I'll give a simple example that's working, but you should improve this by using a version and etc..
Dockerfile:
FROM nginx
COPY ./html /usr/share/nginx/html
...
$ docker build -t my-nginx:0.0.1 .
$ docker run -d -p 8080:80 my-nginx:0.0.1
$ curl localhost:8080
hello world
You can also use docker-compose. By the way, those examples are just to give you some idea of how it works.

Copy with Dockerfile

I try to run the Angular app inside docker with Nginx:
$ ls
dist Dockerfile
Dockerfile:
FROM nginx
COPY ./dist/statistic-ui /usr/share/nginx/html/
Inside dist/statistic-ui/ all app files.
But COPY command doesn't work, Nginx just starts with default welcome page and when I check files inside /usr/share/nginx/html/ only default Nginx files.
Why COPY command doesn't work and how to fix it?
UPDATE
Run docker container
sudo docker run -d --name ui -p 8082:80 nginx
You need to build an image from your Dockerfile then run a container from that image:
docker build -t angularapp .
docker run -d --name ui -p 8082:80 angularapp
Make sure you include the trailing dot at the end of the docker build command.

why do i keep seeing nginx index.html on localhost when i run my docker image

I installed and run nginx on my linux machine to understand the configurations etc. After a while i decided to remove it safely by following this thread in order to use it in docker
By following this documentaion i run this command
sudo docker run --name ngix -d -p 8080:80 pillalexakis/myrestapi:01
And i saw ngix's homepage at localhost
Then i deleted all ngix images & stopped all containers and i also run this command
sudo docker system prune -a
But now restarted my service by this command
sudo docker run -p 192.168.2.9:7777:8085 phillalexakis/myfirstapi:01 and i keep seeing at localhost ngix index.html
How can i totally remove it ?
Note: I'm new with docker and i might have missed a lot of things. Let me know what extra docker commands should i run in order provide better information.
Assuming your host have been preparing as below
your files (index.html, js, etc) under folder - /myhost/nginx/html
your nginx configuration - /myhost/nginx/nginx.conf
Solution
map your files (call volume) on the fly from outside docker image via docker cli
This is the command
docker run -it --rm -d -p 8080:80 --name web \
-v /myhost/nginx/html:/usr/share/nginx/html \
-v /myhost/nginx/nginx.conf:/etc/nginx/nginx.conf \
nginx
copy your files into docker image by build your own docker image via Dockerfile
This is your Dockerfile under /myhost/nginx
FROM nginx:latest
COPY ./html/index.html /usr/share/nginx/html/index.html
This is the command to build your docker image
cd /myhost/nginx
docker build -t pillalexakis/nginx .
This is the command to run your docker image
docker run -it --rm -d -p 8080:80 --name web \
pillalexakis/nginx

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

Resources