NOTE : Docker key words
See all notes || ArchiveDocker file note
Certainly, here are all the commonly used Dockerfile commands along with detailed explanations:
-
FROM:
-
Specifies the base image to use as the starting point for your Docker image.
-
Usage:
FROM image:tag
-
Example:
FROM ubuntu:latest
-
-
RUN:
- Executes shell commands within the image during the build process. Typically used for installing software, updating packages, or any setup tasks.
- Usage:
RUN command
- Example:
RUN apt-get update && apt-get install -y python3
-
COPY:
- Copies files or directories from the host machine into the image. It's often used to add application code and resources to the image.
- Usage:
COPY source destination
- Example:
COPY app.py /app/
-
WORKDIR:
- Sets the working directory inside the container where subsequent commands will be executed. Useful for organizing files and specifying paths.
- Usage:
WORKDIR path
- Example:
WORKDIR /app
-
EXPOSE:
- Informs Docker that the container will listen on specific ports at runtime. It doesn't actually expose the ports; it's for documentation purposes.
- Usage:
EXPOSE port(s)
- Example:
EXPOSE 8080
-
CMD:
- Specifies the default command to run when a container is started from the image. You can override it when starting a container.
- Usage:
CMD ["executable", "param1", "param2"]
- Example:
CMD ["python3", "app.py"]
-
ENTRYPOINT:
- Similar to CMD, it defines the default command to run when a container starts, but it's more rigid and doesn't allow command override. Useful for creating containerized applications that act like executables.
- Usage:
ENTRYPOINT ["executable", "param1", "param2"]
- Example:
ENTRYPOINT ["python3", "app.py"]
-
ENV:
- Sets environment variables within the container. These variables can be used in subsequent Dockerfile commands or by the running application.
- Usage:
ENV key=value
- Example:
ENV MY_VARIABLE=value
-
VOLUME:
- Creates a mount point for external volumes or data to be shared with the container. It allows data persistence and sharing between containers.
- Usage:
VOLUME /path
- Example:
VOLUME /data
-
USER:
- Sets the user or UID (User ID) under which the container will run. It's useful for improving security by running processes with lower privileges.
- Usage:
USER username_or_UID
- Example:
USER appuser