Docker quick start guide

I have avoided the whole Docker thing for some time now, mainly cause I didn’t understand why the world needed another kernel level virtualization product. I have been using OpenVZ and FreeBSD Jails for years, and just never saw the need to add another product to the mix.

But after spending some time with it, little did I realize that Docker is nothing more than a wrapper script essentially for using LXC on Linux. So if your already familiar with OpenVZ, LXC, FreeBSD Jails, or Solaris Zones, then Docker it actually really simple to pick up.

I am not going to go into the details of what container virtualization is and the pros and cons since there are literally hundreds of good sites online that dive into that.

Instead so you can see it in action, since many people learn what questions to ask by actually doing it, I’ll post a basic example here so you can get started quickly on CentOS 7 running Docker.

Our end goal for this quick start guide will simply illustrate how you can use Docker on a single CentOS 7 server to spin up multiple containers for a test environment. The Docker host can be a VM, cloud server, vagrant image, or a dedicated server.

So to get started, first configure the basics on your CentOS 7 server, such as setting the hostname, updating the system, installing NTP and sysstat, and rebooting so your running off the latest kernel:

[[email protected] ~]# hostnamectl set-hostname docker01.example.com
[[email protected] ~]# yum -y update
[[email protected] ~]# yum install ntp sysstat
[[email protected] ~]# chkconfig ntpd on
[[email protected] ~]# service ntpd start
[[email protected] ~]# reboot

Now setup the official Docker repo on your server:

[[email protected] ~]# vim /etc/yum.repos.d/docker.repo
[dockerrepo]
name=Docker Repository
baseurl=https://yum.dockerproject.org/repo/main/centos/$releasever/
enable=1
gpgcheck=1
gpgkey=https://yum.dockerproject.org/gpg

Then install Docker:

[[email protected] ~]# yum install docker-engine
[[email protected] ~]# systemctl enable docker
[[email protected] ~]# systemctl start docker

Since there is really no reason to run Docker as root, we are going to setup a unprivileged user to run Docker:

[[email protected] ~]# useradd dockeradmin
[[email protected] ~]# usermod dockeradmin -G docker

Now log in as user dockeradmin, and confirm Docker is working:

[[email protected] ~]# su - dockeradmin
[dockeradmin@docker01 ~]# docker ps
[[email protected] ~]# docker images

If no errors are returned, then Docker is working! So lets pull down some OS images I use often. This is not a requirement, but it just makes deploying new containers a bit faster:

[[email protected] ~]# docker pull centos:centos6
[[email protected] ~]# docker pull centos:centos7
[[email protected] ~]# docker pull ubuntu:precise
[[email protected] ~]# docker pull ubuntu:trusty
[[email protected] ~]# docker pull ubuntu:xenial

Lets spin up our first container:

[[email protected] ~]# docker run --name centos6-test01 -id --restart unless-stopped centos:centos6 /bin/bash

Once its running, you can get to a console by running:

[d[email protected] ~]# docker exec -it centos6-test01 /bin/bash

From there, you will be able to configure and use the container as you see fit!

Some other basic commands you will need to know for how to interact with Docker are below.

To see your running containers:

[[email protected] ~]# docker ps

To see both your running and stopped containers:

[[email protected] ~]# docker ps -a

To stop a running container:

[[email protected] ~]# docker stop your_container_name

To start up a stopped container:

[[email protected] ~]# docker start your_container_name

To create an image of a running container that you can use to deploy new containers from:

[[email protected] ~]# docker commit -m "your_commit_message" -a dockeradmin your_container_name dockeradmin/your_new_image_name:v1

To see what images you have setup:

[[email protected] ~]# docker images

To remove an image:

[[email protected] ~]# docker rmi your_image_id

If you would like you view the stats of your containers:

[[email protected] ~]# docker stats

To delete a container:

[[email protected] ~]# docker stop your_container_name
[[email protected] ~]# docker rm your_container_name

If you want to stop all running containers and delete them from the system so you can start fresh:

[[email protected] ~]# docker stop `docker ps -a -q`
[[email protected] ~]# docker rm `docker ps -a -q`

This should get you started into the world of Docker. Once you get a feel for it, treating the containers as their own “vm’s”, you will start to recognize some of the benefits of Docker.

From there, you will be able to start diving deeper and instead of using containers as a “vm”, you can instead start thinking about how to break up your application into individual containers to give you a finer degree of control and portability over your application.