DEV Community

Cover image for Part 1: Learning Docker for beginners
Suraj Bera
Suraj Bera

Posted on

Part 1: Learning Docker for beginners

What is a Dockerfile?

It contains instruction that docker uses to package up an application into an image.

Containers and Images, What are these?

  • Images: An image includes everything an application needs to run. It includes application files, environment variables and so on.
  • Container: Once we have an image, we can start a container from it. A container is like a VM in a sense that it provides an isolated environment for executing an application. Similar to VM we can stop and restart a container.

Simplified Analogy: Think of images as a class(blueprint) and containers as running instance(objects) for that blueprint.

Steps to create a Dockerfile:

  • Choose a correct base image
  • Setup the working directory
  • Copy application files into the image. Example: COPY <source> <destination>
  • Exclude files & folders(node_modules, venv, etc)
  • Adding environment variables
  • Exposing ports

By default docker runs an application with the root user that has the highest privileges.

Diff. between CMD and RUN instructions:

  • With both these we can execute instructions. The RUN instruction is a build time instruction. This is executed at the time of building an image. Example: npm install
  • The CMD instruction is a runtime instruction, it's executed when starting a container.

Here is a sample Dockerfile for a simple react application. Here is a list of useful commands.

Top comments (0)