Language: EN

usar-docker-en-raspberry-pi

How to Use Docker on a Raspberry Pi

Docker is a platform that allows developers to create, deploy, and run applications in containers. It has revolutionized the way we develop, deploy, and run applications by providing a lightweight and portable container platform.

Containers are isolated environments that package an application and all its dependencies, ensuring they run consistently across different environments. They are like “mini virtual environments.”

Docker makes managing these containers easy with simple commands and configuration files, such as Dockerfiles and docker-compose.yml.

Some of the benefits of using Docker are:

  • Portability: Docker containers can run in any compatible environment, from a local computer to a cloud server.
  • Consistency: By containing all necessary dependencies, Docker ensures the application runs the same way anywhere.
  • Scalability: Docker allows for easy deployment and management of applications at scale.

Installing Docker on Raspberry Pi

To start using Docker on our Raspberry Pi, first, we update the package list and the system to ensure everything is up to date. We run the following commands:

sudo apt update
sudo apt upgrade

Next, we install Docker using the following command:

sudo apt install docker.io

This command will download and install the latest version of Docker for our Raspberry Pi. Finally, we verify that Docker has been installed correctly by running:

docker --version

We should see the installed version of Docker on our Raspberry Pi.

Basic Usage of Docker

With Docker installed, you can start creating and managing containers. Next, we will look at some basic commands and practical examples.

Running a Container

To run a container, use the docker run command. For example, to run a container of the hello-world image, which checks that Docker is working correctly, use:

docker run hello-world

Downloading Images

Docker uses images to create containers. You can search for and install images from Docker Hub (the official Docker image registry). For example, to download an nginx image, use:

docker pull nginx

Running an Nginx Container

To run a container based on the nginx image and map port 80 of the container to port 8080 of your Raspberry Pi, use:

docker run -d -p 8080:80 nginx

Now, if you open a browser and navigate to http://<YOUR_RASPBERRY_IP>:8080, you should see the Nginx welcome page.

Viewing Active Containers

To list the running containers, use:

docker ps

Stopping and Removing Containers

To stop a container, use the docker stop command followed by the container ID or name:

docker stop <container_id>

To remove a stopped container, use:

docker rm <container_id>

Creating Our Own Images

Docker is ideal for setting up consistent development environments. Suppose you need to develop a Python application. You can use a Dockerfile to create a custom image that includes Python and your dependencies.

First, in a new directory, create a file called my-python-app with the following content:

FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "app.py"]

Then, build the image using the command:

docker build -t my-python-app .

Finally, run the image as a container with the following command:

docker run -d my-python-app

Deploying a Web Application

If you are working on a web project, you can use Docker Compose to define and run multiple containers. Create a docker-compose.yml file like the following:

version: '3'
services:
  web:
    image: nginx
    ports:
      - "8080:80"
  db:
    image: postgres
    environment:
      POSTGRES_DB: mydatabase
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password

Run the containers with:

docker-compose up

This will start an Nginx container and a PostgreSQL container, configured to work together.