In this tutorial, I will create a Dockerfile step by step. What is Dockerfile? How to use Docker Image command.
Docker Image command
- shows all images: docker images
- creates an image from Dockerfile: docker build
- creates an image from a tarball: docker import
- turns container filesystem into tarball archive stream to STDOUT: docker export
- loads an image from a tar archive as STDIN, including images and tags (as of 0.7): docker load
- saves an image to a tar archive stream to STDOUT with all parent layers, tags & versions (as of 0.7): docker save
- removes an image: docker rmi
- tags an image to a name (local or registry): docker tag
- shows the history of the image: docker history
- creates an image from a container, Pausing it temporarily if it is running: docker commit
Dockerfile step by step
What is Dockerfile?
A Dockerfile is a text document that contains all the commands a user.
FROM, RUN, CMD in Dockerfile
Example Dockerfile for installing the Nginx web server.
FROM centos:7
RUN yum install -y nginx
CMD ["nginx", "-g", "daemon off;"]
Docker shell command line
docker build -t test/nginx:v1 .
docker run -it --rm -p 80:80 test/nginx:v1
Docker build cache
After each build step, Docker takes a snapshot of the resulting image. You can force a rebuild with docker build –no-cache
Docker JSON syntax
Most Dockerfile arguments
Plain string format:
RUN yum install -y nginx
JSON format list:
RUN ["yum", "install", "-y", "nginx"]
COPY, ENV, EXPOSE in Dockerfile
Example Dockerfile
FROM centos:7
RUN yum update -y
RUN yum install -y nginx
# make utf-8 enabled by default
ENV LANG en_US.utf8
COPY index.html /usr/share/nginx/html/index.html
EXPOSE 80 443
CMD ["nginx", "-g", "daemon off;"]
VOLUME in Dockerfile
For example Dockerfile
VOLUME ["/etc/nginx/"]
ENTRYPOINT vs CMD in Dockerfile
#For example
CMD ["nginx", "-g", "daemon off;"]
ENTRYPOINT ["/docker-entrypoint.sh"]
You will use ENTRYPOINT and CMD together
- ENTRYPOINT will define the base command for our container.
- CMD will define the default parameter(s) for this command.
- They both have to use JSON syntax.
More
- MAINTAINER: Set the Author field of the generated images. Ex: MAINTAINER Huu Phan “huupv@gmail.com”
- ADD: Copies new files, directories or remote file to the container. Invalidates caches. Avoid ADD and use COPY instead. Ex: ADD build-nginx /tmp/build-nginx
- STOPSIGNAL: Sets the system call signal that will be sent to the container to exit. Ex: STOPSIGNAL SIGINT
- WORKDIR: Sets the working directory. Ex: WORKDIR /etc/nginx
- USER: Sets the username for following RUN / CMD / ENTRYPOINT commands. Ex: USER nginx
- LABEL: Apply key/value metadata to your images, containers, or daemons. Ex: LABEL architecture=”amd64″
- ARG: Defines a build-time variable. Ex: ARG buildno
- ONBUILD: Adds a trigger instruction when the image is used as the base for another build. Ex: ONBUILD COPY . /app/src
Dockerfile step by step. Thank you for reading the DevopsRoles page!