Introduction to Docker
Tags: docker, containers, dockerhub
Published: 2023-10-12 09:25:54

Docker allows users to package an app with all of its dependencies into a standardized unit for software development. Docker is a tool to easily deploy applications in a sandbox(called containers) to run on the host operating system i.e. Linux. The sandbox for kids is a box filled with sand and toys they can play with, it’s placed in the middle of the grasses in the backyard where they can play.

**What is a Containers:** 
A container is a sandboxed process running on a host machine that is isolated from all other processes running on that host machine. container is an “instance” of an image. A running container uses an isolated filesystem.

**What is an Images:** 
The isolated filesystem is provided by an image, image must contain everything like dependencies, configurations, scripts, binaries etc.

Difference between images and containers
Docker images and containers are closely related terms and its common to confuse the two for beginners. Docker images are built by executing commands in the Docker File and what it primarily does is that it captures the run-time environment parameters so that if it runs again in some other machine, it runs exactly the same as the current machine. On the other hand, a container is an “instance” of an image.In simple words, docker image be a blueprint of a building, and a container would then be the actual building!

**Writing a Docker file**
Docker file is used to specify instructions to create a Docker container. It is essentially a list of commands in a text file( Docker file has no extension).
You open a dockerfile using the touch command.
    $ touch dockerfile

- A dockerfile is typically constructed from a base image such as Ubuntu(OS), SQL to allow a primary setting of the environment. This is done as follows:
     Syntax: FROM <base-image-name>
     Example: FROM ubuntu:latest

- It is then essential to establish the working directory. You either create one using mkdir command or setting an existing directory as working directory by using
    WORKDIR /<app_name>
    # And then copy the contents in the container
    COPY . /<app_name>

- You can then expose ports or run preferred commands. Some examples are below →
    # Exposing port 8080
      EXPOSE 8080

    # Installing dependencies
    RUN apt-get install -y python
    # Running Python
    CMD ["python", "main.py"]

Note: The difference between RUN and CMD is that the RUN command is executed while building the image and CMD command is executed when we start the container.

In the end, it will look like this:
     FROM ubuntu:latest
     FROM node:18
     # Create app directory
     WORKDIR  /app
     # Install app dependencies
     COPY package*.json ./package.json
     COPY package-lock.json ./package-lock.json
     RUN npm install
     # Bundle app source
     COPY . /app
     EXPOSE 8080
     CMD [ "node", "index.js" ]
     
Once the Docker file is created, you can just build the image using the command
   $ docker build
Docker will find the dockerfile in the current directory and build a docker image based on it.
   
To see the list of docker images available locally
$ docker images
REPOSITORY     TAG      IMAGE ID        CREATED             SIZE
hello-world   latest    bf756fb1ae65   6 months ago        13.3kB

Similarly, you can get a list of containers in a tabular format using the command
$ docker ps
CONTAINER ID   IMAGE          COMMAND                 NAMES
e90b8831a4b8   nginx        "/bin/bash -c 'mkdir'"   my_nginx
00c6131c5e30   telegraf:1.5   "/entrypoint.sh"       my_telegraf

Create a container from a docker image and run it
$ docker run [<image_name> or <image_id>]

You can start or stop a container using the start and stop command
$ docker start <container_id>
$ docker stop <container_id>

You can pull or push an image to Docker Hub. Docker Hub is the world's largest library and community for container images.
$ docker push <image_name>
$ docker pull <image_name>

You can also remove images and containers using rm or rmi command
$ docker rm <container_id>
$ docker rmi <image_id>

Hope that has helped you, thanks for reading.