04 Dockerfile - Jeremy-Feytens/IT-Landscape GitHub Wiki
Dockerfile
A Dockerfile is a simple text file that contains a list of instructions for building a docker image. It defines the environment, software, dependencies, and steps needed to set up and run an application inside a container.
Why Use a Dockerfile?
- Automates the image creation process
- Makes deployments consistent and reproducible
- Easy to share enviroments with others
- Makes it easy to share apps across systems
Example Dockerfile
This is a simple Dockerfile for a node.js application:
# 1. Use an official Node.js base image
FROM node:18-alpine
# 2. Set the working directory
WORKDIR /app
# 3. Copy package.json and install dependencies
COPY package*.json ./
RUN npm install
# 4. Copy the rest of the application code
COPY . .
# 5. Expose the port the app runs on
EXPOSE 3000
# 6. Define the command to run the app
CMD ["node", "index.js"]
Line | Description |
---|---|
FROM node:18-alpine |
Uses a lightweight Node.js base image. |
WORKDIR /app |
Sets /app as the working directory inside the container. |
COPY package*.json ./ |
Copies package files needed for installing dependencies. |
RUN npm install |
Installs project dependencies. |
COPY . . |
Copies the rest of your project files. |
EXPOSE 3000 |
Declares that the app runs on port 3000. |
CMD ["node", "index.js"] |
Tells Docker how to start your app. |
.dockerignore
Create a .dockerignore
file to exclude unnecessary files from the build context:
node_modules
.git
Dockerfile
docker-compose.yml
...
This speeds up builds and keeps your images clean.
Build & Run Commands
To build and run your Docker image:
docker build -t my-node-app .
docker run -p 3000:3000 my-node-app