10.1 – Find and retrieve container images from a remote registry
Overview
Container images are stored in remote registries, such as Docker Hub or Red Hat Container Catalog. Retrieving these images is essential for deploying and managing containers. This guide covers how to find and retrieve container images using tools like podman and docker.
Using podman to Find and Retrieve Container Images
Step 1: Search for Container Images
Use the podman search command to search for container images in a remote registry.
Syntax
podman search <image_name>Example
- Search for an Nginx Image:
podman search nginxSample Output:
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/library/nginx Official build of Nginx. 1500 [OK]
docker.io docker.io/jwilder/nginx-proxy Automated Nginx reverse proxy for docker con... 150 [OK]
docker.io docker.io/richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of... 80 [OK]Step 2: Pull Container Images
Use the podman pull command to retrieve container images from a remote registry.
Syntax
podman pull <image_name>Example
- Pull the Official Nginx Image:
podman pull docker.io/library/nginxSample Output:
Trying to pull docker.io/library/nginx:latest...
Getting image source signatures
Copying blob sha256:...
Copying config sha256:...
Writing manifest to image destination
Storing signaturesStep 3: List Pulled Images
Use the podman images command to list the images that have been pulled.
Syntax
podman imagesExample
- List Pulled Images:
podman imagesSample Output:
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/library/nginx latest sha256:... 2 weeks ago 133 MBUsing docker to Find and Retrieve Container Images
Step 1: Search for Container Images
Use the docker search command to search for container images in a remote registry.
Syntax
docker search <image_name>Example
- Search for an Nginx Image:
docker search nginxSample Output:
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 1500 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con... 150 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of... 80 [OK]Step 2: Pull Container Images
Use the docker pull command to retrieve container images from a remote registry.
Syntax
docker pull <image_name>Example
- Pull the Official Nginx Image:
docker pull nginxSample Output:
Using default tag: latest
latest: Pulling from library/nginx
Digest: sha256:...
Status: Downloaded newer image for nginx:latestStep 3: List Pulled Images
Use the docker images command to list the images that have been pulled.
Syntax
docker imagesExample
- List Pulled Images:
docker imagesSample Output:
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest sha256:... 2 weeks ago 133 MBPractical Examples
Example 1: Find and Retrieve an Nginx Image Using podman
- Search for an Nginx Image:
podman search nginx- Pull the Official Nginx Image:
podman pull docker.io/library/nginx- List Pulled Images:
podman imagesExample 2: Find and Retrieve an Nginx Image Using docker
- Search for an Nginx Image:
docker search nginx- Pull the Official Nginx Image:
docker pull nginx- List Pulled Images:
docker imagesAdditional Tips
- Specify Image Tags:When pulling images, you can specify a tag to pull a specific version.
podman pull docker.io/library/nginx:1.19
docker pull nginx:1.19- Remove Images:Use the
podman rmiordocker rmicommand to remove images.
podman rmi <image_id>
docker rmi <image_id>- Inspect Images:Use the
podman inspectordocker inspectcommand to view detailed information about an image.
podman inspect <image_id>
docker inspect <image_id>- Login to a Private Registry:Use the
podman loginordocker logincommand to authenticate with a private registry.
podman login <registry_url>
docker login <registry_url>- Pull Images from a Private Registry:Specify the registry URL when pulling images from a private registry.
podman pull <registry_url>/<image_name>
docker pull <registry_url>/<image_name>Conclusion
Finding and retrieving container images from a remote registry is essential for deploying and managing containers. Mastery of these tasks ensures that you can efficiently work with container images, which is crucial for the RHCSA exam.
These notes should help you understand how to find and retrieve container images from a remote registry for the RHCSA exam.
10.2 – Inspect container images
Overview
Inspecting container images provides detailed information about the image, including its configuration, layers, and metadata. This information is essential for understanding the contents and behavior of the image. This guide covers how to inspect container images using tools like podman and docker.
Using podman to Inspect Container Images
Step 1: List Available Images
Use the podman images command to list the images available on your system.
Syntax
podman imagesExample
- List Available Images:
podman imagesSample Output:
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/library/nginx latest sha256:... 2 weeks ago 133 MBStep 2: Inspect a Container Image
Use the podman inspect command to view detailed information about a container image.
Syntax
podman inspect <image_id_or_name>Example
- Inspect the Nginx Image:
podman inspect docker.io/library/nginxSample Output (truncated for brevity):
[
{
"Id": "sha256:...",
"RepoTags": [
"docker.io/library/nginx:latest"
],
"RepoDigests": [
"docker.io/library/nginx@sha256:..."
],
"Parent": "",
"Comment": "",
"Created": "2023-09-01T12:00:00Z",
"ContainerConfig": {
"Hostname": "...",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"80/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"nginx",
"-g",
"daemon off;"
],
"Image": "sha256:...",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {}
},
"DockerVersion": "20.10.7",
"Author": "",
"Architecture": "amd64",
"Os": "linux",
"Size": 133000000,
"VirtualSize": 133000000,
"GraphDriver": {
"Data": {
"LowerDir": "...",
"MergedDir": "...",
"UpperDir": "...",
"WorkDir": "..."
},
"Name": "overlay2"
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:...",
"sha256:..."
]
},
"Metadata": {
"LastTagTime": "2023-09-01T12:00:00Z"
}
}
]Step 3: Filter Specific Information
Use the --format option with podman inspect to filter specific information.
Syntax
podman inspect --format '<template>' <image_id_or_name>Example
- Get the Image ID:
podman inspect --format '{{.Id}}' docker.io/library/nginxSample Output:
sha256:...- Get the Exposed Ports:
podman inspect --format '{{.ContainerConfig.ExposedPorts}}' docker.io/library/nginxSample Output:
map[80/tcp:{}]Using docker to Inspect Container Images
Step 1: List Available Images
Use the docker images command to list the images available on your system.
Syntax
docker imagesExample
- List Available Images:
docker imagesSample Output:
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest sha256:... 2 weeks ago 133 MBStep 2: Inspect a Container Image
Use the docker inspect command to view detailed information about a container image.
Syntax
docker inspect <image_id_or_name>Example
Inspect the Nginx Image:
docker inspect nginxSample Output (truncated for brevity):
[
{
"Id": "sha256:...",
"RepoTags": [
"nginx:latest"
],
"RepoDigests": [
"nginx@sha256:..."
],
"Parent": "",
"Comment": "",
"Created": "2023-09-01T12:00:00Z",
"ContainerConfig": {
"Hostname": "...",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"80/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"nginx",
"-g",
"daemon off;"
],
"Image": "sha256:...",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {}
},
"DockerVersion": "20.10.7",
"Author": "",
"Architecture": "amd64",
"Os": "linux",
"Size": 133000000,
"VirtualSize": 133000000,
"GraphDriver": {
"Data": {
"LowerDir": "...",
"MergedDir": "...",
"UpperDir": "...",
"WorkDir": "..."
},
"Name": "overlay2"
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:...",
"sha256:..."
]
},
"Metadata": {
"LastTagTime": "2023-09-01T12:00:00Z"
}
}
]Step 3: Filter Specific Information
Use the --format option with docker inspect to filter specific information.
Syntax
docker inspect --format '<template>' <image_id_or_name>Example
- Get the Image ID:
docker inspect --format '{{.Id}}' nginxSample Output:
sha256:...- Get the Exposed Ports:
docker inspect --format '{{.ContainerConfig.ExposedPorts}}' nginxSample Output:
map[80/tcp:{}]Practical Examples
Example 1: Inspect an Nginx Image Using podman
- List Available Images:
podman images- Inspect the Nginx Image:
podman inspect docker.io/library/nginx- Get the Image ID:
podman inspect --format '{{.Id}}' docker.io/library/nginx- Get the Exposed Ports:
podman inspect --format '{{.ContainerConfig.ExposedPorts}}' docker.io/library/nginxExample 2: Inspect an Nginx Image Using docker
- List Available Images:
docker images- Inspect the Nginx Image:
docker inspect nginx- Get the Image ID:
docker inspect --format '{{.Id}}' nginx- Get the Exposed Ports:
docker inspect --format '{{.ContainerConfig.ExposedPorts}}' nginxAdditional Tips
- Inspect Running Containers:Use the
podman inspectordocker inspectcommand to inspect running containers.
podman inspect <container_id_or_name>
docker inspect <container_id_or_name>- Inspect Specific Layers:Use the
podman historyordocker historycommand to view the history of an image, including its layers.
podman history <image_id_or_name>
docker history <image_id_or_name>- Save and Load Images:Use the
podman saveandpodman loadordocker saveanddocker loadcommands to save and load images.
podman save -o <file_name> <image_id_or_name>
podman load -i <file_name>
docker save -o <file_name> <image_id_or_name>
docker load -i <file_name>- Remove Images:Use the
podman rmiordocker rmicommand to remove images.
podman rmi <image_id_or_name>
docker rmi <image_id_or_name>Conclusion
Inspecting container images provides detailed information about the image, including its configuration, layers, and metadata. Mastery of these tasks ensures that you can efficiently work with container images, which is crucial for the RHCSA exam.
These notes should help you understand how to inspect container images for the RHCSA exam.
10.3 – Perform container management using commands such as podman and skopeo
Overview
Container management involves creating, running, stopping, and managing containers and images. podman is a tool for managing containers and images, while skopeo is used for inspecting and transferring container images. This guide covers essential container management tasks using podman and skopeo.
Using podman for Container Management
Installing podman
Ensure podman is installed on your system.
sudo yum install podmanBasic podman Commands
Pulling Images
Use the podman pull command to retrieve container images from a remote registry.
podman pull <image_name>Example:
podman pull docker.io/library/nginxListing Images
Use the podman images command to list the images available on your system.
podman imagesExample:
podman imagesRunning Containers
Use the podman run command to create and start a new container.
podman run [options] <image_name> [command]Example:
podman run -d --name mynginx -p 8080:80 docker.io/library/nginxListing Containers
Use the podman ps command to list running containers.
podman psExample:
podman psUse the podman ps -a command to list all containers, including stopped ones.
podman ps -aExample:
podman ps -aStopping Containers
Use the podman stop command to stop a running container.
podman stop <container_id_or_name>Example:
podman stop mynginxRemoving Containers
Use the podman rm command to remove a stopped container.
podman rm <container_id_or_name>Example:
podman rm mynginxInspecting Containers
Use the podman inspect command to view detailed information about a container.
podman inspect <container_id_or_name>Example:
podman inspect mynginxPractical Examples
Example 1: Pull and Run an Nginx Container
- Pull the Nginx Image:
podman pull docker.io/library/nginx- Run the Nginx Container:
podman run -d --name mynginx -p 8080:80 docker.io/library/nginx- List Running Containers:
podman ps- Inspect the Nginx Container:
podman inspect mynginx- Stop the Nginx Container:
podman stop mynginx- Remove the Nginx Container:
podman rm mynginxUsing skopeo for Image Management
Installing skopeo
Ensure skopeo is installed on your system.
sudo yum install skopeoBasic skopeo Commands
Inspecting Remote Images
Use the skopeo inspect command to inspect remote images without pulling them.
skopeo inspect docker://<registry>/<image_name>
Example:
skopeo inspect docker://docker.io/library/nginxCopying Images
Use the skopeo copy command to copy images between different locations.
skopeo copy <source> <destination>Example:
skopeo copy docker://docker.io/library/nginx:latest dir:/tmp/nginxDeleting Images
Use the skopeo delete command to delete images from a remote registry.
skopeo delete docker://<registry>/<image_name>Example:
skopeo delete docker://docker.io/library/nginx:latestPractical Examples
Example 1: Inspect a Remote Nginx Image
- Inspect the Nginx Image:
skopeo inspect docker://docker.io/library/nginxExample 2: Copy an Image from Docker Hub to a Local Directory
- Copy the Nginx Image:
skopeo copy docker://docker.io/library/nginx:latest dir:/tmp/nginxExample 3: Delete an Image from a Remote Registry
- Delete the Nginx Image:
skopeo delete docker://docker.io/library/nginx:latestAdditional Tips
- Login to a Private Registry:Use the
podman loginorskopeo logincommand to authenticate with a private registry.
podman login <registry_url>
skopeo login <registry_url>- Tagging Images:Use the
podman tagcommand to tag images.
podman tag <image_id_or_name> <new_tag>- Example:
podman tag docker.io/library/nginx:latest myregistry.com/mynginx:latest- Pushing Images:Use the
podman pushcommand to push images to a remote registry.
podman push <image_id_or_name> <registry_url>/<image_name>Example:
podman push myregistry.com/mynginx:latest- Remove Images:Use the
podman rmicommand to remove images.
podman rmi <image_id_or_name>Example:
podman rmi docker.io/library/nginx- Check Container Logs:Use the
podman logscommand to view container logs.
podman logs <container_id_or_name>Example:
podman logs mynginxConclusion
Performing container management using podman and skopeo involves creating, running, stopping, and managing containers and images. Mastery of these tasks ensures that you can efficiently work with containers and images, which is crucial for the RHCSA exam.
These notes should help you understand how to perform container management using commands such as podman and skopeo for the RHCSA exam.
10.4 – Perform basic container management such as running, starting, stopping, and listing running containers
Overview
Basic container management involves running, starting, stopping, and listing containers. This guide covers how to perform these tasks using podman, a popular container management tool.
Using podman for Basic Container Management
Installing podman
Ensure podman is installed on your system.
sudo yum install podmanRunning Containers
Use the podman run command to create and start a new container.
Syntax
podman run [options] <image_name> [command]Examples
- Run a Container in the Foreground:
podman run docker.io/library/nginx- Run a Container in Detached Mode:
podman run -d --name mynginx docker.io/library/nginx- Run a Container with Port Mapping:
podman run -d --name mynginx -p 8080:80 docker.io/library/nginxStarting Containers
Use the podman start command to start a stopped container.
Syntax
podman start <container_id_or_name>Example
- Start a Stopped Container:
podman start mynginxStopping Containers
Use the podman stop command to stop a running container.
Syntax
podman stop <container_id_or_name>Example
- Stop a Running Container:
podman stop mynginxListing Containers
Use the podman ps command to list running containers.
Syntax
podman psExample
- List Running Containers:
podman psSample Output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
abc123 docker.io/library/nginx:latest nginx -g daemon off; 2 minutes ago Up 2 minutes ago 0.0.0.0:8080->80/tcp mynginxUse the podman ps -a command to list all containers, including stopped ones.
Syntax
podman ps -aExample
- List All Containers:
podman ps -aSample Output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
abc123 docker.io/library/nginx:latest nginx -g daemon off; 2 minutes ago Up 2 minutes ago 0.0.0.0:8080->80/tcp mynginx
def456 docker.io/library/nginx:latest nginx -g daemon off; 5 minutes ago Exited (0) 3 minutes ago 0.0.0.0:8080->80/tcp mynginx2Removing Containers
Use the podman rm command to remove a stopped container.
Syntax
podman rm <container_id_or_name>Example
- Remove a Stopped Container:
podman rm mynginxPractical Examples
Example 1: Run and Manage an Nginx Container
- Run the Nginx Container in Detached Mode:
podman run -d --name mynginx -p 8080:80 docker.io/library/nginx- List Running Containers:
podman ps- Stop the Nginx Container:
podman stop mynginx- Start the Nginx Container:
podman start mynginx- List All Containers:
podman ps -a- Remove the Nginx Container:
podman rm mynginxExample 2: Run a Container with a Custom Command
- Run an Alpine Container with a Custom Command:
podman run --name myalpine alpine echo "Hello, World!"- List All Containers:
podman ps -a- Remove the Alpine Container:
podman rm myalpineAdditional Tips
- Check Container Logs:Use the
podman logscommand to view container logs.
podman logs <container_id_or_name>Example:
podman logs mynginx- Inspect Containers:Use the
podman inspectcommand to view detailed information about a container.
podman inspect <container_id_or_name>Example:
podman inspect mynginx- Execute Commands in Running Containers:Use the
podman execcommand to execute commands in a running container.
podman exec -it <container_id_or_name> <command>Example:
podman exec -it mynginx /bin/bash- Remove All Stopped Containers:Use the
podman rmcommand with the-aoption to remove all stopped containers.
podman rm -a- Run Containers with Environment Variables:Use the
-eoption withpodman runto set environment variables.
podman run -d --name mynginx -e NGINX_PORT=8080 docker.io/library/nginxConclusion
Performing basic container management tasks such as running, starting, stopping, and listing containers is essential for managing containerized applications. Mastery of these tasks ensures that you can efficiently work with containers, which is crucial for the RHCSA exam.
These notes should help you understand how to perform basic container management tasks for the RHCSA exam.
10.5 – Run a service inside a container
Overview
Running a service inside a container involves creating a container from an image that includes the service, configuring the container, and starting the service. This guide covers how to run a service inside a container using podman.
Using podman to Run a Service Inside a Container
Step 1: Pull the Required Image
Use the podman pull command to retrieve the container image that includes the service you want to run.
Syntax
podman pull <image_name>Example
- Pull the Nginx Image:
podman pull docker.io/library/nginxStep 2: Run the Container
Use the podman run command to create and start a new container with the service.
Syntax
podman run [options] <image_name> [command]Examples
- Run the Nginx Container in Detached Mode:
podman run -d --name mynginx -p 8080:80 docker.io/library/nginx- Run the Nginx Container with a Custom Configuration:
podman run -d --name mynginx -p 8080:80 -v /path/to/nginx.conf:/etc/nginx/nginx.conf:ro docker.io/library/nginxStep 3: Verify the Service is Running
Use the podman ps command to list running containers and verify that the service is running.
Syntax
podman psExample
- List Running Containers:
podman psSample Output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
abc123 docker.io/library/nginx:latest nginx -g daemon off; 2 minutes ago Up 2 minutes ago 0.0.0.0:8080->80/tcp mynginxStep 4: Access the Service
Access the service running inside the container using a web browser or a command-line tool like curl.
Example
- Access the Nginx Service Using a Web Browser:Open a web browser and navigate to
http://localhost:8080. - Access the Nginx Service Using
curl:
curl http://localhost:8080Sample Output:
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p>
</body>
</html>Practical Examples
Example 1: Run an Nginx Web Server Inside a Container
- Pull the Nginx Image:
podman pull docker.io/library/nginx- Run the Nginx Container in Detached Mode:
podman run -d --name mynginx -p 8080:80 docker.io/library/nginx- List Running Containers:
podman ps- Access the Nginx Service Using a Web Browser:Open a web browser and navigate to
http://localhost:8080. - Access the Nginx Service Using
curl:
curl http://localhost:8080Example 2: Run an Apache HTTP Server Inside a Container
- Pull the Apache HTTP Server Image:
podman pull docker.io/library/httpd- Run the Apache HTTP Server Container in Detached Mode:
podman run -d --name myhttpd -p 8081:80 docker.io/library/httpd- List Running Containers:
podman ps- Access the Apache HTTP Server Using a Web Browser:Open a web browser and navigate to
http://localhost:8081. - Access the Apache HTTP Server Using
curl:
curl http://localhost:8081Example 3: Run a MySQL Database Server Inside a Container
- Pull the MySQL Image:
podman pull docker.io/library/mysql- Run the MySQL Container with Environment Variables:
podman run -d --name mymysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -p 3306:3306 docker.io/library/mysql- List Running Containers:
podman ps- Access the MySQL Database Server:Use a MySQL client to connect to the database server.
mysql -h 127.0.0.1 -P 3306 -u root -pAdditional Tips
- Check Container Logs:Use the
podman logscommand to view container logs.
podman logs <container_id_or_name>Example:
podman logs mynginx- Inspect Containers:Use the
podman inspectcommand to view detailed information about a container.
podman inspect <container_id_or_name>Example:
podman inspect mynginx- Execute Commands in Running Containers:Use the
podman execcommand to execute commands in a running container.
podman exec -it <container_id_or_name> <command>Example:
podman exec -it mynginx /bin/bash- Remove Containers:Use the
podman rmcommand to remove stopped containers.
podman rm <container_id_or_name>Example:
podman rm mynginx- Remove Images:Use the
podman rmicommand to remove images.
podman rmi <image_id_or_name>Example:
podman rmi docker.io/library/nginxConclusion
Running a service inside a container involves creating a container from an image that includes the service, configuring the container, and starting the service. Mastery of these tasks ensures that you can efficiently deploy and manage containerized services, which is crucial for the RHCSA exam.
These notes should help you understand how to run a service inside a container for the RHCSA exam.
10.6 – Configure a container to start automatically as a systemd service
Overview
Configuring a container to start automatically as a systemd service ensures that the container is managed by systemd and starts at boot. This guide covers how to create a systemd service unit file for a container managed by podman.
Prerequisites
Ensure podman is installed on your system.
sudo yum install podmanSteps to Configure a Container as a Systemd Service
Step 1: Create a Podman Container
First, create and run the container using podman.
Example
- Pull the Nginx Image:
podman pull docker.io/library/nginx- Run the Nginx Container:
podman run -d --name mynginx -p 8080:80 docker.io/library/nginxStep 2: Generate a Systemd Service Unit File
Use the podman generate systemd command to generate a systemd service unit file for the container.
Syntax
podman generate systemd --name <container_name> --files --newExample
- Generate a Systemd Service Unit File for the Nginx Container:
podman generate systemd --name mynginx --files --newThis command generates a service unit file named container-mynginx.service in the current directory.
Step 3: Move the Service Unit File to the Systemd Directory
Move the generated service unit file to the systemd directory.
Example
- Move the Service Unit File:
sudo mv container-mynginx.service /etc/systemd/system/Step 4: Enable and Start the Systemd Service
Use the systemctl command to enable and start the systemd service.
Syntax
sudo systemctl enable <service_name>
sudo systemctl start <service_name>Example
- Enable the Nginx Container Service:
sudo systemctl enable container-mynginx.service- Start the Nginx Container Service:
sudo systemctl start container-mynginx.serviceStep 5: Verify the Service
Use the systemctl status command to verify that the service is running.
Syntax
systemctl status <service_name>Example
- Check the Status of the Nginx Container Service:
systemctl status container-mynginx.serviceSample Output:
● container-mynginx.service - Podman container-mynginx.service
Loaded: loaded (/etc/systemd/system/container-mynginx.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2023-10-01 12:00:00 UTC; 1h 30min ago
Main PID: 1234 (podman)
Tasks: 1 (limit: 4915)
Memory: 5.0M
CGroup: /system.slice/container-mynginx.service
└─1234 /usr/bin/podman start --attach mynginxPractical Examples
Example 1: Configure an Nginx Container to Start Automatically as a Systemd Service
- Pull the Nginx Image:
podman pull docker.io/library/nginx- Run the Nginx Container:
podman run -d --name mynginx -p 8080:80 docker.io/library/nginx- Generate a Systemd Service Unit File:
podman generate systemd --name mynginx --files --new- Move the Service Unit File:
sudo mv container-mynginx.service /etc/systemd/system/- Enable the Nginx Container Service:
sudo systemctl enable container-mynginx.service- Start the Nginx Container Service:
sudo systemctl start container-mynginx.service- Check the Status of the Nginx Container Service:
systemctl status container-mynginx.serviceExample 2: Configure an Apache HTTP Server Container to Start Automatically as a Systemd Service
- Pull the Apache HTTP Server Image:
podman pull docker.io/library/httpd- Run the Apache HTTP Server Container:
podman run -d --name myhttpd -p 8081:80 docker.io/library/httpd- Generate a Systemd Service Unit File:
podman generate systemd --name myhttpd --files --new- Move the Service Unit File:
sudo mv container-myhttpd.service /etc/systemd/system/- Enable the Apache HTTP Server Container Service:
sudo systemctl enable container-myhttpd.service- Start the Apache HTTP Server Container Service:
sudo systemctl start container-myhttpd.service- Check the Status of the Apache HTTP Server Container Service:
systemctl status container-myhttpd.serviceAdditional Tips
- Reload Systemd Configuration:If you make changes to the service unit file, reload the systemd configuration.
sudo systemctl daemon-reload- Check Container Logs:Use the
podman logscommand to view container logs.
podman logs <container_id_or_name>Example:
podman logs mynginx- Inspect Containers:Use the
podman inspectcommand to view detailed information about a container.
podman inspect <container_id_or_name>Example:
podman inspect mynginx- Remove Containers:Use the
podman rmcommand to remove stopped containers.
podman rm <container_id_or_name>Example:
podman rm mynginx- Remove Images:Use the
podman rmicommand to remove images.
podman rmi <image_id_or_name>Example:
podman rmi docker.io/library/nginxConclusion
Configuring a container to start automatically as a systemd service ensures that the container is managed by systemd and starts at boot. Mastery of these tasks ensures that you can efficiently deploy and manage containerized services, which is crucial for the RHCSA exam.
These notes should help you understand how to configure a container to start automatically as a systemd service for the RHCSA exam.
10.7 – Attach persistent storage to a container
Overview
Attaching persistent storage to a container ensures that data is retained even when the container is stopped or removed. This guide covers how to attach persistent storage to a container using podman.
Using podman to Attach Persistent Storage
Step 1: Create a Directory for Persistent Storage
Create a directory on the host system that will be used for persistent storage.
Example
- Create a Directory:
sudo mkdir -p /var/lib/mydata- Set Appropriate Permissions:
sudo chown 1000:1000 /var/lib/mydataStep 2: Run a Container with a Volume
Use the podman run command with the -v option to mount the directory as a volume inside the container.
Syntax
podman run [options] -v <host_directory>:<container_directory> <image_name> [command]Examples
- Run an Nginx Container with Persistent Storage:
podman run -d --name mynginx -p 8080:80 -v /var/lib/mydata:/usr/share/nginx/html:Z docker.io/library/nginx-d: Run the container in detached mode.--name mynginx: Name the containermynginx.-p 8080:80: Map port 8080 on the host to port 80 in the container.-v /var/lib/mydata:/usr/share/nginx/html:Z: Mount the host directory/var/lib/mydatato the container directory/usr/share/nginx/htmlwith SELinux contextZ.
Step 3: Verify the Volume Mount
Use the podman inspect command to verify that the volume is mounted correctly.
Syntax
podman inspect <container_id_or_name>Example
- Inspect the Nginx Container:
podman inspect mynginxSample Output (truncated for brevity):
[
{
"Id": "sha256:...",
"Name": "mynginx",
"Mounts": [
{
"Type": "bind",
"Source": "/var/lib/mydata",
"Destination": "/usr/share/nginx/html",
"Mode": "Z",
"RW": true,
"Propagation": "rprivate"
}
],
...
}
]Step 4: Test Persistent Storage
Create a file in the host directory and verify that it is accessible inside the container.
Example
- Create a File in the Host Directory:
echo "Hello, World!" | sudo tee /var/lib/mydata/index.html- Access the File Inside the Container:Open a web browser and navigate to
http://localhost:8080.
Sample Output:
Hello, World!Practical Examples
Example 1: Attach Persistent Storage to an Nginx Container
- Create a Directory for Persistent Storage:
sudo mkdir -p /var/lib/mydata- Set Appropriate Permissions:
sudo chown 1000:1000 /var/lib/mydata- Run the Nginx Container with Persistent Storage:
podman run -d --name mynginx -p 8080:80 -v /var/lib/mydata:/usr/share/nginx/html:Z docker.io/library/nginx- Verify the Volume Mount:
podman inspect mynginx- Create a File in the Host Directory:
echo "Hello, World!" | sudo tee /var/lib/mydata/index.html- Access the File Inside the Container:Open a web browser and navigate to
http://localhost:8080.
Example 2: Attach Persistent Storage to a MySQL Container
- Create a Directory for Persistent Storage:
sudo mkdir -p /var/lib/mysql_data- Set Appropriate Permissions:
sudo chown 1001:1001 /var/lib/mysql_data- Run the MySQL Container with Persistent Storage:
podman run -d --name mymysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -v /var/lib/mysql_data:/var/lib/mysql:Z -p 3306:3306 docker.io/library/mysql- Verify the Volume Mount:
podman inspect mymysql- Access the MySQL Database Server:Use a MySQL client to connect to the database server.
mysql -h 127.0.0.1 -P 3306 -u root -pAdditional Tips
- Use Named Volumes:Instead of using host directories, you can use named volumes for better management.
podman volume create myvolumepodman run -d --name mynginx -p 8080:80 -v myvolume:/usr/share/nginx/html:Z docker.io/library/nginx- List Volumes:Use the
podman volume lscommand to list all volumes.
podman volume ls- Inspect Volumes:Use the
podman volume inspectcommand to view detailed information about a volume.
podman volume inspect myvolume- Remove Volumes:Use the
podman volume rmcommand to remove volumes.
podman volume rm myvolume- Backup and Restore Volumes:Use the
podman runcommand with tar to backup and restore volumes.Backup:
podman run --rm -v myvolume:/volume -v /backup:/backup alpine tar czvf /backup/myvolume.tar.gz -C /volumeRestore:
podman run --rm -v myvolume:/volume -v /backup:/backup alpine tar xzvf /backup/myvolume.tar.gz -C /volumeConclusion
Attaching persistent storage to a container ensures that data is retained even when the container is stopped or removed. Mastery of these tasks ensures that you can efficiently manage containerized services with persistent storage, which is crucial for the RHCSA exam.
These notes should help you understand how to attach persistent storage to a container for the RHCSA exam.
Discover more from Altgr Blog
Subscribe to get the latest posts sent to your email.
