beginners/dockerfile/lab4_dockerfile_copy.md
The COPY instruction copies files or directories from source and adds them to the filesystem of the container at destination.
Two form of COPY instruction
COPY [--chown=<user>:<group>] <src>... <dest>
COPY [--chown=<user>:<group>] ["<src>",... "<dest>"] (this form is required for paths containing whitespace)
Dockerfile
FROM nginx:alpine
LABEL maintainer="Collabnix"
COPY index.html /usr/share/nginx/html/
ENTRYPOINT ["nginx", "-g", "daemon off;"]
Lets create the <b>index.html</b> file
$ echo "Welcome to Dockerlabs !" > index.html
$ docker image build -t cpy:v1 .
$ docker container run -d --rm --name myapp1 -p 80:80 cpy:v1
$ curl localhost
Welcome to Dockerlabs !
Dockerfile
FROM alpine AS stage1
LABEL maintainer="Collabnix"
RUN echo "Welcome to Docker Labs!" > /opt/index.html
FROM nginx:alpine
LABEL maintainer="Collabnix"
COPY --from=stage1 /opt/index.html /usr/share/nginx/html/
ENTRYPOINT ["nginx", "-g", "daemon off;"]
$ docker image build -t cpy:v2 .
$ docker container run -d --rm --name myapp2 -p 8080:80 cpy:v2
$ curl localhost:8080
Welcome to Docker Labs !
<b>NOTE:</b> You can name your stages, by adding an AS <NAME> to the FROM instruction.By default, the stages are not named, and you can refer to them by their integer number, starting with 0 for the first FROM instruction.You are not limited to copying from stages you created earlier in your Dockerfile, you can use the COPY --from instruction to copy from a separate image, either using the local image name, a tag available locally or on a Docker registry.
COPY --from=nginx:latest /etc/nginx/nginx.conf /nginx.conf
Next » Lab #4: CMD instruction