This guide covers essential container lifecycle operations including creating, starting, stopping, and managing running containers.
Creating containers
The container create command creates a container from an image without starting it. This is useful when you want to configure a container but start it later.
container create --name my-container ubuntu:latest echo "hello"
After creating a container, you can start it using the container start command:
container start my-container
Use container run to create and start a container in a single command. The run command is a combination of create and start.
Running containers
The container run command is the most common way to start containers. It creates and starts a container in one operation.
Basic usage
# Run a container and attach an interactive shell
container run -it ubuntu:latest /bin/bash
# Run a background web server
container run -d --name web -p 8080:80 nginx:latest
# Set environment variables and limit resources
container run -e NODE_ENV=production --cpus 2 --memory 1G node:18
Common options
Process options
Management options
Resource options
-e, --env: Set environment variables (format: key=value)
--env-file: Read environment variables from a file
-u, --user: Set the user for the process
-w, --workdir: Set the initial working directory
-i, --interactive: Keep standard input open
-t, --tty: Open a TTY with the process
-d, --detach: Run the container in the background
--name: Assign a name to the container
--rm: Automatically remove the container after it stops
--entrypoint: Override the entrypoint of the image
--init: Run an init process that forwards signals and reaps processes
-c, --cpus: Number of CPUs to allocate
-m, --memory: Amount of memory (with optional K, M, G, T, or P suffix)
Detached vs. interactive mode
Detached mode (-d) runs containers in the background:
container run -d --name my-web-server --rm web-test
The --detach flag runs the container in the background, so you can continue running commands in the same terminal.
Interactive mode (-it) allows you to interact with the container:
container run -it ubuntu:latest /bin/bash
The -i flag keeps stdin open, and -t allocates a pseudo-TTY.
You will often see these two options abbreviated and specified together as -ti or -it.
Starting and stopping containers
Starting containers
Start a stopped container:
container start <container-id>
You can attach to the container’s output streams:
container start --attach --interactive my-container
Stopping containers gracefully
The container stop command stops running containers gracefully by sending a signal (default: SIGTERM):
# Stop a specific container
container stop my-web-server
# Stop multiple containers
container stop container1 container2 container3
# Stop all running containers
container stop --all
# Specify a custom timeout before SIGKILL
container stop --time 10 my-container
The --time option specifies seconds to wait before killing the container (default: 5 seconds).
The container kill command immediately kills running containers:
# Kill a container with default KILL signal
container kill my-container
# Send a custom signal
container kill --signal SIGTERM my-container
# Kill all running containers
container kill --all
Use kill with caution: it does not allow for graceful shutdown. Prefer stop for normal operations.
Executing commands in running containers
The container exec command runs a new command inside a running container:
# Execute a single command
container exec my-web-server ls /content
Output:
Interactive shell
Run an interactive shell to explore the container:
container exec --tty --interactive my-web-server sh
/content # ls
index.html
/content # uname -a
Linux my-web-server 6.12.28 #1 SMP Tue May 20 15:19:05 UTC 2025 aarch64 Linux
/content # exit
The --tty and --interactive flags allow you to interact with the shell from your host terminal. These are often abbreviated as -it.
Additional exec options
# Run as a specific user
container exec --user www-data my-container whoami
# Set environment variables
container exec --env DEBUG=true my-container /app/script.sh
# Change working directory
container exec --workdir /var/log my-container ls -la
# Run in detached mode
container exec --detach my-container /background-task.sh
Viewing container logs
The container logs command displays the output from your containerized application.
Basic usage
container logs my-web-server
Example output:
192.168.64.1 - - [15/May/2025 03:00:03] "GET / HTTP/1.1" 200 -
Following logs in real-time
container logs --follow my-web-server
Press Ctrl+C to stop following the logs.
Limiting log output
# Show only the last 50 lines
container logs -n 50 my-web-server
Viewing boot logs
Use the --boot option to see the logs for the virtual machine boot and init process:
container logs --boot my-web-server
Example output:
[ 0.098284] cacheinfo: Unable to detect cache hierarchy for CPU 0
[ 0.098466] random: crng init done
[ 0.099657] brd: module loaded
[ 0.100707] loop: module loaded
[ 0.127467] EXT4-fs (vda): mounted filesystem without journal. Quota mode: disabled.
[ 0.127525] VFS: Mounted root (ext4 filesystem) readonly on device 254:0.
[ 0.143252] Run /sbin/vminitd as init process
2025-05-15T02:24:08+0000 info vminitd : [vminitd] vminitd booting...
Listing containers
The container list (or container ls) command shows containers.
List running containers
ID IMAGE OS ARCH STATE ADDR
my-web-server web-test:latest linux arm64 running 192.168.64.3
List all containers (including stopped)
You can abbreviate the --all option to -a:
Inspecting containers
The container inspect command displays detailed container information in JSON format:
container inspect my-web-server | jq
[
{
"status": "running",
"networks": [
{
"address": "192.168.64.3/24",
"gateway": "192.168.64.1",
"hostname": "my-web-server.test.",
"network": "default"
}
],
"configuration": {
"mounts": [],
"hostname": "my-web-server",
"id": "my-web-server",
"resources": {
"cpus": 4,
"memoryInBytes": 1073741824
}
}
}
]
Use jq to filter and format JSON output:# Get only the IP address
container inspect my-web-server | jq '.[0].networks[0].address'
Deleting containers
The container delete (or container rm) command removes one or more containers.
Delete stopped containers
container delete my-container
Delete multiple containers
container delete container1 container2 container3
Force delete running containers
container delete --force my-running-container
Forcing deletion of a running container will immediately kill it without graceful shutdown.
Delete all containers
Auto-remove with —rm flag
Use the --rm flag when running a container to automatically remove it after it stops:
container run --rm --name temp-container ubuntu:latest echo "hello"
Pruning stopped containers
The container prune command removes all stopped containers to reclaim disk space:
The command outputs the amount of space freed after deletion.
Pruning only affects stopped containers. Running containers are not touched.