A Comprehensive Guide to Running Projects on a Local Network with Portainer
Portainer is an excellent web-based management tool that simplifies running Docker projects on a local machine or home network. It provides a graphical user interface (GUI) that allows you to manage containers, images, volumes, and networks without needing to use the command line for every task. This makes it perfect for beginners and home lab enthusiasts.
1. Installation and Initial Setup
To get started, you’ll need Docker installed on your machine. Portainer itself runs as a Docker container, so the installation is straightforward.
First, create a Docker volume to ensure Portainer’s data is persistent. This means if you stop or remove the container, your configurations and user accounts will be saved.
docker volume create portainer_data
Next, run the Portainer container. The command below uses the portainer/portainer-ce
image, which is the free Community Edition.
docker run -d -p 9443:9443 --name portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ce
Command Breakdown:
-d
: Runs the container in detached mode (in the background).-p 9443:9443
: Maps port 9443 on your host to port 9443 inside the container. This is the default port for Portainer’s secure web UI.--name portainer
: Gives the container a recognizable name.--restart=always
: Ensures Portainer automatically starts if your host machine reboots.-v /var/run/docker.sock:/var/run/docker.sock
: This is a critical step. It mounts the Docker socket from the host into the container, allowing Portainer to communicate with your Docker engine and manage other containers.-v portainer_data:/data
: Mounts the persistent volume you created to the container’s data directory.
After running this command, open your web browser and navigate to https://localhost:9443
or https://<your-machine-ip>:9443
. You’ll be prompted to create an administrator user and password to log in for the first time.
2. Core Concepts: Containers, Images, and Stacks
Once logged in, you’ll see a dashboard with an overview of your Docker environment. The main tools you’ll use are:
- Containers: These are the running instances of your applications. You can view, start, stop, restart, and inspect logs for each container.
- Images: These are the read-only templates used to create containers. You can pull images from a registry (like Docker Hub) and delete unused ones to free up space.
- Volumes: This is where your application’s data is stored persistently. You can create and manage volumes separately from your containers, ensuring your data is safe even if a container is removed.
- Stacks: This is the most powerful feature for managing projects. A stack is Portainer’s term for a multi-container application defined by a Docker Compose file. Instead of managing each container individually, you can deploy an entire project (e.g., a web server and a database) from a single YAML file.
3. Deploying Your First Project with a Stack
To deploy a project using a stack:
- In the Portainer sidebar, go to Stacks and click “Add stack”.
- Choose a name for your stack.
- In the Web editor, paste the contents of your
docker-compose.yml
file. - Click “Deploy the stack”. Portainer will read the file, pull the necessary images, and create all the containers, networks, and volumes defined in the file.
4. How to Deploy New Code
To update a running project with new code, you need to update the Docker image that your stack is using. This process is often referred to as “pulling and redeploying.”
- Build and Push the New Image: On your local development machine, you’ll build a new version of your Docker image with the updated code. Then, you’ll push it to your private Docker registry that we discussed in previous conversations. Make sure to use the same tag you specified in your
docker-compose.yml
file.# Example commands docker build -t 192.168.0.14:5000/my-app:latest . docker push 192.168.0.14:5000/my-app:latest
- Trigger a Redeployment in Portainer:
- Navigate to Stacks in the Portainer UI.
- Click on the name of the stack you want to update.
- On the stack details page, click the “Pull and redeploy” button.
Portainer will then connect to your registry, pull the new image, and seamlessly restart the container with the updated code.
5. Accessing Your Running Servers from the Network
To access a service (like a web server) running in a container from another machine on your network, you must configure port mapping in your docker-compose.yml
file. This is a crucial step that tells Docker to forward traffic from a port on your host machine to a port inside the container.
Example docker-compose.yml
with Port Mapping:
version: '3'
services:
web:
image: nginx
ports:
- "8080:80" # <--- This is the port mapping
How to access it:
- The first number (
8080
) is the port on your host machine. This is the port you will use to access the service from other devices. - The second number (
80
) is the port inside the container.
With this configuration, any computer on your network can access the Nginx web server by navigating to your host machine’s IP address followed by the mapped port.
- From your local network, open a web browser and go to:
http://<your-machine-ip>:8080