LoginSignup
0
0

Directory not found in `docker image build`

Last updated at Posted at 2024-05-01

Summary

1. Cause

In Docker, it is not possible to reference sibling directories.

2. Solution

# Instead of the current directory `.`,
docker image build .

# specify the parent directory `..` for building the docker image
docker image build ..

Details

1. What I want to do

$ # Display up to the second level starting from the parent directory .. using tree
$ tree -L 2 ..
.
├── backend <-- Start from here
│   └── Dockerfile
├── frontend
│   └── Dockerfile
└── storage <-- Want to reference this
    └── scripts
$
$ # Assume the current directory is the backend directory
$ pwd
/home/user/project/backend
$

2. Before Improvement

backend/Dockerfile
# Incorrect
#   This causes an error
#   Docker cannot see sibling directories
COPY ../storage .
$ docker image build -t backend .
It results in an error...
=> ERROR [7/8] COPY ../storage .                                                                                             0.0s
------
 > [7/8] COPY ../storage .:
------
dockerfile:13
--------------------
  11 |     
  12 |     WORKDIR /storage
  13 | >>> COPY ../storage .
  14 |     
  15 |     WORKDIR /app
--------------------
ERROR: failed to solve: failed to compute cache key: failed to calculate checksum of ref b2b8bec0-7f16-4a4e-82ce-50cc6ea8ad78::olt6lm81xjn7ke9u319v7irsg: "/storage": not found
yaju@yaju-house:project$ docker image build -t backend -f backend/dockerfile .
[+] Building 4.2s (13/13) FINISHED         

3. After Improvement

backend/Dockerfile
# Incorrect
# COPY ../storage .

# Correct
#   The relative path to 'storage' should start from the parent directory
COPY storage .
$ # Incorrect
$ # docker image build -t backend .
$
$ # Correct
$ #   Since the build context sets the path for the Docker file,
$ #   use the -f option to specify the path to the Docker file explicitly.
$ #   The relative path in the -f option starts from the current directory,
$ #   not the build context.
$ docker image build -t backend -f ./Dockerfile ..  

Background

1. Question

Why can't sibling directories be referenced? It seems that including unnecessary files during docker image build makes the process heavier. I found a Japanese translation site, but I couldn't find any mention on the original English official site...

If you accidentally include files that are not needed for the image build, the build context becomes larger, and as a result, a larger image is created. This will increase the time it takes to build the image, and the time to push or pull this image, and it will also increase the capacity needed at runtime of the container. You can see how big the build context has become by checking a message like the following when building with a Dockerfile.

Sending build context to Docker daemon  187.8MB

Dockerfile Best Practices | Docker Documentation

2. Terminology

Build Context

The collection of files used when building a docker image.

The build context is the set of files that your build can access.
Build context | Docker Docs

The positional argument in docker image build, such as . or .. in this article, specifies the collection of files, i.e., the build context.

In the docker image build command, the "positional argument" specifies the path where the collection of files that make up the build context is located, and the "-f option" specifies the path to the Docker file.

3. Measures

For those who want to specify the build context directory individually rather than roughly specifying the parent directory, please ask ChatGPT.

Can the docker build image command include multiple build contexts?

References

This helped me solve it. Thank you.

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0