Docker 101

Docker 101

It is an open-source project that automates the deployment of software applications inside containers by providing an additional layer of abstraction and automation of OS level virtualization on Linux. In simple words, Docker is a tool for running your applications inside containers.

Container package all the dependencies and code your app needs to run into a single file, which will run the same way on any machine.

Terminologies

Docker Container

A container is a standard unit of software that packages up code and all its dependencies, so the application runs quickly and reliably from one computing environment to another.

Container Image

A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings.

How containers work ?

By using the low-level mechanics of the host operating system, containers provide most of the isolation of virtual machines at a fraction of the computing power.

Virtual Machines

VMs are great at providing full process isolation for applications: there are very few ways a problem in the host operating system can affect the software running in the guest operating system, and vice-versa.

Cons of Virtual Machines

However, the isolation comes at a high cost: the computational overhead associated with virtualizing hardware for use by a guest OS is significant.

Image vs Container

ContainerImage
An instance of an image is called container.You have an image, which is a set of layers as you describe. It you start a image, you have a running container of that image.
Isolated Application PlatformRead only templates used to create containers.
Container need to run your application based on images.Build by you or other docker users.
Stored in the docker hub or your local registry.

Docker Daemon

  • The background service running on the host that manages building, running and distributing Docker services.

  • The daemon is the process that runs in the operating system which clients talk to.

Docker Client

  • The command line tool that allows the use to interact with the daemon.

  • More generally, there can be other forms of clients too - such as Kitematic which provide a GUI to the users.

Docker Hub

  • A registry of Docker images.

  • You can think of the registry as a directory of all available Docker images.

  • If required, one can host their own Docker registries and use them for pulling images.

Docker Shell Commands

Run command

  1. docker run hello-world

    • If the image of the name ‘hello-world’ is not present in the local registry, it will be pulled from the Docker Hub.

    • By default, it pulls the image fo the latest version from the Docker Hub.

    • pull command pulls the image, and run command does (pull + execute)

  2. docker run -it busybox bash

    • bash is the command to run.

    • Busybox is a software suite that provides several Unix utilities.

    • -i flag makes the container interactive STDIN mode.

    • -t provides pseudo TTY to the container.

  3. docker run busybox echo "Hello World"

    • echo is a command to print the text.

    • "Hello World" is the text to print.

    • If you’ve noticed, all of that happened pretty quickly. Imagine, booting up a virtual machine, running a command and then killing it. Now we know why they say containers are fast.

List Containers

  1. docker ps

    • Lists all the running containers.
  2. docker ps -a

    • Lists all the containers.
  3. docker ps -q

    • Lists only the container IDs.

Stop Container

  1. docker stop <container-id>

    • Stops the container with the given ID.
  2. docker stop $(docker ps -q)

    • Stops all the running containers.

Remove Container

  1. docker rm <container-id>

    • Removes the container with the given ID.
  2. docker rm $(docker ps -q)

    • Removes all the running containers.

List Images

  1. docker images -a

    • Lists all the images.
  2. docker images -q

    • Lists only the image IDs.

Remove Image

  1. docker rmi <image-id>

    • Removes the image with the given ID.
  2. docker rmi $(docker images -q)

    • Removes all the images.

List Volume

  1. docker volume ls

    • Lists all the volumes.
  2. docker volume ls -q

    • Lists only the volume IDs.

Remove Volume

  1. docker volume rm <volume-id>

    • Removes the volume with the given ID.
  2. docker volume rm $(docker volume ls -q)

    • Removes all the volumes.

Delete all Containers

  1. docker rm -f $(docker ps -a -q)

    • Removes all the containers.

Dockerfile

  • A Dockerfile is a text file that describes how to build a Docker image.

  • It contains all the commands a user could call on the comand line to assemble a Docker image.

Commands

  1. FROM <image>

    • The FROM command specifies the base image that the Dockerfile will build on.

    • The image can be a Docker image name, a Docker image tag, or a Docker image ID.

    • The FROM command is required.

    • Eg: FROM ubuntu:latest

      • In this case, the Dockerfile will build on the latest version of the Ubuntu image.
  2. MAINTAINER <name>

    • The MAINTAINER command specifies the person or organization that built the image.

    • The MAINTAINER command is optional.

    • Eg: MAINTAINER "Arjun Adhikari"

      • In this case, the Dockerfile will be built by Arjun Adhikari.
  3. RUN <command>

    • The RUN command specifies a command that will be run when the image is built.

    • The RUN command is optional.

    • Eg: RUN apt-get update

      • In this case, the Dockerfile will run the apt-get update command.
    • Eg: RUN ["<executable>", "<arguments>"]

      • In this case, the Dockerfile will run the executable with the arguments.

      • Shell form, the command is run in a shell, which by default is /bin/sh -c on Linux or cmd /S /C on Windows.

  4. CMD <command>

    • The main purpose of a CMD is to provide defaults for an executing container.

    • Eg: CMD ["<executable>", "<arguments>"]

      • In this case, the Dockerfile will execute the executable with the arguments.
  5. ENTRYPOINT <command>

    • The ENTRYPOINT command specifies a command that will be run when the container is started.

    • If the ENTRYPOINT isn’t specified, the default command is /bin/sh -c.

    • However, if you want to override some of the system defaults, you can specify own entrypoint and therefore manipulate the environment.

    • The ENTRYPOINT command also allows arguments to be set from the Docker RUN command as well, whereas CMD cannot.

    • Eg: ENTRYPOINT ["<executable>", "<arguments>"]

      • In this case, the Dockerfile will execute the executable with the arguments.
  6. WORKDIR <path>

    • The WORKDIR command specifies the working directory for the container.

    • Sets the working directory for any RUN, CMD, ENTRYPOINT, COPY, and ADD instructions that follow it.

    • It can be used multiple times in the one Dockerfile.

    • If a relative path is provided, it will be relative to the path of the previous WORKDIR command.

    • Eg: WORKDIR /home/user

      • In this case, the Dockerfile will be built in the /home/user directory.
  7. LABEL <key>=<value>

    • The LABEL command specifies a label for the image.

    • It is mainly used for setting metadata for the images.

    • It can be used multiple times in the one Dockerfile.

    • Eg: LABEL version=1.0

      • In this case, the Dockerfile will be labeled with the version 1.0.
  8. EXPOSE <port>

    • The EXPOSE command specifies a port that the container will expose to the host.

    • Informs Docker that the container will be listening on the specified port at runtime.

    • EXPOSE doesnot make the ports of the container accessible to the host.

    • It can be used multiple times in the one Dockerfile.

    • Eg: EXPOSE 8080

      • In this case, the Dockerfile will expose the port 8080.
  9. ENV <key>=<value>

    • The ENV command specifies an environment variable for the container.

    • It can be used multiple times in the one Dockerfile.

    • Eg: ENV PATH=/usr/local/bin

      • In this case, the Dockerfile will set the PATH environment variable to /usr/local/bin.
  10. ADD <source> <destination>

  • The ADD command specifies a file or directory that will be copied into the container at runtime.

  • Eg: ADD /home/user/file.txt /home/user/file.txt

    • In this case, the Dockerfile will copy the file.txt from the host to the container.
  1. COPY <source> <destination>
  • The COPY command specifies a file or directory that will be copied into the container at runtime.

  • Eg: COPY /home/user/file.txt /home/user/file.txt

    • In this case, the Dockerfile will copy the file.txt from the host to the container.

COPY only supports the basic copying of local files into the container, while ADD has some features (like local-only tar extraction and remote URL support) that are not immediately obvious. Consequently, the best use for ADD is local tar file auto-extraction into the image, as in ADD rootfs.

Example - Lint Python Code from a Repository

FROM ubuntu:latest

WORKDIR /workspace


RUN apt-get update && apt-get install -y \
    git python3-pip

RUN git clone https://github.com/theArjun/python-codes 
RUN pip3 install pylint
RUN pylint python-codes