Docker
10%
Course progress
2 of 31 topics done
Keep going, you are close!
3 topics in a row
2 of 31 topics done
LessonΒ·3 of 31

Run

N/A31 lessons
1

Run

🐳 Running Containers in Docker

Docker RunContainer ExecutionModesOperationsLevelDuration

πŸ“‹ Overview

🎯 Master Docker Container Execution Modes

This guide explores the docker run command 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

Command OverviewFundamentals

The docker container run command combines the process of creating and starting a container. It does the following:

  1. Creates a writable container layer over the specified image
  2. Starts the container using the specified command
  3. Executes the command inside the container
  4. Manages the container lifecycle

πŸ“Š Basic Syntax

bash
docker container run [OPTIONS] IMAGE [COMMAND] [ARG...]

or

bash
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

πŸ”‘ Key Components

ComponentDescription
OPTIONSFlags that modify container behavior
IMAGEImage to use for creating the container
COMMANDCommand to execute inside container
ARGArguments for the command

πŸ”„ Running a Container in Detached Mode

Detached ModeBackground Execution

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

bash
docker container run -d [OPTIONS] IMAGE [COMMAND]

πŸ“Œ Example: Run Ubuntu Container in Background

bash
docker container run -d ubuntu:20.04 sleep 30

πŸ“Š Sample Output

text
0fbc31ed81ef0f25d245b32d040389f640ce95c58f2d541e9f81bf733e84dcde

The output is the Container ID, which uniquely identifies the running container.

πŸ” What Happens

  1. Container is created from the Ubuntu image
  2. Container starts in the background
  3. Command sleep 30 executes for 30 seconds
  4. Container stops automatically after command completes
  5. 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

bash
docker container run -d -p 80:80 nginx

This runs Nginx in the background and maps port 80.


πŸ’» Running a Container in Interactive Mode

Interactive ModeTerminal Access

Docker provides the -it option, which allows users to run containers in interactive mode with access to a terminal.

πŸ“Œ Syntax

bash
docker container run -it [OPTIONS] IMAGE [COMMAND]

πŸ“Œ Example: Interactive Ubuntu Shell

bash
docker container run -it ubuntu:20.04 /bin/bash

πŸ“Š Sample Output

text
root@ad9f71ce3a04:/#

You're now inside the container with a shell prompt.

πŸ” What Happens

  1. Container is created from the Ubuntu image
  2. Container starts and connects to your terminal
  3. You get a shell prompt inside the container
  4. You can execute commands interactively
  5. Terminal is attached to the container

πŸ’» Example Commands Inside Container

bash
# 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

FlagFull NamePurpose
-i--interactiveKeep STDIN open even if not attached
-t--ttyAllocate a pseudo-terminal
-itCombinedInteractive 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

Exit OptionsContainer Control

Option 1: Exit and Stop Container

bash
root@ad9f71ce3a04:/# exit

This exits the container and stops it.

Option 2: Detach Without Stopping

bash
[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

bash
[Ctrl + D]

This also exits and stops the container.

πŸ“Š Comparison

MethodContainer StatusUse Case
exitStopsWhen done with container
Ctrl+P Ctrl+QKeeps RunningWhen you want to detach temporarily
Ctrl+DStopsQuick exit

πŸ“Œ Common Docker Run Options

OptionsFlags

🏷️ Naming and Identification

bash
# Name the container
docker run --name my-container ubuntu

# Assign hostname
docker run --hostname myhost ubuntu

πŸ”Œ Port Mapping

bash
# 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

bash
# 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

bash
# Mount host directory to container
docker run -v /host/path:/container/path ubuntu

# Mount named volume
docker run -v myvolume:/data ubuntu

πŸ’» Resource Limits

bash
# 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

bash
# Always restart
docker run --restart=always nginx

# Restart on failure
docker run --restart=on-failure:5 ubuntu

πŸ“Š Practical Examples

ExamplesReal World

Example 1: Run Nginx Web Server

bash
docker run -d -p 80:80 --name webserver nginx
  • -d: Run in background
  • -p 80:80: Map port 80
  • --name webserver: Name the container
  • nginx: Use Nginx image

Example 2: Run MySQL Database

bash
docker run -d \
  --name mysql-db \
  -e MYSQL_ROOT_PASSWORD=password \
  -p 3306:3306 \
  mysql:8.0

Example 3: Interactive Python Shell

bash
docker run -it python:3.9 python

Example 4: Run with Volume Mount

bash
docker run -it \
  -v /home/user/data:/data \
  ubuntu:20.04 \
  /bin/bash

βœ… Best Practices

Best PracticesStandards
  • Use Specific Tags: Always specify image versions
  • Name Containers: Use --name for 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

Navigation

⬅️ Previous: First Container

πŸ“ Current Location: Docker Run (You are here)

➑️ Next Step: Docker Start

πŸ—ΊοΈ Full Path: Installation β†’ Run β†’ Run β†’ Start β†’ Inspect


πŸ“š Additional Resources

ResourcesDocumentation

Docker Run Mastery

πŸŽ‰ Excellent! Docker Run Complete! πŸŽ‰

➑️ Continue to Docker Start