serhii.net

In the middle of the desert you can say anything you want

20 Jun 2022

Docker adventures

Since Docker is again part of my life, I’ll add things here as I google them.

Building

Contexts

  • docker build ./somedirectory has that dir as build context.
  • docker build -f ./somedirectory/Dockerfile has the current directory as build context, and all siblings of somedirectory are part of the context too.

Relevant for COPY that can work only on files in the current build context: Dockerfile reference | Docker Documentation

.dockerignore

If the context is big it takes time. In my case I had a lot of stray virtualenvs that made it really big.

.dockerignore helps:

Has to be in the root directory of the context.

Samples:

And things like .venv or ./venv are only relative to context root! **/.venv

Listing context after .dockerignore

Did that, context was still big. dockerfile - Docker command/option to display or list the build context - Stack Overflow told me that my favourite ncdu parses them nicely!

ncdu -X .dockerignore

Not the same but exactly what I wanted. Then I got the list of all weird environments I created by adding the missing ones, leading to this:

# Environments
**/.env
**/.venv
**/env
**/venv
**/ENV
**/venv_jupyter
**/build_venv
**/venv_torch
**/.install_venv

Docker build

  • docker build . -t imagename:optionaltag so you don’t have to copy the ID every time.

Then you can just cycle between these two commands when developing:

docker build -t name .
docker run --rm -it -p 8888:8888 -v /home/sh/hsa/Data:/docker_vol name:latest

Things get nicely cached - which means installing tensorflow ideally would be above the lines in the Dockerfile that get changed often as part of the process above.

Dockerfile commands

COPY and slashes

From the official docu:

  • If <dest> has a slash at the end it’s considered a directory.
  • If it doesn’t - it’s a regular file

Matters when copying multiple things, or if it doesn’t exist.

WORKDIR

Tried

RUN cd whatever
RUN python3 -m pip install -r requirements.txt

Didn’t work. I needed WORKDIR.

It works like cd, if called sequentially each path is relative to the previous one.

Nel mezzo del deserto posso dire tutto quello che voglio.
comments powered by Disqus