0

How do I copy an index.html file into nginx?

root@mordor:/home/nicholas/docker/nginx# 
root@mordor:/home/nicholas/docker/nginx# ll
total 12
drwxrwxr-x 2 nicholas nicholas 4096 Nov 13 16:18 ./
drwxrwxr-x 3 nicholas nicholas 4096 Nov 13 16:12 ../
-rw-rw-r-- 1 nicholas nicholas   60 Nov 13 16:18 dockerfile
root@mordor:/home/nicholas/docker/nginx# 
root@mordor:/home/nicholas/docker/nginx# cat dockerfile 
FROM nginx
COPY /home/nicholas/html /usr/share/nginx/html


root@mordor:/home/nicholas/docker/nginx# 
root@mordor:/home/nicholas/docker/nginx# docker build -t some-content-nginx .
Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM nginx
 ---> c39a868aad02
Step 2/2 : COPY /home/nicholas/html /usr/share/nginx/html
COPY failed: stat /var/snap/docker/common/var-lib-docker/tmp/docker-builder726461098/home/nicholas/html: no such file or directory
root@mordor:/home/nicholas/docker/nginx# 
root@mordor:/home/nicholas/docker/nginx# ls /home/nicholas/html/
index.html
root@mordor:/home/nicholas/docker/nginx# 
root@mordor:/home/nicholas/docker/nginx# cat /home/nicholas/html/index.html 



index
hello world



nicholas@mordor:~/html$ 

I'm unclear on the source and destination, which might be complicated by docker having been installed from snap.


Looks like ansible has slightly clearer, at least for me, syntax:

  tasks:
  - name: "Copying files"
    copy:
      src: files/{{ item }}
      dest: /my/folder/
    with_items:
      - file.txt

or perhaps I'm doing something incorrectly.

2 Answers 2

1

The docker build command builds Docker images from a Dockerfile and a “context”. A build’s context is the set of files located in the specified PATH or URL. The build process can refer to any of the files in the context. For example, your build can use a COPY instruction to reference a file in the context.

From docker build documentation.

As you run the docker build with the . path for the context, it will try to find the folder home/nicholas/html from the context root@mordor:/home/nicholas/docker/nginx

The COPY command documentation shows some examples

2
  • yet the copy command wasn't, and isn't, acting as expected. Nov 14, 2020 at 7:34
  • 1
    If you push minimal files into a repository, I can drive deep a little bit ;-) Nov 14, 2020 at 8:45
0

This at least builds and runs:

root@mordor:/home/nicholas/docker/nginx# 
root@mordor:/home/nicholas/docker/nginx# ls -al
total 16
drwxrwxr-x 2 nicholas nicholas 4096 Nov 13 17:18 .
drwxrwxr-x 3 nicholas nicholas 4096 Nov 13 16:12 ..
-rw-rw-r-- 1 nicholas nicholas   49 Nov 13 17:18 dockerfile
-rw-rw-r-- 1 nicholas nicholas   56 Nov 13 17:18 index.html
root@mordor:/home/nicholas/docker/nginx# 
root@mordor:/home/nicholas/docker/nginx# cat dockerfile 
FROM nginx
COPY index.html /usr/share/nginx/html
root@mordor:/home/nicholas/docker/nginx# 
root@mordor:/home/nicholas/docker/nginx# cat index.html 



hello world

how do I put this file in a diff dir?


root@mordor:/home/nicholas/docker/nginx# 
root@mordor:/home/nicholas/docker/nginx# docker build -t some-content-nginx .
Sending build context to Docker daemon  3.072kB
Step 1/2 : FROM nginx
 ---> c39a868aad02
Step 2/2 : COPY index.html /usr/share/nginx/html
 ---> Using cache
 ---> 07a94375fb55
Successfully built 07a94375fb55
Successfully tagged some-content-nginx:latest
root@mordor:/home/nicholas/docker/nginx# 
root@mordor:/home/nicholas/docker/nginx# docker run --name some-nginx -d -p 8080:80 some-content-nginx
2c912aa5ce5c8b864a6fe79a18ee8e833874f85a75fed1c7020163ca7b90bedb
root@mordor:/home/nicholas/docker/nginx# 

although I can't seem to get anything from navigating to localhost so I'll have to look closer at the container which is built.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.