How to Manage Docker Containers with Portainer

4942 VIEWS

·

Portainer is an open-source container management tool for  Docker, Docker swarm, Kubernetes, and Azure Container Instances. It provides a solution to manage containerized apps using GUI elements. With Portainer, you can manage and deploy your containers without having to write any commands, contrary to when using a terminal to do the same. This means you don’t need prior knowledge of various commands on how to manage Docker containers with Portainer.

This guide will teach you how to set up and manage Docker Containers with Portainer. You will learn how to streamline container management using Portainer using Docker.

To comfortably follow along with this article, ensure you have installed Docker Engine and that it is up and running on your local computer.

Creating Portainer using Docker Engine

Portainer runs as a standalone container. To start administrating your environment, install Portainer as a container using Docker. In the next steps, you will learn how to install and activate your Portainer.

There are many ways to spin a Docker container, including running the Docker command directly in your terminal. However, Docker Compose stands as the easiest choice. You must write a few docker commands that automatically start your container with a single command. In your computer, create a project directly and add a docker-compose.yml file. Processed and write the following configurations that will create Portainer with Docker:

  • First, add the version of docker-compose that you want to run:
version: "3.9"
  • Create a service to execute Portainer, add the restart policy and the container name:
services:
  portainersrv:
    container_name: portainer_ctr
    restart: always
ports:
  - 9000:9000
  • To persist the container data, configure the volume for Docker storage. Also, you need to add a docker.sock volume to map the Docker Network mapping to allow Docker container management using Portainer.
volumes:
    - /portainer_data
    - /var/run/docker.sock:/var/run/docker.sock

Here is how your docker-compose.yml file should look so far:

version: "3.9"
services:
  portainersrv:
    container_name: portainer_ctr
    restart: always
    image: portainer/portainer-ce:latest
    ports:
      - 9000:9000
    volumes:
      - /portainer_data
      - /var/run/docker.sock:/var/run/docker.sock

To execute it, run the following command:

docker compose up -d

Accessing the Docker with Portainer

The above command will build the above configurations and spin up your container. You can confirm these specifications on your Docker Engine. Then open http://localhost:9000/ to access your first up-and-running Portainer container as follows:

Proceed and create a user account for your new Portainer installation.

To initialize container management using Portainer, you need to set up an environment that will load the containerization system from your local Docker environment in which Portainer is running in.

On your environment wizard, click Get Started to initialize Portainer with Docker:

Because Docker is running locally, an overview of all the environments you introduced to Portainer using Docker will be displayed in a dashboard that loads as such:

This will display the docker-compose stacks, such as images, volumes, networks, and containers, currently available on your local Docker Engine.

Up to this point, you can visualize a GUI container management platform that uses Docker with Portainer. You should now be able to navigate around the stack that shows you how to manage Docker Containers with Portainer interface. You can do all tasks that you would instead have to use the CLI.

Portainer deployment is not limited to Docker. Check this guide and learn more ways to deploy a Portainer environment.

How to Manage Docker Containers with Portainer

Let’s now explore this tool. Assuming you want to run a PostgreSQL database container, here is how you can do that using Portainer.

Let’s create a stack to execute PostgreSQL alongside its GUI pgadmin. On your Portainer interface, navigate to Stacks and Add a new stack.

Here you can paste your docker-compose file, upload it or even load it from a publicly hosted URL such as a GitHub repository.

Go ahead and use the following docker-compose to spin PostgreSQL:

services:
 db:
   image: postgres
   container_name: postgres_ctr
   restart: always
   ports:
    - "15432:5432"
   environment:
     POSTGRES_DB: postgres
     POSTGRES_USER: admin
     POSTGRES_PASSWORD: admin
     PGDATA: /var/lib/postgresql/data
   volumes:
    - /var/lib/postgresql/data
 
 pgadmin:
   image: dpage/pgadmin4
   container_name: pgadmin_ctr
   restart: always
   ports:
    - "8080:80"
   environment:
     PGADMIN_DEFAULT_EMAIL: [email protected]
     PGADMIN_DEFAULT_PASSWORD: secret
     PGADMIN_LISTEN_PORT: 80
   links:
    - "db:pgsql-server"
   volumes:
    - /var/lib/pgadmin

Name your PostgreSQL and add this configuration to your Editor as follows:

Scroll down and hit Deploy Stack. This will build your Docker with Portainer and create your stack without running a single command.

And now you can access your container Portainer with Docker stack on http://localhost:8080

Additionally, you can make changes to this container and redeploy the stack with the new changes. Let’s say you want to add environment variables to PostgreSQL. This means your docker-compose will change as follows:

services:
 db:
   image: postgres
   container_name: postgres_ctr
   restart: always
   ports:
    - "15432:5432"
   environment:
     POSTGRES_DB: ${DB_NAME}
     POSTGRES_USER: ${DB_USER}
     POSTGRES_PASSWORD: ${DB_PASS}
     PGDATA: /var/lib/postgresql/data
   volumes:
    - /var/lib/postgresql/data
 
 pgadmin:
   image: dpage/pgadmin4
   container_name: pgadmin_ctr
   restart: always
   ports:
    - "8080:80"
   environment:
     PGADMIN_DEFAULT_EMAIL: ${PG_EMAIL}
     PGADMIN_DEFAULT_PASSWORD: ${PG_PASS}
     PGADMIN_LISTEN_PORT: 80
   links:
    - "db:pgsql-server"
   volumes:
    - /var/lib/pgadmin

Navigate to the created stack. Click Editor and update docker-compose with the above changes. Now here you are introducing Docker environment variables. If you have them ready to can use Portainer and load them directly from an .env file and create them as follows:

Navigate to Add an environment variable tab and add them as follows:

Hit Update Stack. Proceed and update the stack. You can as well choose Re-pull the image and redeploy. This will update the container with new changes as such.

Portainer also allows you to run basic templates that are available for deployments. For example, to spin a container using NGINX, you can select an available template as follows:

Navigate to Template sections and select the NGINX webserver:

Provide the configuration name and deploy the container. This will deploy NGINX and expose it on http://localhost:51505/

Conclusion

You have learned how to run containers with Portainer using Docker. This guide has explored how to manage Docker containers with Portainer GUI. You can further explore this tool and learn how to build images, set up volumes, create network bridges, etc.


Joseph is fluent in Fullstack web development and has a lot of passion for DevOps engineering. He loves to write code and document them in blogs to share ideas.


Discussion

Leave a Comment

Your email address will not be published. Required fields are marked *

Menu
Skip to toolbar