Here’s a basic Dockerfile cheat sheet to help you create Docker images:
Dockerfile Basics
Set the Base Image:
FROM base_image:tag
Set the Working Directory:
WORKDIR /app
Copy Files into the Image:
COPY source destination
Install Dependencies:
RUN apt-get update && apt-get install -y package_name
Expose Ports:
EXPOSE 8080
Building and Running
Build the Docker Image:
docker build -t image_name:tag .
Run a Container:
docker run -p 8080:8080 image_name:tag
Run in Detached Mode:
docker run -d -p 8080:8080 image_name:tag
Environment Variables
Set Environment Variables:
ENV KEY=value
Container Lifecycle
Command to Run on Container Start:
CMD ["executable", "arg1", "arg2"]
Entrypoint (Override CMD):
ENTRYPOINT ["executable", "arg1", "arg2"]
Healthcheck:
HEALTHCHECK CMD curl -f http://localhost/ || exit 1
Cleanup
Remove Intermediate Containers:
FROM base_image:tag AS builder
# Build steps go here
FROM another_image:tag
COPY --from=builder /app /app
Remove Cached Layers:
docker image prune
Multistage Builds
Use Multistage Builds:
FROM base_image:tag AS builder
# Build steps go here
FROM another_image:tag
COPY --from=builder /app /app
These examples cover some basic Dockerfile commands. For more details and options, refer to the official Docker documentation.