Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker ONBUILD COPY doesn't seem to copy files

I am trying to copy files to a docker image when I execute the docker build command. I am not sure what I am doing wrong because this seems to work for the docker rails onbuild file but doesn't work for my custom dockerfile.

Here is my Dockerfile

FROM ubuntu:14.04

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

ONBUILD COPY Gemfile /usr/src/app

CMD ["tail", "-f", "/dev/null"]

The docker commands that I run are the following.

docker build -t copy-test .
docker run --name run-test -d copy-test
docker exec -i -t 2e9adebae0fc /bin/bash

When I connect to the container with docker exec it starts in /usr/src/app but the Gemfile is not there. I don't understand why the mkdir and WORKDIR instructions seem to work but the ONBUILD COPY does not. (And yes the directory I am invoking these commands in does have a Gemfile present)

like image 548
MikeV Avatar asked Jun 14 '16 14:06

MikeV


People also ask

How do I copy a file in Docker?

Obtain the name or id of the Docker container. Issue the docker cp command and reference the container name or id. The first parameter of the docker copy command is the path to the file inside the container. The second parameter of the docker copy command is the location to save the file on the host.

What does Onbuild instruction do in Dockerfile?

The ONBUILD instruction adds to the image a trigger instruction to be executed at a later time, when the image is used as the base for another build. The trigger will be executed in the context of the downstream build, as if it had been inserted immediately after the FROM instruction in the downstream Dockerfile.

Can you copy a file into a docker image?

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).

How does copy work in Docker?

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.


1 Answers

You have to use COPY to build your image. Use ONBUILD if your image is kind of template to build other images.

See Docker documentation:

The ONBUILD instruction adds to the image a trigger instruction to be executed at a later time, when the image is used as the base for another build. The trigger will be executed in the context of the downstream build, as if it had been inserted immediately after the FROM instruction in the downstream Dockerfile.

like image 70
molivier Avatar answered Sep 17 '22 20:09

molivier