mahmood
mahmood

Reputation: 24795

Copy a file from host OS to docker image

I have a docker file which contains a line like this

FROM ubuntu:14.04
RUN curl -Lso serf.zip https://URL/serf.zip \ 
&& unzip serf.zip -d /bin \
&& rm serf.zip

And that means it downloads a file and extract it within the linux image (here it is an Ubuntu-14.04).

Now, I don't want to use that URL. Instead I have the file on my local (host) machine which is a Ubuntu-12.04. The question is, how can I transfer the file from the host OS to the docker image? Then, I am able to change the Dockerfile to

FROM ubuntu:14.04
RUN unzip serf.zip -d /bin \
&& rm serf.zip

?

P.S: Currently, I have the following images

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
<none>              <none>              99ca37acd529        39 hours ago        261.2 MB
data-analytics      dataset             1db2326dc82b        2 days ago          1.742 GB
ubuntu              14.04               780bd4494e2f        4 weeks ago         187.9 MB

Upvotes: 3

Views: 1429

Answers (2)

Louis Kottmann
Louis Kottmann

Reputation: 16648

If you're ok having the file under the same directory (or subdirectories) of your Dockerfile, you can use COPY.

ADD will unzip it for you at the same time, removing the need for unzip and rm.

Now if you want to share the serf.zip file between projects, links won't do, you need to bind mount it on the host before running docker build:

sudo mount --bind /path/to/serf.zip_folder /path/to/Dockefile_folder

Upvotes: 1

kojiro
kojiro

Reputation: 77167

FROM whatever
COPY contextfile containerfile
RUN do_thing_to containerfile

in your case

FROM ubuntu:14.04
COPY serf.zip serf.zip
RUN unzip serf.zip -d /bin \
&& rm serf.zip
  1. The file must be in the build context. Conventionally, that means it's in the directory or a subdirectory below the directory containing the Dockerfile.
  2. You will either have to create a layer just container the newly copied file, or use ADD magic to automatically uncompress it. Both approaches have their drawbacks.

Upvotes: 7

Related Questions