How to build docker images with Dockerfile
Updated:
A Dockerfile is a script containing a set of instructions to automate the building of Docker images. It defines everything required to set up a containerized environment.
Basic Structure of a Dockerfile
A Dockerfile typically includes:
Command | Description |
---|---|
FROM | Specifies the base image (e.g., ubuntu, nginx, node) |
LABEL | Metadata like maintainer information |
RUN | Runs commands during image build (e.g., install packages) |
WORKDIR | Copies files from the host to the container |
COPY | Specifies the base image (e.g., ubuntu, nginx, node) |
ADD | Like COPY, but also supports URLs and tar extraction |
ENV | Sets environment variables inside the container |
EXPOSE | Defines which ports the container will listen on |
CMD | Default command executed when the container starts |
ENTRYPOINT | Similar to CMD, but more flexible for argument handling |
Example Dockerfile
Building and Running a Dockerfile
After creating your Dockerfile, build an image and run it:
- Build the Image
(-t
assigns a tag to the image, .
refers to the current directory)
- Run a Container from the Image