Dockerfile
Dockerfile is a text document that containes all the commands a user could call on the command line to assemble an image. Using docker build users can create ana automated build that executes several command-line instructions in succession.
Whenever possible, Docker will reuse the intermediate images(cache), to accelerate the docker build process significantly.
Here is the format of the Dockerfile:
# Comment
INSTRUCTION arguments
A Dockerfile must start with a "FROM" instruction
FROM -> Used to specify the base image from your custome image
USER -> User that will run the container
ADD -> Copy some file in your current directory into some dir inside the image
EXPOSE -> informs Docker that the container listens on the specified network ports at runtime
RUN -> run some commands in the container
CMD -> command that will be executed when the container started
WORKDIR -> The working directory of the running container
ENV -> Inject some environment variable inside the container
Example:
FROM centos:latest
RUN yum -y install epel-release && yum -y install nginx && yum clean all
ADD index.html /usr/share/nginx/html/
EXPOSE 80 443
RUN systemctl enable nginx
CMD ["/usr/sbin/init"]
These are written in a file named "Dockerfile"
Build the image
# docker build -t custom-nginx .
. -> means current directory
t --> means "allocate a terminal"
Run the image
# docker run -d --name nginx -p 8080:80 custom-nginx
8080:80 -> forward port 8080 to 80
Here is one example.
Whenever possible, Docker will reuse the intermediate images(cache), to accelerate the docker build process significantly.
Here is the format of the Dockerfile:
# Comment
INSTRUCTION arguments
A Dockerfile must start with a "FROM" instruction
FROM -> Used to specify the base image from your custome image
USER -> User that will run the container
ADD -> Copy some file in your current directory into some dir inside the image
EXPOSE -> informs Docker that the container listens on the specified network ports at runtime
RUN -> run some commands in the container
CMD -> command that will be executed when the container started
WORKDIR -> The working directory of the running container
ENV -> Inject some environment variable inside the container
Example:
FROM centos:latest
RUN yum -y install epel-release && yum -y install nginx && yum clean all
ADD index.html /usr/share/nginx/html/
EXPOSE 80 443
RUN systemctl enable nginx
CMD ["/usr/sbin/init"]
These are written in a file named "Dockerfile"
Build the image
# docker build -t custom-nginx .
. -> means current directory
t --> means "allocate a terminal"
Run the image
# docker run -d --name nginx -p 8080:80 custom-nginx
8080:80 -> forward port 8080 to 80
Here is one example.