Setting up Docker on your Raspberry Pi

What is Docker? This is a tool for creating and running containers, which are environments similar to virtual machines. Docker images provide several advantages such as the ability to clone a developer environment and deploy that as a docker image (package) that comes with all required dependencies and incredibly shortens troubleshooting / deployment time for applications.

Spinning up Docker on a Raspberry Pi is a great project due to the operating system being Linux and it plays well with Raspberry Pi boards. This is a fantastic tutorial for setting up your Raspberry Pi if you just acquired one: How to set up your first Raspberry Pi | ZDNET

Setting up an SSH server on your Raspberry Pi and preparing for first-use:

In conjunction with the previous article, we want to setup SSH (Secure Shell) for remote access via a terminal too. You can do this at startup following these instructions or you can proceed below and set it up that way too: https://roboticsbackend.com/enable-ssh-on-raspberry-pi-raspbian/

Run the following commands in succession:

1) sudo apt-get install openssh-server -y

2) service ssh start

On the host you’re remoting from: ssh <username>@<raspberryIPaddress>, provide your password and you can begin working with the Raspberry Pi from a distance.

Once both of these are taken care of you should now be able to access your Raspberry Pi in a couple ways, and have the OS setup. Lastly, update and upgrade your machine: sudo apt-get updates && sudo apt-get upgrade -y and then reboot the Raspberry Pi: sudo reboot

Installing Docker and creating the required user profile to allow the current user to use the Docker group:

Pulling down and installing via the installation script: curl -sSL https://get.docker.com | sh

Allowing the current user to use Docker: sudo usermod -aG docker $USER

You will need to logout and log back in through your SSH session in order to apply the changes to your user’s groups. Once this is complete you will have the privileges to run any Docker command without sudo (administrative permissions and running id will reflect the docker group too).

Getting started with Docker commands:

Using the command docker ps we can list the docker containers that are currently running on our system. This should be none, but it provides a status of container IDs, names, images, creation information, and resource utilization.

We can see the usage statistics of the Docker containers by issuing a docker stats command. It should be empty like this without anything to track.

You can display the current version of Docker using: docker version and we can get a listing of all the Docker commands using docker help.

Downloading images, running them, and searching through the Docker repository:

Once Docker is installed we can download a test container to get an example of a container in action. It will notice it is not installed, download it (pull), and then reun the container specified. This is done by issuing a docker run command with the context of the container name hello world.

We can download a new image with docker pull <image name> and then run it similarly to previous with docker run <image name>. We can also find Docker images that we may want to download via the Docker repository intuitively with docker search <keyword>.

You can find an accurate easy on the eyes listing for all the options here: Docker Hub Container Image Library | App Containerization be aware verified images and official images from Docker themselves are preferred, there is user risk with random images.

You can see all the images that are present on your local computer by issuing the following: docker images command

Creating an Ubuntu Docker container we can interact with:

We can create a container running Ubuntu and use the switch -it for interactive, to run commands inside the container in a /bin/bash session:

docker run -it ubuntu /bin/bash

We should now be running inside of an Ubuntu system, you can see this by running more /etc/os-release but if you do lscpu you can see we’re running on the Raspberry Pi there and running uname -a also confirms we’re running on the kernel of the Pi. You will also notice we’re running as the ROOT user of the container.

We can also create containers, name them accordingly and keep them running in the background by utilizing the -d switch and —name switch. Changing the command that runs from bash typically to one that blocks - for example tail -f /dev/null:

To exit the container you can do CTRL+D or type exit and you’ll be back in your Raspberry Pi.

Understanding the “temporary” nature of containers (container history):

Something to pay attention to when working in containers- when you exit the container or the job is done it will return back to its original state.

You CAN ‘recover’ the work that was done in that container after you exit by running the following command: docker container ls -a and see a history of containers that were previously spun up.

Each of these will have container IDs attached to them and you can start those with the following: docker start -i <ID> and it takes the same state as it previously was.

Wiping out old images:

You can delete all the previous containers you’ve had, removing the history, by running the following command: docker container prune

You can also stop containers individually by running the following: docker stop <container id>

Application deployment with Docker files to create a Simple Node(js) Webserver:

Docker files define what goes on in the environment inside your container and allows you to build your own applications into your containers. We can expose port 80 and run a webserver with node.js inside of the docker container, hosting a very simple web server by creating the following file:

Then we can create a file called index.js which will have a simple Node.JS web page that reports the date.

We will build a simple node webserver in the current working directory from the ‘Dockerfile’ we have present:

Now we can start the Simple Node Webserver with the current working directory hosting our index.js file and we can serve up a webpage by doing:

Any requests made to the server will resolve in the top window hosting this server. So we can visit to the webpage in a browser too (instead of just using curl) and see what this minimalistic web application looks like.

Because we’re using containers you could increase the port number specified from 4000 to 4001 on port 80 and continue to host more via containerization. This is a great way to experience getting your Pi setup, deploying Docker and learning how to use it, and deploying a first simple web application that could be duplicative.

Previous
Previous

Wireless Penetration Testing: WPA2-PSK

Next
Next

Golang for Offensive Security