services:
nginx:
image: nginx:latest
container_name: nginx_container
ports:
- "80:80" # Exposing port 80 on the host to port 80 on the container
volumes:
- ./nginx/html:/usr/share/nginx/html # Mounting local HTML directory to Nginx container
restart: always7 Week 4/5 (23/30-Out-2025)
7.1 Portainer Introductory Exercises
7.1.1 Getting Started with Portainer
- Question: What is Portainer, and why would you use it?
- Solution: Portainer is a management tool that simplifies the use of Docker containers. It provides a graphical interface to manage Docker environments, making it easier to deploy, monitor, and manage containers.
- Question: How do you install Portainer on a Docker-enabled system?
Solution: Use the following command:
docker volume create portainer_data docker run -d -p 8000:8000 -p 9443:9443 --name portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ce:latest
- Question: How can you access the Portainer web interface after installation?
- Solution: Open a web browser and navigate to
https://<server-ip>:9443.
- Solution: Open a web browser and navigate to
7.1.2 Basic Container Management
- Question: How do you deploy a new container using Portainer?
- Solution: Go to “Containers” > “Add Container,” specify the container name, image, ports, and other settings, then click “Deploy the container.”
- Question: How do you start, stop, and restart a container in Portainer?
- Solution: Click on the container name, and use the “Start,” “Stop,” or “Restart” buttons.
- Question: How can you view the logs of a container in Portainer?
- Solution: Select the container, then click on the “Logs” tab.
- Question: How do you execute a command inside a running container using Portainer?
- Solution: Go to the container and click on “Console,” then choose “bash” or “sh” and start the session.
- Question: What are the steps to remove a container in Portainer?
- Solution: Select the container, stop it if running, and click the “Remove” button.
7.1.3 Image Management
- Question: How do you pull a new image from Docker Hub using Portainer?
- Solution: Go to “Images” > “Pull Image,” enter the name of the image, and click “Pull.”
- Question: How do you see all the Docker images stored on your system in Portainer?
- Solution: Go to “Images” to view the list of images.
- Question: How can you remove unused images in Portainer?
- Solution: Select the image and click “Remove.”
7.1.4 Network Management
- Question: How do you create a new network in Portainer?
- Solution: Go to “Networks” > “Add Network,” specify the network details, and click “Create Network.”
- Question: How can you attach a running container to a different network in Portainer?
- Solution: Go to the container’s details, click on “Networks,” and add the container to a new network.
- Question: How do you view details about a specific network in Portainer?
- Solution: Go to “Networks” and click on the network name to view its details.
7.1.5 Volume Management
- Question: How do you create a new volume in Portainer?
- Solution: Go to “Volumes” > “Add Volume,” specify the volume name, and click “Create Volume.”
- Question: How can you mount a volume to a container in Portainer?
- Solution: While deploying or editing a container, go to the “Volumes” section and add the volume.
- Question: How do you remove a volume that is no longer in use?
- Solution: Go to “Volumes,” select the volume, and click “Remove.”
7.1.6 Stack Management
- Question: What is a stack in Portainer, and how do you deploy one?
- Solution: A stack is a group of interrelated services defined by a Docker Compose file. Go to “Stacks” > “Add Stack,” upload or paste your Compose file, and click “Deploy the stack.”
- Question: How can you view the status of a stack in Portainer?
- Solution: Go to “Stacks” and click on the stack name to see the details and status of each service.
- Question: How do you remove a stack in Portainer?
- Solution: Select the stack, and click the “Remove” button.
7.1.6.1 Add a Stack (one container)
To add an Nginx container using Docker Compose in Portainer, you can follow these steps:
20.1. Access Portainer: - Log into your Portainer dashboard.
20.2. Navigate to Stacks: - Go to the menu on the left and click on Stacks. - Click on + Add Stack.
20.3. Name the Stack: - Give your stack a name, e.g., nginx-webserver.
20.4. Add Docker Compose Configuration: - In the Web editor section, paste the following Docker Compose YAML configuration:
- Explanation:
image: Defines the Nginx image that will be pulled from Docker Hub.ports: Maps port 80 on the host to port 80 on the container, making Nginx accessible viahttp://your-server-ip:80.volumes: Maps local directories to the Nginx container for HTML files and custom Nginx configuration.restart: Ensures that the container always restarts if it stops or fails.
20.5. Deploy the Stack: - Click on Deploy the stack. - Portainer will pull the Nginx image and start the container based on the configuration.
20.6. Verify: - After deployment, navigate to Containers or Stacks to see your running Nginx container. - Open your browser and visit http://your-server-ip to verify that Nginx is running.
You can adjust the Docker Compose configuration as needed (e.g., change port mappings, add more volumes, or set environment variables).
7.1.6.2 Add a Stack (one container)
To add a MySQL container using Docker Compose in Portainer, follow these steps:
20.7. Access Portainer: - Log into your Portainer dashboard.
20.8. Navigate to Stacks: - Go to the menu on the left and click on Stacks. - Click on + Add Stack.
20.9. Name the Stack: - Give your stack a name, e.g., mysql-database.
20.10. Add Docker Compose Configuration: - In the Web editor section, paste the following Docker Compose YAML configuration:
services:
mysql:
image: mysql:latest
container_name: mysql_container
environment:
- MYSQL_ROOT_PASSWORD=rootpassword # Set the root password
- MYSQL_DATABASE=mydatabase # Optional: create a database
- MYSQL_USER=myuser # Optional: create a user
- MYSQL_PASSWORD=mypassword # Optional: set password for the user
ports:
- "3306:3306" # Expose MySQL port 3306
volumes:
- mysql_data:/var/lib/mysql # Persist MySQL data
restart: always
volumes:
mysql_data:
driver: local- Explanation:
image: Defines the MySQL image that will be pulled from Docker Hub.environment: Set environment variables to configure MySQL (e.g., root password, database name, user, password).ports: Maps port 3306 on the host to port 3306 on the container, making MySQL accessible.volumes: Ensures data is persisted even if the container is stopped or removed.restart: Automatically restarts the container if it stops or fails.
20.11. Deploy the Stack: - Click on Deploy the stack. - Portainer will pull the MySQL image and start the container based on the configuration.
20.12. Verify: - After deployment, navigate to Containers or Stacks to see your running MySQL container. - You can connect to the MySQL server using a MySQL client or management tool like mysql CLI, MySQL Workbench, or another application by specifying your-server-ip:3306.
Adjust the configuration (e.g., change environment variables, port mappings, or volume settings) as needed.
7.1.6.3 Add a Stack (with two containers)
To add both a MySQL container and an Nginx container in the same stack and network using Portainer, follow these steps:
20.13. Access Portainer: - Log into your Portainer dashboard.
20.14. Navigate to Stacks: - Go to the menu on the left and click on Stacks. - Click on + Add Stack.
20.15. Name the Stack: - Give your stack a name, e.g., nginx-mysql-stack.
20.16. Add Docker Compose Configuration: - In the Web editor section, paste the following Docker Compose YAML configuration:
services:
nginx:
image: nginx:latest
container_name: nginx_container
ports:
- "80:80" # Expose Nginx on port 80
volumes:
- ./nginx/html:/usr/share/nginx/html # Mount HTML files
networks:
- web_network
restart: always
mysql:
image: mysql:latest
container_name: mysql_container
environment:
- MYSQL_ROOT_PASSWORD=rootpassword # Set the root password
- MYSQL_DATABASE=mydatabase # Optional: create a database
- MYSQL_USER=myuser # Optional: create a user
- MYSQL_PASSWORD=mypassword # Optional: set password for the user
ports:
- "3306:3306" # Expose MySQL on port 3306
volumes:
- mysql_data:/var/lib/mysql # Persist MySQL data
networks:
- web_network
restart: always
volumes:
mysql_data:
driver: local
networks:
web_network:
driver: bridge- Explanation:
services: Defines both the Nginx and MySQL containers.networks: Both containers are connected to the sameweb_network, allowing them to communicate internally.volumes: Persist data for MySQL and mount HTML/config files for Nginx.restart: Ensures both containers restart automatically if they stop or fail.
20.17. Deploy the Stack: - Click on Deploy the stack. - Portainer will pull the necessary images and start the containers based on the configuration.
20.18. Verify: - After deployment, navigate to Containers or Stacks to see your running Nginx and MySQL containers. - Nginx will be accessible via http://your-server-ip:80, and MySQL can be accessed internally within the same network or externally via your-server-ip:3306. - You can connect to the MySQL server using a MySQL client or management tool like mysql CLI, MySQL Workbench, or another application by specifying your-server-ip:3306.
You can modify the configuration, such as changing environment variables, volume paths, or adding other services to the same network.
7.1.6.4 Installing MySQL Workbench
To install MySQL Workbench, follow the appropriate steps for your operating system:
7.1.7 1. Windows:
20.19. Download the Installer: - Go to the MySQL Workbench download page. - Select your operating system and click on the Download button. - You may be asked to log in or sign up for an Oracle account, but you can click on No thanks, just start my download to skip this.
20.20. Run the Installer: - Open the downloaded .msi file and run the installer. - Follow the setup wizard steps, choosing the Complete installation type to install all necessary components.
20.21. Finish the Installation: - Once the installation is complete, click Finish. - You can now open MySQL Workbench from the Start Menu.
7.1.8 2. macOS:
20.22. Download the Installer: - Go to the MySQL Workbench download page. - Select macOS and download the .dmg file.
20.23. Install the Application: - Open the downloaded .dmg file. - Drag the MySQL Workbench icon to the Applications folder.
20.24. Open MySQL Workbench: - You can now open MySQL Workbench from the Applications folder or through Spotlight Search.
7.1.9 3. Linux (Ubuntu/Debian):
20.25. Open Terminal and Update Package Lists:
sudo apt update20.26. Install MySQL Workbench:
sudo apt install mysql-workbench20.27. Launch MySQL Workbench: - After installation, you can open it by searching for MySQL Workbench in your application menu or by running:
mysql-workbench7.1.10 4. Linux (CentOS/RHEL/Fedora):
20.28. Enable the MySQL Repository (if not already installed):
sudo yum install https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm20.29. Install MySQL Workbench:
sudo yum install mysql-workbench20.30. Launch MySQL Workbench: - Open MySQL Workbench from the application menu or by running:
mysql-workbenchAfter installation, you can use MySQL Workbench to connect to your MySQL server and manage your databases.
7.1.11 Managing Users and Teams
- Question: How do you add a new user in Portainer?
- Solution: Go to “Users” > “Add User,” enter the username and password, and click “Create user.”
- Question: How can you create a new team in Portainer?
- Solution: Go to “Teams” > “Add Team,” enter the team name, and assign users to the team.
- Question: How do you assign a team to a specific environment?
- Solution: Go to “Environments,” select the environment, and assign a team under “Access Control.”
7.1.12 Environment Management
- Question: How do you connect Portainer to a remote Docker environment?
- Solution: Go to “Environments” > “Add Environment,” select “Docker” and “Agent,” and provide the necessary details.
- Question: How can you configure multiple Docker environments in Portainer?
- Solution: Go to “Environments” and add as many environments as needed, configuring each individually.
- Question: What steps are involved in enabling TLS for a Docker environment in Portainer?
- Solution: While adding or editing an environment, check the “TLS” option and upload the required certificates.
7.1.13 Resource Management
- Question: How do you limit the CPU and memory usage for a container in Portainer?
- Solution: While deploying or editing a container, specify the CPU and memory limits in the “Resource control” section.
- Question: How can you configure environment variables for a container?
- Solution: During container setup, go to the “Environment variables” section and add the key-value pairs.
- Question: How do you set up port mappings for a container in Portainer?
- Solution: In the “Network” section during container deployment, specify the host and container ports.
7.1.14 Monitoring and Maintenance
- Question: How can you view the resource usage (CPU, memory, etc.) of a container in Portainer?
- Solution: Select the container and go to the “Stats” tab.
- Question: How do you back up Portainer data?
- Solution: Backup the
portainer_datavolume usingdocker runcommands or manually copy the data to another location.
- Solution: Backup the
- Question: How can you update Portainer to a newer version?
- Solution: Pull the new image, stop and remove the current Portainer container, and re-deploy with the new version.
7.1.15 Security and Access Control
- Question: How do you enable authentication in Portainer?
- Solution: Go to “Settings,” enable authentication, and configure users under “Users.”
- Question: How can you create and manage roles for users in Portainer?
- Solution: Use the “Roles” section to define specific access permissions for users.
- Question: How do you set up OAuth or LDAP authentication in Portainer?
- Solution: Go to “Settings” > “Authentication,” and configure the external authentication provider.
7.1.16 Advanced Topics
- Question: How do you deploy a service with replicas using Portainer?
- Solution: Go to “Stacks” and use a Docker Compose file to define the service with the
replicasparameter.
- Solution: Go to “Stacks” and use a Docker Compose file to define the service with the
- Question: How can you set up automatic updates for containers using Watchtower in Portainer?
- Solution: Deploy the Watchtower container through Portainer, specifying the
--cleanupand--intervalflags.
- Solution: Deploy the Watchtower container through Portainer, specifying the
- Question: How do you deploy a custom Docker image from a private registry in Portainer?
- Solution: Add the registry under “Registries,” then specify the image during container deployment.
- Question: What is the process to configure Docker Swarm services in Portainer?
- Solution: Go to “Stacks,” and use Docker Compose files to define and deploy Swarm services.
- Question: How do you use Portainer to manage Kubernetes clusters?
- Solution: Connect to a Kubernetes environment in “Environments” and use the “Applications” feature to manage pods, services, and deployments.
These exercises cover fundamental and advanced aspects of using Portainer, helping professionals to understand both basic container management and more complex setups like multi-environment management, stacks, and security.