14 Nov 2023
Docker Containers
What is Virtualization:
- It’s a process where software is used to create an abstraction layer over computer hardware.
- It allows H/W components of a single machine to be divided into multiple VMs which share these computing resources
- The abstraction software is called hypervisor, which separates VMs from one another and allocates processors, memory and storage among them
What are Containers:
- More agile way of handling virtualization, it abstracts the OS layer rather than H/W
- Rather than spinning up entire VM, containerization packages together everything needed to run an application/microservice (along with run time libraries they require to run)
- Container includes the code, its dependencies and even the OS itself. This allows application to almost anywhere.
- Container uses a form of virtualization where the leverage the feature of host OS to isolate processes' access to CPU, memory and disk
- Containers share the same OS kernel
Installation - Ubuntu:
Update the Ubuntu System Packages
--updates package info so that the system knows which one to upgrade
sudo apt-get update
--performs the upgrades of the packages installed on the system to the newest versions
sudo apt-get upgrade
Install the required dependencies for connecting to a Repo over https
sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
- This is part of a dependency package which is required for Docker to run
- apt-transport-https
- Ubuntu uses this to allow apt-get to download packages via HTTPS
- ca-certificate
- This package contains a list of common Certificate Authorities
- CURL
- Client URL Request Library
- A tool to get and send files using a URL syntax
- Used for downloading repo files for a package
- gnupg-agent
- This is used to manage secrets(private keys).
- You need to add Docker's official GPG key on your system
- software-properties-common
- Tool used to manage package sources
- Without this you would have to manually add/remove package repos on your system
Add the official GPG key from Docker
- Most Linux packages are verified using its GPG key - to verify authenticity & integrity of packages
- Use below command to download and add official GPG key from Docker into the Ubuntu systems
- The downloaded key is added to the list of trusted keys in the system
curl - fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
- In order to verify the authenticity of the downloaded key, fingerprints are used.
- Docker has provided the fingerprint in their docs so that it can be verified with the one we downloaded
- Use below command to verify the fingerprint
sudo apt-key finger 0EGHSSA - replace this with fingerprint
Add the Docker stable repo to your system
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
• The info will be added in /etc/apt/sources.list
sudo cat /etc/apt/sources.list
• Run apt-get update again to update new packages that were installed earlier
sudo apt-get update
Install Docker CE
sudo apt-get install docker-ce
• The other package dependencies that will be installed alongside docker:
○ Docker-ce-cli
○ Containerd.io
• This will also start the docker daemon (service)
• Verify the docker
sudo docker version
○ This provides info about client and engine both
The Docker Ecosystem:
• Docker CLI
• Docker Daemon
• Docker Commands - https://docs.docker.com/engine/reference/commandline/docker
Docker Images, Containers and Hub:
• Image is static, read-only template
• Non-running representation of all the components required to run a corresponding container application
• Container, however, is a run time instance of the image.
• Hub is a container registry or repo. It is public repo and cloud based
○ Used for creating, sorting, testing and distributing Docker images
○ It has got below services & features:
§ Repo - storage where images are saved for either public or private use
§ Official Images - set of repos providing base OS images, download-and-use image of programming languages and platforms
§ Publisher images - images from software vendors like Microsoft, Google, IBM etc.
§ Builds - allows automatically build and upload images from Git repos
§ Webhooks - integrate with other services through automation
□ Eg. Creating an automation server when you successfully upload a Docker image to Docker hub
§ https://hub.docker.com
§ Docker search microsoft | grep sql
Docker image naming convention:
• REGISTRY[:PORT]/REPO/IMAGE[:TAG]
• Registry hostname is a DNS comply name, default value is docker hub: https://index.docker.io
○ Run docker info command to get the details or registry
○ Available public container registeries
§ Microsoft Container Registry (MCR) : mcr.microsoft.com
§ Amazon Elastic Container Registry (ECR) : dkr.<region>.amazonaws.com
§ Google Container Registry (GCR) : gcr.io
• Repository name
○ Repo structure depends on how the images are stored on the container registry
○ For eg. Docker.io/microsoft is MS official repo on Docker Hub
• Image name
○ Identifies the contents of the Docker image
○ With numerous versions, editions, and OS combo, relying solely on image name isn't enough to get the right image
○ That is where tags come in
• Tags
○ To further identify the contents of the Docker image, providing additional details
○ If tag name is not provided, Docker will pull the latest tag that the corresponding image has in the repo
Docker run hello-world is equivalent to docker run hello-world:nanoserver-sac2016 or docker run hello-world:latest
○ List available tags for SQL server on Linux image - https://hub.docker.com/_/microsoft-mssql-server
Running a SQL Server on Linux Container:
• Pull the image
○ Docker pull mcr.microsoft.com/mssql/server:2017-CU14-ubuntu
• Run the container
○ Docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=D0cker@098!QAZ" -p 1433:1433 --name sqldevlinuxcon01 -d -h linuxsqldev01 mcr.microsoft.com/mssql/server:2017-CU14-ubuntu
○ -e : sets required env. Variables for SQL
○ -p : publishes container's TCP port(range) to the host - ip:hostPort:containerPort - mapping port 1433 on the host to port 1433 on the container
○ --name : custom name to help ifentify the container instead of a system-generated one
○ -d : runs the container in detached mode (background process) and prints the container ID. It ensures container still runs in background post docker run command.
○ -h : server hostname that you want to assign to the container
• Check for errors/status of containers
○ Docker images
○ Docker ps -a
Lifecycle of a container:
• Docker ps -a
• Created: when you create a container using docker create command but not run it
• Running: This is when container is up and running and doing its job
• Exited: This is when container has gone through running and completed its job. When primary process is not running, the container exits
• Paused: This is when you choose to pause the container using docker pause command, suspending all the processes
• Restarting: A container where a restart policy has been configured
• Dead: This is when a docker daemon attempted to stop the container but failed
• Docker run = docker create + docker start
○ Docker create hello-world
○ Docker start -a containerID
Some important docker commands:
Docker version -
Docker info
Docker run
Docker search
Docker pull
Docker images
Docker ps -a