Start learning free

LESSON 153 OF 173 · BACKEND: SHIPPING IT

.dockerignore in JavaScript

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, the packages npm installed on your computer, 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 string dockerignore: an array with one string per line of the file, joined with \n, the line break. 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
node_modules: leaves out node_modules/express/index.js
.git: leaves out .git/config
COPY . . copies into /app:
app.js
package.json
package-lock.json
Dockerfile
.dockerignore
Nothing that should stay out is in the image, and nothing it needs is missing.

Your task: fill the dockerignore array so that it leaves out the secrets file .env, every file ending in .db, the node_modules 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 string in the array, with a comma after it. On the server, the shop gets its secrets as environment variables, its database at DATABASE_PATH and its packages from npm ci, so the image needs none of them from your computer.


STEP 1 OF 4
    Ctrl + Enter to check

    Ship Dockerignore in JavaScript

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