Last active
June 8, 2025 22:03
-
-
Save sd031/13699e946213c773b9246f8274794229 to your computer and use it in GitHub Desktop.
Building a Basic Docker Image Using Dockerfile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Dockerizing Any Application Using Dockerfile - Full Step by Step Process to make docker image: | |
| What is the Dockerfile? | |
| Dockerfiles are instructions. They contains all of the commands used to build an image. | |
| Docker images consist of read-only layers. | |
| Each represents a Dockerfile instruction. | |
| Layers are stacked. | |
| Each layer is a result of the changes from the previous layer. | |
| Images are built using the docker image build command. | |
| Install Docker: | |
| https://docs.docker.com/install/ | |
| Let's start with a Simple Hello World application | |
| Git link: https://github.com/sd031/node_js_hello_world_docker | |
| Now let's try a MEAN App: | |
| Popular Git Repo: https://github.com/linnovate/mean | |
| FROM node:8 | |
| WORKDIR /usr/src/app | |
| ADD . /usr/src/app | |
| RUN yarn | |
| RUN yarn build | |
| ENV NODE_ENV production | |
| ENV SERVER_PORT 4040 | |
| ENV JWT_SECRET 0a6b944d-d2fb-46fc-a85e-0295c986cd9f | |
| ENV MONGO_HOST mongo db url here | |
| ENV MEAN_FRONTEND angular | |
| EXPOSE 4040 | |
| CMD ["yarn", "serve"] | |
| filename: .dockerignore (to avoid adding unnecessery files in docker image) | |
| file contents: | |
| .git/ | |
| dist/ | |
| examples/ | |
| node_modules/ | |
| Change the MongoDb url environment variable to proper mongoDb url | |
| To build image go inside the the project folder and run: | |
| docker image build -t mean_demo:v1 . | |
| To check working fine or not: | |
| if didn't have sparate mongodb url then run: | |
| docker-compose up | |
| if have separate mongodb url: | |
| docker container run --name mean_demo -p 4040:4040 mean_demo:v1 | |
| Now check the url: your browser localhost:4040 | |
| Choose your base image: https://hub.docker.com/search?q=&type=image | |
| Docker image samples: https://docs.docker.com/samples/ | |
| Best light weight base image: https://hub.docker.com/_/alpine/ | |
| Difference: | |
| Use like you would any other base image: | |
| FROM alpine:3.7 | |
| RUN apk add --no-cache mysql-client | |
| ENTRYPOINT ["mysql"] | |
| This example has a virtual image size of only 36.8MB. | |
| Compare that to our good friend Ubuntu: | |
| FROM ubuntu:18.04 | |
| RUN apt-get update \ | |
| && apt-get install -y --no-install-recommends mysql-client \ | |
| && rm -rf /var/lib/apt/lists/* | |
| ENTRYPOINT ["mysql"] | |
| This yields us a virtual image size of about 145MB image. | |
| Dockerfile best practices: | |
| https://docs.docker.com/v17.09/engine/userguide/eng-image/dockerfile_best-practices/ | |
| Know the Commands | |
| FROM: Initializes a new build stage and sets the Base Image | |
| RUN: Will execute any commands in a new layer | |
| CMD: Provides a default for an executing container. There can only be one CMD instruction in a Dockerfile | |
| LABEL: Adds metadata to an image | |
| EXPOSE: Informs Docker that the container listens on the specified network ports at runtime | |
| ENV: Sets the environment variable <key> to the value <value> | |
| ADD: Copies new files, directories or remote file URLs from <src> and adds them to the filesystem of the image at the path <dest>. | |
| COPY: Copies new files or directories from <src> and adds them to the filesystem of the container at the path <dest>. | |
| ENTRYPOINT: Allows for configuring a container that will run as an executable | |
| VOLUME: Creates a mount point with the specified name and marks it as holding externally mounted volumes from native host or other containers | |
| USER: Sets the user name (or UID) and optionally the user group (or GID) to use when running the image and for any RUN, CMD, and ENTRYPOINT instructions that follow it in the Dockerfile | |
| WORKDIR: Sets the working directory for any RUN, CMD, ENTRYPOINT, COPY, and ADD instructions that follow it in the Dockerfile | |
| ARG: Defines a variable that users can pass at build-time to the builder with the docker build command, using the --build-arg <varname>=<value> flag | |
| ONBUILD: Adds a trigger instruction to the image that will be executed at a later time, when the image is used as the base for another build | |
| HEALTHCHECK: Tells Docker how to test a container to check that it is still working | |
| SHELL: Allows the default shell used for the shell form of commands to be overridden | |
| Best Practices: | |
| Follow Principle of the 12 Factor App. | |
| Avoid including unnecessary files. | |
| Use .dockerignore. | |
| Use multi-stage builds. | |
| Don’t install unnecessary packages. | |
| Decouple applications. | |
| Minimize the number of layers. | |
| Sort multi-line arguments. | |
| Leverage build cache. | |
| Follow this link for more best practices: | |
| https://docs.docker.com/develop/develop-images/dockerfile_best-practices/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment