build context for docker image very large

2019-01-22 10:56发布

问题:

I have created a couple different directories on my host machine as I try to learn about Docker just to keep my dockerfiles organized. My Dockerfile I just ran looks like this:

FROM crystal/centos
MAINTAINER crystal

ADD ./rpms/test.rpm ./rpms/ 
RUN yum -y --nogpgcheck localinstall /rpms/test.rpm 

My actual rpm is only 1 GB. But when I try to do sudo docker build -t="crystal/test" ., I get sending build context to Docker daemon 3.5 GB. Is there something else that I'm unaware of as you continue to build Docker images? Is my memory accumulating as I build more images in my other directories on my host machine?

回答1:

The Docker client sends the entire "build context" to the Docker daemon. That build context (by default) is the entire directory the Dockerfile is in (so, the entire rpms tree).

You can setup a .dockerignore file to get Docker to ignore some files. You might want to experiment with it.

Alternatively, you can move your rpms folder one directory level above your Dockerfile, and only symlink test.rpm into the Dockerfile's directory.



回答2:

I fixed it by moving my Dockerfile and docker-compose.yml into a subfolder and it worked great. Apparently docker sends the current folder to the daemon and my folder was 9 gigs.



回答3:

In my case that was when i execute with wrong -f arguments - without path to directory where located Dockerfile

docker build --no-cache -t nginx5 -f /home/DF/Dockerfile /home/DF/ - right

docker build --no-cache -t nginx5 -f /home/DF/Dockerfile - wrong



回答4:

If you have a .dockerignore file and build context is still large, you can check what is being sent to the docker build context using The Silver Searcher:

ag --path-to-ignore .dockerignore --files-with-matches

Note that some ** patterns might not work properly.

See this Github issue for additional comments: https://github.com/moby/moby/issues/16056



回答5:

I had the same issue as FreeStyler. However I was building from a directory one up from my context. So the -f arguments were correct the context was incorrect.

project 
|
-------docker-dir 

Building from the docker-dir the following was fine

docker build -t br_base:0.1 . 

Building from the dock-dir the the build context changed. Therefore I needed to change the context in the command. Context is given by the '.' in the command above.

The the new command from the project directory should be

docker build -t br_base:0.1 ./base

Context here is given by the './base'



标签: docker