Manage Containers
1. Introduction to Containers
Containers are a lightweight, standalone, and executable software package that includes everything required to run a piece of software, including the code, runtime, libraries, and system tools. They isolate applications from the system, ensuring consistency across different environments.
2. Finding and Retrieving Container Images
a. Remote Registry
A service responsible for hosting and distributing container images.
Example: Using Podman to search for a container:
# Searching for a CentOS container image podman search centos
Example: Pulling a container image using Podman:
# Pulling the latest CentOS container image podman pull centos
3. Inspecting Container Images
Retrieving detailed information about a container image, such as its layers, size, and metadata.
Example:
# Inspecting the CentOS container image podman inspect centos
4. Container Management with podman and skopeo
a. Podman
A tool to manage OCI containers and container images, offering a similar experience to Docker but without the daemon.
Example: Listing running containers:
podman ps
b. Skopeo
A command-line utility that performs various operations on container images and image repositories. For instance, copying container images between different registries or locally.
Example: Copying an image from Docker Hub to a local directory:
skopeo copy docker://centos:7 dir:centos7_image
5. Building Containers from a Containerfile
A Containerfile is similar to a Dockerfile and dictates how to build a particular container image.
Example: A basic Containerfile for a web application might look like:
FROM centos:7 RUN yum install -y httpd COPY ./mywebpage /var/www/html/ CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]
Building the container:
podman build -t mywebapp .
6. Basic Container Management
Example: Running a container:
podman run -d --name mywebcontainer mywebapp
Stopping, starting, and listing containers:
podman stop mywebcontainer podman start mywebcontainer podman ps
7. Running Services Inside a Container
Example: Running a database service inside a container:
podman run -d --name mysqldb -e MYSQL_ROOT_PASSWORD=mypassword mysql
8. Configuring Containers with systemd
Integrating container management into the system’s init system to start containers at boot.
Example: A basic systemd service file for a web application container might look like:
[Unit] Description=My Web App Container After=network.target [Service] Restart=always ExecStart=/usr/bin/podman start -a mywebapp ExecStop=/usr/bin/podman stop -t 10 mywebapp [Install] WantedBy=multi-user.target
9. Attaching Persistent Storage to a Container
Providing a container with access to persistent data, ensuring data outlives the container’s lifecycle.
Example: Mounting a directory into a container:
podman run -d --name mywebcontainer -v /path/on/host:/path/in/container mywebapp
Practical Exercise
- Set up a simple web application container and expose it on a specific port.
- Attach a volume to persist the web application’s logs.
- Use
skopeoto copy a container image between different registries.
Summary
Containers have transformed software development and deployment by isolating applications and unifying deployment procedures. Red Hat’s toolset, including Podman and Skopeo, provides a robust platform for managing containers without relying on a central daemon.
Key Points
- Containers are isolated but share the host’s OS kernel.
podmanprovides an intuitive interface to manage containers.skopeoallows precise control over container images across registries.- Containers can be integrated with the host’s init system (
systemd) for autostart capabilities. - Data persistence in containers is achieved through volumes.