Docker

How is Docker different from a virtual machine?

VM vs Container:
  • VM
  • A Virtual Machine, on the other hand, is not based on container technology.
    They are made up of user space plus kernel space of an operating system.
    Under VMs, server hardware is virtualized. Each VM has its own Operating system (OS) and apps. It shares hardware resource from the host.

  • Container
  • Docker is container based technology and containers are just user space of the operating system.

    At the low level, a container is just a set of processes that are isolated from the rest of the system, running from a distinct image that provides all files necessary to support the processes. It is built for running applications.
    In Docker, the containers running use the same host OS kernel.

You can use Docker to isolate individual applications, and use Virtual Machines to isolate entire systems. They are operating at different levels of abstraction.

Containers are typically much smaller and faster, which makes them a much better fit for fast development cycles and microservices.

Containers don’t do true virtualization; you can’t run a windows container on a Linux host for example.

Docker is basically using the OS-level virtualization, Linux namespaces and control groups, for example. Its overhead is very thin compared to a virtualization technique, like Hypervisor used by virtual machines.

Containers allow you to encapsulate your application dependencies and allow that to be included as part of your application development(the code and everything your application depends on: libraries, runtime versions, etc).

docker for beginners

Learn to build and deploy your distributed applications easily to the cloud with Docker.

INTRODUCTION

Docker is a tool that allows developers, sys-admins etc. to easily deploy their applications in a sandbox (called containers) to run on the host operating system i.e. Linux.

The key benefit of Docker is that it allows users to package an application with all of its dependencies into a standardized unit for software development. This decoupling allows container-based applications to be deployed easily and consistently, regardless of whether the target environment is a private data center, the public cloud, or even a developer’s personal laptop.

GETTING STARTED

Install Docker Engine on Ubuntu

  • Set up the repository
  •   
    $ sudo apt-get install ca-certificates curl gnupg lsb-release  
      	
    Add Docker’s official GPG key
      
    $ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
    	
    Use the following command to set up the stable repository.
      
    $ echo \
      "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
      $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
    	
  • Install Docker Engine
  • 
    $ sudo apt-get update
    $ sudo apt-get install docker-ce docker-ce-cli containerd.io  
      	
  • Verify that Docker Engine is installed correctly
  • Running the hello-world image.
      
    $ sudo docker run hello-world
    Unable to find image 'hello-world:latest' locally
    latest: Pulling from library/hello-world
    2db29710123e: Pull complete 
    Digest: sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f
    Status: Downloaded newer image for hello-world:latest
    
    Hello from Docker!
    This message shows that your installation appears to be working correctly.
    
    To generate this message, Docker took the following steps:
     1. The Docker client contacted the Docker daemon.
     2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
        (amd64)
     3. The Docker daemon created a new container from that image which runs the
        executable that produces the output you are currently reading.
     4. The Docker daemon streamed that output to the Docker client, which sent it
        to your terminal.
    
    To try something more ambitious, you can run an Ubuntu container with:
     $ docker run -it ubuntu bash
    
    Share images, automate workflows, and more with a free Docker ID:
     https://hub.docker.com/
    
    For more examples and ideas, visit:
     https://docs.docker.com/get-started/
    
    	
    This command downloads a test image and runs it in a container.
    When the container runs, it prints a message and exits.
You need to use sudo to run Docker commands.
To allow non-privileged users to run Docker commands and for other optional configuration steps, see Linux postinstall.

Uninstall Docker Engine

  • Uninstall the Docker Engine, CLI, and Containerd packages:
  • 
    $ sudo apt-get purge docker-ce docker-ce-cli containerd.io  
      
  • To delete all images, containers, and volumes:
  • 
    $ sudo rm -rf /var/lib/docker
    $ sudo rm -rf /var/lib/containerd  
      

HELLO WORLD

Playing with Busybox

To run a Busybox container,
  • fetches the image
  • The pull command fetches the busybox image from the Docker registry and saves it to our system.
    
    $ sudo docker pull busybox
    Using default tag: latest
    latest: Pulling from library/busybox
    3cb635b06aa2: Pull complete 
    Digest: sha256:b5cfd4befc119a590ca1a81d6bb0fa1fb19f1fbebd0397f25fae164abe1e8a6a
    Status: Downloaded newer image for busybox:latest
    docker.io/library/busybox:latest    
        
    To list all images on your system:
    
    $ sudo docker images
    REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
    busybox       latest    ffe9d497c324   2 weeks ago    1.24MB
    hello-world   latest    feb5d9fea6a5   2 months ago   13.3kB  
      
  • run
  • Load up the container and then runs a command in that container:
    
    $ sudo docker run busybox echo "hello from busybox"
    hello from busybox  
      
    Show a list of all containers that we ran:
    
    $ sudo docker ps -a
    CONTAINER ID   IMAGE         COMMAND                  CREATED          STATUS                      PORTS     NAMES
    73d35e9a8883   busybox       "echo 'hello from bu…"   5 minutes ago    Exited (0) 5 minutes ago              goofy_panini
    130d470e951c   hello-world   "/hello"                 23 minutes ago   Exited (0) 23 minutes ago             adoring_mendeleev
      
      
    Use docker run --help to see a list of all flags it supports.
    For ex,
      -t, --tty                            Allocate a pseudo-TTY
      -i, --interactive                    Keep STDIN open even if not attached 
      
    Running the run command with the -it flags attaches us to an interactive tty in the container. Now we can run as many commands in the container as we want.
    
    $ sudo docker run -it busybox sh
    / # whoami
    root
    / # ls
    bin   dev   etc   home  proc  root  sys   tmp   usr   var
    / #   
      
  • clean up containers
  • Once you're done with containers, you can delete the container with the container's ID,
    
    $ sudo docker rm b1836ad4ee6e 9fce066a8c10 73d35e9a8883    
        

Terminology

  • Images
  • The blueprints of our application which form the basis of containers. In the demo above, we used the docker pull command to download the busybox image.
  • Containers
  • Created from Docker images and run the actual application. We create a container using docker run which we did using the busybox image that we downloaded. A list of running containers can be seen using the docker ps command.
  • Docker Daemon
  • The background service running on the host that manages building, running and distributing Docker containers. The daemon is the process that runs in the operating system which clients talk to.
  • Docker Client
  • The command line tool that allows the user 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 can use them for pulling images.

留言

熱門文章