Run
π³ Running Containers in Docker
π Overview
π― Master Docker Container Execution Modes
This guide explores the
docker runcommand and different modes for running containers, including detached and interactive modes.
π What This Guide Covers
- π Docker Run Command - Creating and starting containers
- π Detached Mode - Running containers in background
- π» Interactive Mode - Running containers with terminal access
- π Port Mapping - Exposing container ports
- π Environment Variables - Setting container environment
- π Volume Mounting - Attaching storage to containers
- β Best Practices - Container execution guidelines
π Docker Run Command Overview
The docker container run command combines the process of creating and starting a container. It does the following:
- Creates a writable container layer over the specified image
- Starts the container using the specified command
- Executes the command inside the container
- Manages the container lifecycle
π Basic Syntax
docker container run [OPTIONS] IMAGE [COMMAND] [ARG...]
or
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
π Key Components
| Component | Description |
|---|---|
| OPTIONS | Flags that modify container behavior |
| IMAGE | Image to use for creating the container |
| COMMAND | Command to execute inside container |
| ARG | Arguments for the command |
π Running a Container in Detached Mode
To run a container in the background, we use the -d option (detach mode). This allows the container to run independently of the terminal.
π Syntax
docker container run -d [OPTIONS] IMAGE [COMMAND]
π Example: Run Ubuntu Container in Background
docker container run -d ubuntu:20.04 sleep 30
π Sample Output
0fbc31ed81ef0f25d245b32d040389f640ce95c58f2d541e9f81bf733e84dcde
The output is the Container ID, which uniquely identifies the running container.
π What Happens
- Container is created from the Ubuntu image
- Container starts in the background
- Command
sleep 30executes for 30 seconds - Container stops automatically after command completes
- Terminal returns immediately to your prompt
π‘ Use Cases for Detached Mode
- Running web servers (Nginx, Apache)
- Running databases (MySQL, PostgreSQL)
- Running background services
- Running long-running processes
- Running multiple containers simultaneously
π Example: Run Nginx Web Server
docker container run -d -p 80:80 nginx
This runs Nginx in the background and maps port 80.
π» Running a Container in Interactive Mode
Docker provides the -it option, which allows users to run containers in interactive mode with access to a terminal.
π Syntax
docker container run -it [OPTIONS] IMAGE [COMMAND]
π Example: Interactive Ubuntu Shell
docker container run -it ubuntu:20.04 /bin/bash
π Sample Output
root@ad9f71ce3a04:/#
You're now inside the container with a shell prompt.
π What Happens
- Container is created from the Ubuntu image
- Container starts and connects to your terminal
- You get a shell prompt inside the container
- You can execute commands interactively
- Terminal is attached to the container
π» Example Commands Inside Container
# Check hostname
root@ad9f71ce3a04:/# hostname
ad9f71ce3a04
# Check OS information
root@ad9f71ce3a04:/# cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04.3 LTS (Focal Fossa)"
# List files
root@ad9f71ce3a04:/# ls -la
total 72
drwxr-xr-x 1 root root 4096 Jan 10 12:34 .
drwxr-xr-x 1 root root 4096 Jan 10 12:34 ..
# Install a package
root@ad9f71ce3a04:/# apt-get update
root@ad9f71ce3a04:/# apt-get install -y curl
π Flag Breakdown
| Flag | Full Name | Purpose |
|---|---|---|
-i | --interactive | Keep STDIN open even if not attached |
-t | --tty | Allocate a pseudo-terminal |
-it | Combined | Interactive terminal session |
π‘ Use Cases for Interactive Mode
- Debugging containers
- Exploring container filesystem
- Testing applications
- Learning Docker
- Development and testing
- Running one-off commands
π Exiting Interactive Mode
Option 1: Exit and Stop Container
root@ad9f71ce3a04:/# exit
This exits the container and stops it.
Option 2: Detach Without Stopping
[Ctrl + P] [Ctrl + Q]
This detaches from the container without stopping it. The container continues running in the background.
Option 3: Exit with Ctrl+D
[Ctrl + D]
This also exits and stops the container.
π Comparison
| Method | Container Status | Use Case |
|---|---|---|
exit | Stops | When done with container |
Ctrl+P Ctrl+Q | Keeps Running | When you want to detach temporarily |
Ctrl+D | Stops | Quick exit |
π Common Docker Run Options
π·οΈ Naming and Identification
# Name the container
docker run --name my-container ubuntu
# Assign hostname
docker run --hostname myhost ubuntu
π Port Mapping
# Map port 8080 on host to 80 in container
docker run -p 8080:80 nginx
# Map multiple ports
docker run -p 8080:80 -p 443:443 nginx
π Environment Variables
# Set environment variable
docker run -e MYSQL_ROOT_PASSWORD=password mysql
# Set multiple variables
docker run -e VAR1=value1 -e VAR2=value2 ubuntu
πΎ Volume Mounting
# Mount host directory to container
docker run -v /host/path:/container/path ubuntu
# Mount named volume
docker run -v myvolume:/data ubuntu
π» Resource Limits
# Limit memory
docker run -m 512m ubuntu
# Limit CPU
docker run --cpus="0.5" ubuntu
# Limit both
docker run -m 512m --cpus="0.5" ubuntu
π Restart Policy
# Always restart
docker run --restart=always nginx
# Restart on failure
docker run --restart=on-failure:5 ubuntu
π Practical Examples
Example 1: Run Nginx Web Server
docker run -d -p 80:80 --name webserver nginx
-d: Run in background-p 80:80: Map port 80--name webserver: Name the containernginx: Use Nginx image
Example 2: Run MySQL Database
docker run -d \
--name mysql-db \
-e MYSQL_ROOT_PASSWORD=password \
-p 3306:3306 \
mysql:8.0
Example 3: Interactive Python Shell
docker run -it python:3.9 python
Example 4: Run with Volume Mount
docker run -it \
-v /home/user/data:/data \
ubuntu:20.04 \
/bin/bash
β Best Practices
- Use Specific Tags: Always specify image versions
- Name Containers: Use
--namefor easy identification - Set Resource Limits: Prevent containers from consuming all resources
- Use Volumes: For persistent data storage
- Set Environment Variables: For configuration management
- Use Health Checks: Monitor container health
- Clean Up: Remove stopped containers regularly
- Security: Don't run as root unless necessary
- Logging: Configure proper logging
- Documentation: Document container configurations
π Navigation
β¬ οΈ Previous: First Container
π Current Location: Docker Run (You are here)
β‘οΈ Next Step: Docker Start
πΊοΈ Full Path: Installation β Run β Run β Start β Inspect
π Additional Resources
- π Docker Run Reference: docs.docker.com/engine/reference/commandline/run
- π Container Lifecycle: docs.docker.com/engine/reference/commandline/container
- π‘ Docker Best Practices: docs.docker.com/develop/dev-best-practices
π Excellent! Docker Run Complete! π