Neoxian's guide to Docker

in #programming9 years ago (edited)

No, this isn't a Steemit whale, it's the Docker whale! from https://www.docker.com/

I'm just going to share with you some basics of using docker. I found it very handy when setting up my Steem Witness node.

What is it?

Docker allows you to run a virtual, contained system. It is a little bit like VMWare or Virtual box, except those two emulate computer hardware where as Docker can make use of your existing OS kernel. This makes Docker much more lightweight and lean, but still gives you contained, virtual operating system.

Installing

First you have to install docker. I'm not going to go into too much detail for this. If you are on Linux, you can use the package management system and install.

For windows, you can get it from here: https://docs.docker.com/docker-for-windows/

Some caveats, you'll need 64 bit windows 10 pro for this, and you may have to go into the Bios and turn virtualization on.

Making sure it works

You'll want to run the command
docker info

You should get something like this:
neo@hanxia ~ $ docker info
Containers: 1
Running: 1
Paused: 0
Stopped: 0
Images: 4
Server Version: 1.12.6
Storage Driver: aufs
etc..

If you don't get this, then something is not working. You may have to start the Docker daemon, and/or put your user in the 'docker' group.

The next basic thing you can do is to run a simple container:
docker run hello-world

What this does is to download a simple container and then run it.

Image management

A docker image is a blueprint for a container. A container is the thing that actually runs. To see what images you have taking up your disk space, do

You can see here that I have three images. If I want to remove an image I don't need, I can do:

docker rmi hello-world
That will remove the 'hello-world' image.

How did I get that "ubuntu 16.04" image? You can do:
docker pull ubuntu:16.04
This image is very useful for compiling Steemd you need for a witness node.

Container management

A container is a running instance that is created from an image. Let's say we want to run an instance of ubuntu 16.04 . We can do:

docker run --name myubuntu -it ubuntu:16.04 /bin/bash

Ok, I used "--name" to give my container a name. Otherwise docker will give it some funny name. "-it" means to make it an interactive terminal. "ubuntu:16.04" is what image I wanted to run. And of course I want to run a nice bash shell.

You can see that I'm "root" in this little system, the hostname is the pithy "92dc248fb1d6" and it has it's own file system.

Let's make a tiny change to this running container:

Now it has a "steemit" file...

You can exit the shell using "exit" which will also stop the container, but it's still there.
Use
docker ps -a to show all the containers, even if they are not running at the moment (the -a).

We can run a second container from the same ubuntu:16.04 image and call it "myubuntutwo".

You'll notice that it does not have the "steemit" file we added. This is a different container!

"exit" the container and list them again:

There is our two containers!

Now if we want to get back in our first container, we do:

docker restart myubuntu
docker attach myubuntu then hit return.

And look, our "steemit" file is back!

Let's say you want to open up a second terminal into this container without closing the first one. You can do:
docker exec -it myubuntu bash

If you have too many containers running and want to get rid of one, do:
docker rm myubuntutwo

This will delete the "myubuntutwo" container, be careful with this sort of thing.

Ok, phew, one last command for now, I promise. Supposing we really like the "myubuntu" container we made, with our change to it (the steemit file) and we want to save this as an image, or template for future containers. You can run this command:
docker commit myubuntu neoxian/myubuntu

We have taken the "myubuntu" container and created the image "neoxian/myubuntu". The new image shows up when you run docker images.

You now have Docker Kung-fu!

Clearly this just scratches the surface of Docker, there is way more to it, and I'm still just a noob myself, but this guide should help get you started.

Happy Dockering!

Sort:  

Thanks for the info! Curious, if you are already running an Ubuntu machine, are there any benefits to running everything through Docker?

Two advantages:

  • Security: the container is a sandbox, so if it get hacked, the damage is limited to the container and not the whole system.
  • Repeatability: with a container, you can have a known system that has everything you need for your application and little to no extra cruft, so you can compile, run and debug your apps in a more consistant way. You don't have to worry about hundreds of different types of linux distributions or windows setups that have become 'unique' over time.

Cool, thanks!

Good to know dockering

Coin Marketplace

STEEM 0.09
TRX 0.31
JST 0.031
BTC 104655.48
ETH 3723.17
USDT 1.00
SBD 0.58