65

I have a Dockerfile and there is a syntax like this COPY ["Gemfile", "Gemfile.lock", "Procfile", ".env", "/huginn/"]

I use RUN /bin/bash -l -c "ls -a" to check file cope status, I find .env file doen't be copied to the image.

I change the .env file name to test.env and use COPY ["Gemfile", "Gemfile.lock", "Procfile", "test.env", "/huginn/"], then it work, test.env is copied to the image.

Anyone know why is? And any solution can let docker support COPY .env file name?

Hsiu Chuan Tsao
  • 1,396
  • 1
  • 12
  • 23
  • A `.env` file should be copied by default, is the ls running in the same dir? `ls -a /huginn`? – Matt Feb 09 '17 at 13:28
  • The path must be inside the context of the build; you cannot COPY ../something /something, because the first step of a docker build is to send the context directory (and subdirectories) to the docker daemon. – Natan Rubinstein Jun 26 '19 at 14:36

6 Answers6

61

If you have .dockerignore file then it might be you added to ignore hidden files like .git, .vagrant etc.

If .dockerfile ignoring hidden files then either you can enable to not ignore or change file name.

For more info about .dockerignore file

pl_rock
  • 14,054
  • 3
  • 30
  • 33
23

There is a statement in .dockerignore file documentation:

Note: For historical reasons, the pattern . is ignored.

enter image description here

eQ19
  • 9,880
  • 3
  • 65
  • 77
  • 1
    The author is asking about a specific file: `.env`. I would expect that the note you mentioned is about `.` only, and not all files or folders that start with it (e.g. `.*`). Additionally, the documentation states: `A preprocessing step removes leading and trailing whitespace and eliminates . and .. elements using Go’s filepath.Clean. Lines that are blank after preprocessing are ignored.` – psq Nov 15 '22 at 17:14
2

share my dockerfile, it can work correctly now

FROM ruby:2.3

MAINTAINER Tomato <tsaohucn@gmail.com>

ENV DEBIAN_FRONTEND noninteractive
ENV LANG C.UTF-8
ENV RAILS_VERSION 5.0.1

# install rails && bundler
RUN gem install rails --version "$RAILS_VERSION"

WORKDIR /huginn

# copy huginn config file
COPY ["Gemfile", "Gemfile.lock", "Procfile", ".env", "/huginn/"]
COPY lib/gemfile_helper.rb /huginn/lib/
COPY vendor/gems /huginn/vendor/gems

# run bundle install
RUN bundle install

# copy huginn
COPY . /huginn/
RUN ls -a

And there is a .dockerignore:

.git
tmp
log
doc
spec
media
.openshift
.bundle
vendor/bundle
db/*.sqlite3
public/system/*
coverage
.travis.yml
build_docker_image.sh
# Copied from .gitignore
*.rbc
*.sassc
.sass-cache
capybara-*.html
.rspec
!/tmp/.gitkeep
**.orig
rerun.txt
pickle-email-*.html
.idea/
.DS_Store
deployment/tmp
deployment/cookbooks
.vagrant
.*un~
.ruby-gemset
.ruby-version
manifest.yml
config/unicorn.rb
db/schema.rb
Hsiu Chuan Tsao
  • 1,396
  • 1
  • 12
  • 23
1

I faced the same issue, here is an easy solution===>

Copy hidden files with exact directory path Like:-

Example:- COPY path/.hidenfile destination_path

Kumar Pankaj Dubey
  • 1,541
  • 3
  • 17
  • 17
0

In my case, I had to explicitly copy the file to the image

Just adding this line in the Dockerfile solved my problem:

COPY .env /app/

Jonas Braga
  • 379
  • 4
  • 5
-3

Your COPY syntax is wrong, this syntax is valid for ENTRYPOINT or CMD in a Dockerfile.

See the doc for COPY

https://docs.docker.com/engine/reference/builder/#/copy

user2915097
  • 30,758
  • 6
  • 57
  • 59
  • 2
    It's valid syntax for COPY, the second form in the docco – Matt Feb 09 '17 at 13:22
  • https://docs.docker.com/engine/reference/builder/#copy : COPY COPY has two forms: COPY [--chown=:] ... COPY [--chown=:] ["",... ""] – Frans Aug 18 '20 at 12:48