How to use the command 'podman-compose' (with examples)
This article provides examples of how to use the ‘podman-compose’ command. ‘podman-compose’ is a command-line tool that allows users to run and manage container definitions using the Compose Specification. It is an alternative to the ‘docker-compose’ command and is commonly used with the Podman container engine.
Use case 1: List all running containers
Code:
podman-compose ps
Motivation: This use case is useful when you want to quickly check which containers are currently running.
Explanation: The ‘ps’ command is used to list the running containers managed by podman-compose. It provides information such as the container ID, name, and status.
Example output:
Name Command State Ports
------------------------------------
webapp_1 /bin/sh -c npm start Up
database_1 /docker-entrypoint.sh Up
Use case 2: Create and start all containers in the background using a local ‘docker-compose.yml’
Code:
podman-compose up -d
Motivation: This use case is helpful when you want to launch all the containers defined in the local ‘docker-compose.yml’ file and run them in the background. This allows you to start the containers without blocking the console.
Explanation: The ‘up’ command is used to create and start containers defined in the ‘docker-compose.yml’ file. The ‘-d’ flag is used to start the containers in the background (detached mode).
Example output:
Creating webapp_1 ... done
Creating database_1 ... done
Use case 3: Start all containers, building if needed
Code:
podman-compose up --build
Motivation: This use case is useful when you have made changes to the container images defined in the ‘docker-compose.yml’ file and want to rebuild the images before starting the containers.
Explanation: The ‘–build’ flag is used to rebuild the container images before starting the containers. This is necessary when there are changes to the Dockerfiles or build context.
Example output:
Building webapp
Step 1/5 : FROM python:3.9
3.9: Pulling from library/python
...
Use case 4: Start all containers using an alternate compose file
Code:
podman-compose -f path/to/file up
Motivation: This use case is helpful when you want to use a different compose file to define the containers instead of the default ‘docker-compose.yml’ file.
Explanation: The ‘-f’ flag is used to specify an alternate compose file. The ‘path/to/file’ should be the path to the desired compose file.
Example output:
Creating custom_webapp_1 ... done
Creating custom_database_1 ... done
Use case 5: Stop all running containers
Code:
podman-compose stop
Motivation: This use case is useful when you want to stop all the running containers managed by podman-compose.
Explanation: The ‘stop’ command is used to stop the running containers. It gracefully stops the containers, allowing them to perform cleanup operations defined in their entrypoint scripts.
Example output:
Stopping webapp_1 ... done
Stopping database_1 ... done
Use case 6: Remove all containers, networks, and volumes
Code:
podman-compose down --volumes
Motivation: This use case is helpful when you want to completely remove all the containers, networks, and volumes created by podman-compose.
Explanation: The ‘down’ command is used to stop and remove all the containers, networks, and volumes created by podman-compose. The ‘–volumes’ flag is used to also remove the volumes associated with the containers.
Example output:
Stopping webapp_1 ... done
Stopping database_1 ... done
Removing webapp_1 ... done
Removing database_1 ... done
Removing network myapp_default
Use case 7: Follow logs for a container (omit all container names)
Code:
podman-compose logs --follow
Motivation: This use case is useful when you want to follow the logs of all containers managed by podman-compose without specifying individual container names.
Explanation: The ’logs’ command is used to display the log output of the containers. The ‘–follow’ flag is used to continuously display the logs as they are generated.
Example output:
webapp_1 | Server running at http://localhost:3000
database_1 | MySQL init process done. Ready for start up.
Use case 8: Run a one-time command on a service with no ports mapped
Code:
podman-compose run service_name command
Motivation: This use case is helpful when you want to execute a one-time command on a specific service managed by podman-compose. It is particularly useful when the service does not have any ports mapped.
Explanation: The ‘run’ command is used to run a command in a specific service. ‘service_name’ should be replaced with the name of the service defined in the compose file, and ‘command’ should be replaced with the command to be executed.
Example output:
Running command 'bash' in service 'webapp_1'
root@container:/app#
Conclusion:
The ‘podman-compose’ command provides a convenient way to run and manage container definitions using the Compose Specification. With its various options, it allows users to start, stop, build, and interact with containers managed by podman-compose. By understanding these use cases, users can effectively use podman-compose for their container orchestration needs.