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)
| Platform | Number of Instance | Reading Time |
|---|---|---|
| Play with Docker | 1 | 5 min |
- Create an account with DockerHub
- Open PWD Platform on your browser
- Click on Add New Instance on the left side of the screen to bring up Alpine OS instance on the right side
- Create an image with COPY instruction
- COPY instruction in Multi-stage Builds
Dockerfile
FROM nginx:alpine
LABEL maintainer="Collabnix"
COPY index.html /usr/share/nginx/html/
ENTRYPOINT ["nginx", "-g", "daemon off;"]
Lets create the index.html 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 !
NOTE: You can name your stages, by adding an AS 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