How to use the command 'podman run' (with examples)

How to use the command 'podman run' (with examples)

The ‘podman run’ command is used to run a command in a new Podman container. It is a versatile command that offers various options for running containers. Below, we will explore different use cases of the ‘podman run’ command along with their codes, motivations, explanations, and example outputs.

Use case 1: Run command in a new container from a tagged image

Code:

podman run image:tag command

Motivation: This use case is helpful when you want to run a command in a new container using a specific image and tag. It allows you to easily execute commands or applications within a container environment.

Explanation:

  • podman run starts a new container.
  • image:tag specifies the image and its associated tag. Replace image with the desired image name and tag with the corresponding tag.
  • command is the command to be executed inside the container.

Example output:

Hello, World!

Use case 2: Run command in a new container in background and display its ID

Code:

podman run --detach image:tag command

Motivation: Running a container in the background allows you to start long-running services or applications that you don’t need to actively monitor. Displaying the container ID enables you to manage and interact with the container later.

Explanation:

  • --detach runs the container in the background.
  • image:tag specifies the image and its associated tag.
  • command is the command to be executed inside the container.

Example output:

7c19e48286eb5e4309437b732f352516702d0f2e0dcbf16c4ce3fd2ff5ee2eab

Use case 3: Run command in a one-off container in interactive mode and pseudo-TTY

Code:

podman run --rm --interactive --tty image:tag command

Motivation: When you want to run a command interactively in a container and have access to the container’s terminal, you can use this use case. It is useful for debugging or testing purposes.

Explanation:

  • --rm automatically removes the container when it exits.
  • --interactive allocates a pseudo-TTY for the container.
  • --tty connects the container to your terminal.
  • image:tag specifies the image and its associated tag.
  • command is the command to be executed inside the container.

Example output:

Welcome to the container!

Use case 4: Run command in a new container with passed environment variables

Code:

podman run --env 'variable=value' --env variable image:tag command

Motivation: Sometimes, you need to provide specific environment variables to a container at runtime. This use case allows you to pass environment variables to the container easily.

Explanation:

  • --env 'variable=value' sets the environment variable variable to value inside the container.
  • --env variable sets the environment variable variable with the same value as the host’s environment variable.
  • image:tag specifies the image and its associated tag.
  • command is the command to be executed inside the container.

Example output:

Variable: value

Use case 5: Run command in a new container with bind mounted volumes

Code:

podman run --volume /path/to/host_path:/path/to/container_path image:tag command

Motivation: Bind mounting allows you to access files or directories from the host system inside the container. This use case is beneficial when you need to share data between the host and the container.

Explanation:

  • --volume /path/to/host_path:/path/to/container_path mounts the host’s directory or file at /path/to/host_path to the container’s directory or file at /path/to/container_path.
  • image:tag specifies the image and its associated tag.
  • command is the command to be executed inside the container.

Example output:

File contents: Hello, World!

Use case 6: Run command in a new container with published ports

Code:

podman run --publish host_port:container_port image:tag command

Motivation: Publishing container ports allows you to access services running inside the container from your host system or other networks. This use case is useful for running networked applications within containers.

Explanation:

  • --publish host_port:container_port maps the container’s container_port to the host’s host_port.
  • image:tag specifies the image and its associated tag.
  • command is the command to be executed inside the container.

Example output:

Server listening on port 8080...

Use case 7: Run command in a new container overwriting the entrypoint of the image

Code:

podman run --entrypoint command image:tag

Motivation: Overwriting the entrypoint allows you to run a different command from the one specified in the image. This use case enables customization and flexibility when executing commands within containers.

Explanation:

  • --entrypoint command specifies the command to be executed instead of the image’s default entrypoint.
  • image:tag specifies the image and its associated tag.

Example output:

Command output: Hello, World!

Use case 8: Run command in a new container connecting it to a network

Code:

podman run --network network image:tag

Motivation: Connecting a container to a network enables network communication between containers or between containers and the host system. This use case is suitable for building complex distributed applications using containers.

Explanation:

  • --network network connects the container to the specified network.
  • image:tag specifies the image and its associated tag.

Example output:

Connected to network.

Conclusion

The ‘podman run’ command offers a wide range of options for running commands in new containers. By utilizing these use cases, you can effectively manage and interact with containers, customize their behavior, and create diverse containerized environments.

Related Posts

How to use the command 'luac' (with examples)

How to use the command 'luac' (with examples)

The command ’luac’ is the Lua bytecode compiler. It is used to compile a Lua source file into Lua bytecode.

Read More
How to use the command pacman --remove (with examples)

How to use the command pacman --remove (with examples)

Pacman is a package manager utility for Arch Linux. The pacman --remove command is used to remove packages from the system.

Read More
How to use the command 'toolbox init-container' (with examples)

How to use the command 'toolbox init-container' (with examples)

This article provides examples of how to use the ’toolbox init-container’ command.

Read More