Introduction To Docker

·

4 min read

Virtual Machines

● Isolates applications and allocates resources to run that application

● VMs can be shared as images

● Aren’t dependent on the Host OS

● Multiple VMs can be run simultaneously using a hypervisor

Docker Containers

● Standard unit of software

● Packages code and dependencies

● Can be shared as Docker Images

● Multiple containers can be run simultaneously

● Portable - Can be used with any OS

● Lightweight - Uses the host operating system

● Secure - Strong default isolation features

● Sometimes used with VMs

Microservices

● Breaks large applications down into smaller executable components

● Easy to maintain and test

● Loosely coupled and can be deployed independently

● Can be combined with serverless architecture (AWS Fargate)

Why Use Docker

● Develop applications that work on any OS

● Easy to share applications among teams

● Easy to scale across multiple servers

● Large applications can be broken into multiple containers - one for each microservice

● Great solution for Cloud Computing

● Big community and library of Docker Images

Serverless

● Removes Dependency on Infrastructure

● Allows developers to focus on application development

● Microservices can be decoupled with different cloud services

● Usually more cost effective

● Probably covered in more depth in a Cloud Computing class

Install Docker Engine

You can create an Ubuntu EC2 Instance on AWS and run the below commands to install docker.

sudo apt update
sudo apt install docker.io -y

Start Docker daemon

You use the below command to verify if the docker daemon is actually started and Active

sudo systemctl status docker
sudo systemctl start docker

Grant Access to your user to run docker commands

To grant access to your user to run the docker command, you should add the user to the Docker Linux group. Docker group is create by default when docker is installed.

sudo usermod -aG docker $USER

Docker is Installed, up and running 🥳🥳

Use the same command again, to verify that docker is up and running.

docker run hello-world

Output should look like

....
....
Hello from Docker!
This message shows that your installation appears to be working correctly.
...
...

Pull An Image

There are many publicly available images that we can use to work with Docker. The example below pulls a hello-world image using the docker pull command:

[ ~ ]docker pull hello-world

Using default tag: latest

latest: Pulling from library/hello-world

0e03bdcc26d7: Pull complete

Digest:sha256:31b9c7d48790f0d8c50ab433d9c3b7e17666d6993084c002c2ff1ca09b96391d

Status: Downloaded newer image for hello-world:latest

docker.io/library/hello-world:latest

[ ~ ]docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

hello-world latest bf756fb1ae65 12 months ago 13.3kB

Create A Container

To create a container from an image we can use the docker create command

[ ~ ]docker create hello-world

2ffd5f2c5a7562fbf1d7b89a14c11a52e5843dd7938f380a8cd53f3952da99de

Run A Container

To run a container we can use the docker container start command to start a container. The -i runs the container interactively and allows us to see the output

[ ~ ] docker container start -i 2ffd5f2c5a7562fbf1d7…

Hello from Docker!

This message shows that your installation appears to be working correctly.

Run An Image

There is a shortcut for building a container from an image and running it with the docker run command. This will create a new container for an image and run it:

[ ~ ] docker run hello-world

Hello from Docker!

This message shows that your installation appears to be working correctly.

List Images

To see what images are already installed on your machine you can use the docker image ls command. We can see our hello-world image below:

[ ~ ]docker image ls

REPOSITORY TAG IMAGE ID CREATED SIZE

ubuntu latest f63181f19b2f 13 hours ago 72.9MB

hello-world latest bf756fb1ae65 12 months ago 13.3kB

List Containers

To list the containers that we have built, we can use the docker container ls command. The -a flag allows us to see both stopped and running containers. There are two containers below, one that was built with the docker create command and the other that was built with docker run:

[ ~ ]docker container ls –a

List Running Processes

To see what containers are currently running, we can use the docker ps command. This is useful when you are running containers in the background.

[ ~ ]docker ps

That's a wrap.......