Introduction to Container (Docker)

HackMD Link

1. What is container ?

The difference between Operating System,Virtual Machine and Container: image

A container is a lightweight, portable unit that packages an application together with all its dependencies (libraries, configuration files, etc.)

  • Packaging the application with its dependencies allows it to run consistently across different environments (independence of package version or platform).
  • The container processes are isolated from other processes on the same host operating system (in terms of resources, network, filesystem, user permission).
  • Unlike virtual machines, processes running in the containers can directly call the kernel API or make system call of the host OS.

2. Image and Container

  • Image is a read-only template that contains everything needed to run an application (code, environment settings, libraries and dependencies)
  • Container is a running instance of an image. It can be treated as a normal process with isolated filesystem, processes, network, and resources. Therefore, it can be started, stopped, paused, and deleted independently.

Image is the snapshot or template of the ingredient to run a process (immutable), the container is the actual process (mutable).

3. Docker Daemon and Client

  • To manage the images and containers using Docker, one must install and run the docker daemon (Docker Desktop). The daemon is a special kind of application which runs in the background and listening to requests.
  • The actual commands (requests) to manage the images and containers is sent using the docker client (Docker CLI, included in Docker Deskop).

In some cases, the client and the daemon needs to be installed separately.

4. Syntax in Terminal

List Installed Images - images

$ sudo docker images

image

  • Check Docker Hub for the public available images.

Download Images – pull

$ sudo docker pull [images_ID]

image

  • A Docker image is made up of stacked read-only layers. Each layer represents a set of filesystem changes. This allows the repository to check the changes between different layers to improve the efficiency of managing an image.

image

Download Images – rmi

$ sudo docker rmi [IMAGE_ID]

image

Create and Run Container Instance – run

$ sudo docker run [PARAMETERS] [IMAGE]:[TAG] [COMMAND]
$ sudo docker run [PARAMETERS] [IMAGE_ID] [COMMAND]

image

  • A Docker container will be created based on the quay.io/biocontainers/biopython image (version 1.76—2).
  • Then execute the command python -c “from Bio.Seq import Seq; seq = Seq(‘ATGCGT’); print(‘Sequence:’, seq, ‘Reverse complement:’, seq.reverse_complement())“
  • The container process finish the execution and exit.
$ sudo docker run –v [HOST-PATH]:[CONTAINER-PATH] [IMAGE]:[TAG] [COMMAND]

image

  • The container has its own filesystem, so it cannot read the files on the host operating system.
  • To allow the container to read the files on the host operating system, use -v to mount the directory (use absolute path)
  • In this case, the file /mnt/c/Users/dn070/Workspace/seq.txt on the host operating system will be mapped to /seq.txt in the container.
$ sudo docker run –p [HOST-PORT]:[CONTAINER-PORT] -d [IMAGE]:[TAG] [COMMAND]

image

  • Some daemon processes are running in the background continuously, they will not be terminated unless manually stopped.
  • Use detach mode (-d) to disable the shell from waiting for the docker container to return the output and exit.
  • Use port mapping (-p) to map the port from host to the container. image

Check Container Process Information – ps

$ sudo docker ps [-a]

image

  • Use -a to show the information of stopped processes

Check Container Logs – logs

$ sudo docker logs [CONTAINER-NAME|CONTAINER-ID]

image

  • Docker logs show the standard output and standard error of the running process (particularly useful for processes running in detached mode)

Create a Shell CLI in Running Container – exec

$ sudo docker exec -it [CONTAINER-NAME|CONTAINER-ID] bash

image

  • Note that it doesn’t creates a new container process. Instead, it creates a new process inside the container.
  • This is particularly useful to view the file system or check log files in the running container.
  • It is also possible to run other processes, just replace bash with the processes name.

Stop Running Process – stop

$ sudo docker stop [CONTAINER-NAME|CONTAINER-ID]

image

Restore Stopped Process – start

$ sudo docker start [CONTAINER-NAME|CONTAINER-ID]

image

  • You should be able to see the nginx content on the browser again (localhost:8080)

Remove Stopped Process – rm

$ sudo docker rm [CONTAINER-NAME|CONTAINER-ID]

Build Docker Image from Scratch – build and Dockerfile

1.Create custom index.html file to show on the nginx server. image

2.Create a Dockerfile image

  • Create an image based on the nginx:latest (base image)
  • Command to executed (in order to create the new image)
    • RUN: execute shell command (e.g. apt install, rm, mv)
    • COPY: copy file from hosting OS to the container.
  • This container expose 80 port (for documentation purpose)
  • If no command provided in docker run, running container based on this image will execute the command in the ENTRYPOINT

3.Build custom image. 4.Run the container based on the built image. image image

Related articles

Garage S3 Introduction and Usage Guide

Garage is an open-source distributed object storage platform that implements the Amazon S3 API. It allows users to store data on their own infrastructure while still using S3-compatible tools and w…

Training

Kubernetes Introduction Guide

Kubernetes is an open-source container orchestration system originally released by Google. It manages containerized applications across many machines and provides automation for scheduling, scaling…

Training