How to build a Dockerfile?
Published on Aug. 22, 2023, 12:19 p.m.
To build a Docker image from a Dockerfile, you can use the docker build
command. Here are the general steps to build a Dockerfile:
- Create a Dockerfile in your project directory. The Dockerfile contains the instructions for building the image.
- Open a terminal or command prompt and navigate to your project directory.
- Run the
docker build
command, specifying the tag name for the image and the location of the Dockerfile. For example:
docker build -t myimage:1.0 .
The .
at the end specifies that the build context is the current directory.
4. Wait for the image to build. The output will show the progress of each step in the build process.
5. Once the build is complete, you can run the image using the docker run
command, specifying the name of the image.
Here is an example Dockerfile that uses Node.js 16 as the base image and copies the app files into the image:
# Use Node.js v16 as the base image
FROM node:16
# Set the working directory
WORKDIR /app
# Copy package.json and package-lock.json
COPY package*.json ./
# Install dependencies
RUN npm install
# Copy app files
COPY . .
# Start the app
CMD ["npm", "start"]
After creating the Dockerfile, you can build the image using the command above.