Skip to content

Latest commit

 

History

History
257 lines (147 loc) · 9.56 KB

File metadata and controls

257 lines (147 loc) · 9.56 KB

5. "Hello, world!" Docker

1-lxc-docker

What is LXC (Linux Containers)?

LXC (Linux Containers) is an operating-system-level virtualization method for running multiple isolated Linux systems (containers) on a control host using a single Linux kernel.

What is Docker

Docker is an open-source project that automates the deployment of applications inside software containers, by providing an additional layer of abstraction and automation of operating-system-level virtualization on Linux.

Installing Docker on Ubuntu Linux

Follow the Ubuntu Xenial 16.04 (LTS) instructions here to install Docker Engine.

Note: Make sure to skip the section labelled Ubuntu Precise 12.04 (LTS) as it is not applicable to the version of Ubuntu Linux we are using.

Keep going until you get to sudo docker run hello-world and you will see the following.

2-sudo-docker-run-hello-world

Optional Docker Configuration

Now continue on and complete the following optional configurations as they will make running Docker lower friction and more enjoyable for you.

  1. Manage Docker as a non-root user so you don't have to run Docker with sudo.

  2. Enable memory and swap accounting primarily to avoid getting spammed with warnings.

  3. Configure Docker to start on boot so you don't have to remember to start it every time.

Once you have completed the three optional Docker configuration steps above, reboot and then run docker version. You should see the following output which confirms that Docker Daemon is running.

3-docker-version

.NET Core "Hello, world!" on Docker

Lets give .NET core and Docker a go. First we'll get the latest dotnet docker image and run it.

docker run -it microsoft/dotnet:latest

4-docker-run-dotnet

Create a new dotnet core app.

mkdir DotNetCoreOnDocker
cd DotNetCoreOnDocker
dotnet new

5-dotnet-new

Restore the dependencies and run the app.

dotnet restore
dotnet run

6-dotnet-run

Superb! You have now seen "Hello, world!" on:

  1. .NET Core,
  2. ASP.NET Core
  3. Docker, and
  4. .NET Core in a Docker container.

That's a lot of "Hello, world!", let's take a step back and learn more about Docker.

Docker 101

Now what just happed there!!! Well first of all, docker pulled down the latest microsoft/dotnet image from docker hub and ran that in a new Docker container with the -it switch. Let's go back a bit and explain some of the core components of Docker to better understand what that means.

The 3 main concepts to understand are: Images, Containers and Registries.

Images

Docker images are the recipes for building containers. They are read-only and are usually composed of layers of other images with a base image of something like Ubuntu, Readh at or Debian. The layers are built from Dockerfile's, which are just a set of instructions of how to build the image.

Containers

Docker containers are running/runnable instances of docker images. Think if images and containers like classes and objects.

Registries (i.e. Docker Hub)

Docker registry's are repositories for docker images. The default public registry is the docker hub. Images are stored on the local host when pulled from the registry.

7-docker-architecture

Docker itself is actually a client-server application where the client is the CLI and the server is the docker daemon (a daemon is like a windows service). The CLI is how we send commands to the server. The server then does the heavy lifting of pulling images from the registry and building containers.

Now let's go back to our initial command.

docker run -it microsoft/dotnet:latest

Because we did not already have the Docker image on our machine, Docker will go to the registry (Docker Hub) and download the image before building the container and running it.

Exploring the Docker CLI

We left our terminal inside the actual running Docker container, so let's exit that and give a few of the basic docker CLI commands a try.

First of all let's have a look at the images available on the local machine.

docker images

You should see a list with 2 images like this.

8-docker-images

Next, let's have a look at the running containers.

docker ps

9-docker-ps

Hmm... No containers running... But we might have some stopped containers.

docker ps -a

This will give us all containers on the system, running or not.

10-docker-ps-a

When we started this chapter we started the microsoft/dotnet container with the -it switch. now let's start it again but this time we'll add d to the switches as follows.

docker run -itd --name dotnet microsoft/dotnet:latest

11-docker-run-daemon

This creates a new container and starts it but does not attach the console to it. Instead it just returns the id of the container.

Run docker ps to confirm that we now have a running container.

12-docker-ps-again

Now to attach to the console of the running container run.

docker attach dotnet

You may have to press the ENTER key in order to see the ssh session.

Pro Tip! To escape the container without stopping it, use the escape sequence Ctrl-p + Ctrl-q.

13-docker-attach

You can now run the following set of commands to clean up your dotnet container.

Cleaning up the dotnet container

Run the following command to kill (terminate) the running Docker container dotnet.

docker kill dotnet

Run the following command to rm (delete) the dotnet Docker container from the filesystem.

docker rm dotnet

14-docker-cleanup

Cleaning up everything

That's just a basic introduction to running Docker commands. Run the following and have a look around at all the possible commands you could run.

docker --help

Here are a few examples of some helpful commands for cleaning everything up.

Stop all containers

docker kill $(docker ps -q)

Remove all containers

docker rm $(docker ps -a -q)

Remove all docker images

docker rmi $(docker images -q)

For a more in-depth Docker have a look at the Docker Docs.

All right stop, collaborate and listen!

So you have seen what Docker can do on Linux. Let's see what Docker can do on macOS and Windows as well.

Note: If you're not running on macOS or Windows you have one of the following two options:

  1. Continue on with 6. "Hello, world!" via Node.js & Yeoman where you will learn about Node.js and scaffolding parts of you .NET Core and ASP.NET Core application with Yeoman.

  2. Spend some time learning more about .NET Core and building your own application. The .NET Core Concepts will be useful to you.

Docker for macOS and/or Windows

Recently Docker has also been released for macOS and Windows. Both macOS and Windows run the CLI (client) as a native application, but run the Docker Engine (daemon) through a hypervisor. Hyper-V on Windows and xhyve on macOS.

Running "Hello, World!" on macOS on Docker

Have a go at running Docker on macOS using Docker.dmg

Keep going until you get your "Hello, World!".

15-hello-world-macos

Running "Hello, World!" on Windows on Docker

Have a go at running Docker on Windows using InstallDocker.msi.

Keep going until you get your "Hello, World!".

16-hello-world-on-windows

Kitematic (beta)

Kinematic is a useful GUI application for managing Docker images and containers on macOS and Windows:

Kitematic is an open source project built to simplify and streamline using Docker on a Mac or Windows (coming soon) PC. Kitematic automates the Docker installation and setup process and provides an intuitive graphical user interface (GUI) for running Docker containers. Kitematic integrates with Docker Machine to provision a VirtualBox VM and install the Docker Engine locally on your machine.

Have a go at installing and using the Docker Toolbox via either DockerToolbox-1.12.3.pkg on macOS or DockerToolbox-1.12.3.exe on Windows.

Have a play around with the product, install some containers and see if you can find something interesting to you. Share what you have found with your neighbours.

17-kitematic-dash

End of Part 5

Excellent! You have dug into Docker on Linux and you have also seen Docker running on either macOS or Windows.

Take a break and then continue with 6. "Hello, world!" via Node.js & Yeoman.