Debian Jessie with Docker from scratch

- 5 mins

Introduction

In this tutorial, we’re going to configure and install Docker on a fresh Debian 8 Jessie VPS (cloud server) instance from DigitalOcean.

Step 1 - Create a new droplet

First, we need to create a new droplet with Debian Jessie. In your panel, create a new droplet and select the Debian 8.0 x64 image.

Select the Debian 8 x64 image

I recommend you to add your SSH key.

Step 2 - Basic configuration

In this part we will setup a basic configuration of our new server. You can now connect to your server with SSH to start the configuration.

[local]$ ssh [email protected]_ip

Note: in this part, all the commands are executed with the root user.

Locales and timezones

We can start by configuring the locales. Locales are used by glibc and other locale-aware programs or libraries for rendering text, correctly displaying regional monetary values, time and date formats, alphabetic idiosyncrasies, and other locale-specific standards.

Run the next command and select the locales that match your uses (languages or nationality for example). Prefer to select an UTF-8 version of your locales.

dpkg-reconfigure locales

For examples, I selected the locales en_US.UTF-8.

The second things that we need to configure is our timezone. Again, choose the timezone that match the most your use.

dpkg-reconfigure tzdata

Debian mirror

In this part we will setup the Debian mirror. The mirror is used to install or update software and Debian with the package manager (apt-get or aptitude on Debian).

The installation of Docker requires that we activate the jessie-backports repository. Backports are packages taken from the next Debian release, i.e. that are less outdated than the stable package.

Edit the file /etc/apt/sources.list and replace its content with these lines:

deb http://httpredir.debian.org/debian jessie main
deb-src http://httpredir.debian.org/debian jessie main

deb http://security.debian.org/ jessie/updates main
deb-src http://security.debian.org/ jessie/updates main

deb http://httpredir.debian.org/debian jessie-updates main
deb-src http://httpredir.debian.org/debian jessie-updates main

deb http://httpredir.debian.org/debian jessie-backports main
deb-src http://httpredir.debian.org/debian jessie-backports main

Note: you can choose any mirrors from this list. I recommend you to use the httpredir.debian.org mirror because it automatically choose the best mirror for you in terms of location, speed and up-to-date.

Now you can update your operating system.

apt-get update && apt-get upgrade -y

Create a new user account

It’s not recommended to manipulate Debian with the root user. So, in this part we will create a new user account. But first, we need to install sudo. It will be used get the root permission, from our basic user account, in order to execute some commands.

apt-get install sudo

Now we can create the new user account. Replace the username sammy by what you want.

adduser sammy

Then, we add sammy to the group sudo in order to have the right to use the command sudo.

adduser sammy sudo

Now you can disconnect from your VPS and connect again with your new user account. For example, in my case:

[local]$ ssh [email protected]_ip

(Optional) Secure your SSH server

This part is not mandatory, but it’s always a good idea to secure your server. I will only show you how to configure the server and I assume that during the creation of your Droplet, you selected an SSH key. If not, you can follow this tutorial.

First, we need to copy our SSH key from the root directory to the new user account directory. And then, we can give the own right to the new user account.

$ cp -r .ssh/ /home/sammy
$ chown -R sammy:sammy /home/sammy/.ssh/

Now, we can edit the server configuration. Edit the file /etc/ssh/sshd_config and change these lines:

PermitRootLogin no
PubkeyAuthentication yes
PasswordAuthentication no

Let me give you some explications of each line:

Restart the SSH server to activate the new configuration.

$ sudo systemctl restart ssh.service

Step 3 - Install and configure Docker

To install Docker, we can use the generic script provided to us by Docker.

$ curl -sSL https://get.docker.com/ | sh

Once the installation is completed, we can enable the service with systemd. This allows the service to start every time you restart your server.

$ sudo systemctl enable docker.service

Then, we can launch the service so we can use it right now.

$ sudo systemctl start docker.service

You can check the service status with the following command:

$ systemctl status docker.service

Finally, you can check your installation:

$ sudo docker run --rm hello-world

Step 4 - Best practice

Docker highly recommends that you add your user account to the docker group. This will allow you to execute all the docker commands without sudo.

$ sudo usermod -aG docker sammy

Moreover, it’s a good idea to have a /home directory where you can store all your Dockerfile (especially if you mount a folder to a Docker’s container). So, first create a directory:

$ mkdir /home/docker

We can set the access right and the permission. The idea is that you can manipulate this folder with the user sammy.

$ chown sammy:docker -R /home/docker
$ chmod 755 -R /home/docker

Now, you should be able to execute the test without asking for the root access.

$ docker run --rm hello-world

Conclusion

Et voilà, you have a fully operational VPS with Docker.

Thibault Deutsch

Thibault Deutsch

I'm a french student who studies CS.

rss facebook twitter github youtube mail spotify instagram linkedin google pinterest medium vimeo