Start learning free

LESSON 153 OF 188 · BACKEND: SHIPPING IT

.dockerignore in Python

Practice

In your Dockerfile, COPY . . copies the whole shop folder into the image. But the folder holds more than the shop: the secrets in .env, a test database, Python's caches, the git history. Anyone who gets an image can read every file in it.

A file named .dockerignore, next to the Dockerfile, lists what docker build must leave out, one pattern per line. A name leaves out that file, or that folder and everything in it. A * stands for any characters. For example, a project that writes log files and keeps a scratch folder could use:

*.log
scratch

Here the file's text is the Python string dockerignore: one pattern per line, each ending with \n. The check reads it, says what each line leaves out, then lists what COPY . . would copy into /app. When your file is done, it prints:

.dockerignore:
.env: leaves out .env
*.db: leaves out shop.db
__pycache__: leaves out __pycache__/app.cpython-313.pyc
.git: leaves out .git/config
COPY . . copies into /app:
app.py
requirements.txt
Dockerfile
.dockerignore
Nothing that should stay out is in the image, and nothing it needs is missing.

Your task: write the string dockerignore so that it leaves out the secrets file .env, every file ending in .db, the __pycache__ folder and the .git folder, and nothing the shop needs.


Assignment
Follow the Coding Tutorial to write the four lines of the shop's .dockerignore.


Hint
Each pattern is its own piece of the string, ending with \n. On the server, the shop gets its secrets as environment variables and its database at DATABASE_PATH, so the image needs neither.


STEP 1 OF 4
    Ctrl + Enter to check

    Ship Dockerignore in Python

    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free