Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use COPY command in Docker build?

I am trying to perform some automation to while build my docker image. Below is my code run in Windows 8, Kitematic, Virtual Box:

FROM node:6

# Create directory
RUN mkdir -p /tempDir && mkdir -p /tempDir/built && mkdir -p /data

# Setup build environment
COPY . /tempDir
RUN npm install -g gulp typings 

# Build from source
WORKDIR /tempDir 
RUN npm install && typings install && gulp build 

Up to here, everything is fine, success to build my typescript to javascript in /tempDir/built directory. I bash into my container, it look like this:

tempDir/gulpfile.js
tempDir/typings
tempDir/src 
tempDir/built

My next step is to move this built folder to another directory then delete tempDir. My problem is that COPY command not work as I expected.

COPY built/* /data/

I keep on getting error like 'no such file or directory' or 'lstat built/: no such file or directory'. I tried ./built, ./built/, built/, /tempDir/built/, and other still getting the same error. Anyone can help on this?

like image 468
Simon Avatar asked Sep 14 '16 01:09

Simon


People also ask

How does Copy command work in Dockerfile?

COPY and ADD are both Dockerfile instructions that serve a similar purpose. They let you copy files from a specific location into a Docker image. COPY takes in a source and destination. It only lets you copy in a local or directory from your host (the machine-building the Docker image) into the Docker image itself.

How does copy in docker work?

The docker cp utility copies the contents of SRC_PATH to the DEST_PATH . You can copy from the container's file system to the local machine or the reverse, from the local filesystem to the container. If - is specified for either the SRC_PATH or DEST_PATH , you can also stream a tar archive from STDIN or to STDOUT .

How do I copy a docker image?

In order to transfer a Docker image from one server to another, what you need to do is first export the image to a file, then copy that file over from your current server to the new one using scp or rsync and finally load the image to your new server.

How do I copy a file between docker containers?

You can use the docker cp command to copy the file. The first path (Source) is the path in the Docker Container and the second one is the path inside your Local System (Destination).


2 Answers

COPY copies files from your host filesystem into the container. It looks like you want to copy files from one directory in the container to another. For that you need to use RUN and cp

RUN cp -r built/* /data/

Since you will be removing the /tempDir/ directory, you can speed things up a bit by renaming the directory:

RUN mv built /data

This way you don't have to copy data around and then delete the originals.

like image 69
drs Avatar answered Oct 22 '22 20:10

drs


you are trying to copy something that's in your container and so COPY will not work because it is specific to your Host --> Container.

Instead you will have to run a bash command inside your container.

RUN cp -rf built /data/

like image 27
Mike Tung Avatar answered Oct 22 '22 20:10

Mike Tung