Running Your First Container
Now that Docker is installed, let's run your first container. This hands-on lesson introduces the fundamental docker run command and its essential options.
The docker run Command
The docker run command creates and starts a container from an image:
docker run [OPTIONS] IMAGE [COMMAND] [ARGS]
Running Hello World
Let's start with the simplest example:
docker run hello-world
What happens:
- Docker looks for
hello-worldimage locally - If not found, pulls it from Docker Hub
- Creates a container from the image
- Runs the container
- Container prints a message and exits
Running an Interactive Container
Let's run a more useful container - an Ubuntu shell:
docker run -it ubuntu bash
Breaking down the options:
-i(--interactive): Keep STDIN open-t(--tty): Allocate a pseudo-terminalubuntu: The image namebash: The command to run
You're now inside an Ubuntu container! Try some commands:
# Check the OS
cat /etc/os-release
# List files
ls -la
# Exit the container
exit
Running a Web Server
Let's run Nginx, a popular web server:
docker run -d -p 8080:80 nginx
Breaking down the options:
-d(--detach): Run in background-p 8080:80: Map port 8080 on host to port 80 in containernginx: The image name
Visit http://localhost:8080 in your browser to see Nginx running!
Essential docker run Options
| Option | Short | Description |
|---|---|---|
| --detach | -d | Run in background |
| --interactive | -i | Keep STDIN open |
| --tty | -t | Allocate pseudo-TTY |
| --publish | -p | Publish port (host:container) |
| --name | Assign a name to container | |
| --rm | Remove container when it exits | |
| --env | -e | Set environment variables |
| --volume | -v | Mount a volume |
Naming Containers
Give containers meaningful names for easier management:
# Run with a custom name
docker run -d --name my-nginx -p 8080:80 nginx
# Now you can reference it by name
docker stop my-nginx
docker start my-nginx
docker logs my-nginx
Automatic Cleanup
Use --rm to automatically remove the container when it exits:
# Container is removed after the command completes
docker run --rm ubuntu echo "Hello from container"
# Useful for one-off commands
docker run --rm -it python:3.11 python --version
Environment Variables
Pass environment variables to configure containers:
# Single variable
docker run -e MY_VAR=hello ubuntu env
# Multiple variables
docker run -e DB_HOST=localhost -e DB_PORT=5432 myapp
# From a file
docker run --env-file .env myapp
Running in the Background
For long-running services, use detached mode:
# Start in background
docker run -d --name web -p 80:80 nginx
# Check it's running
docker ps
# View logs
docker logs web
# Follow logs in real-time
docker logs -f web
Working with Container Output
View output from containers:
# View all logs
docker logs container_name
# Follow logs (like tail -f)
docker logs -f container_name
# Last 100 lines
docker logs --tail 100 container_name
# Logs since a timestamp
docker logs --since 2023-01-01 container_name
Executing Commands in Running Containers
Use docker exec to run commands in an existing container:
# Start a container
docker run -d --name mycontainer nginx
# Execute a command
docker exec mycontainer ls -la
# Open an interactive shell
docker exec -it mycontainer bash
# Run as a different user
docker exec -u root mycontainer whoami
Stopping and Removing Containers
# Stop a running container (graceful)
docker stop container_name
# Stop immediately (force)
docker kill container_name
# Remove a stopped container
docker rm container_name
# Stop and remove in one command
docker rm -f container_name
# Remove all stopped containers
docker container prune
Practical Examples
Run a Database
docker run -d \
--name postgres \
-e POSTGRES_PASSWORD=mysecretpassword \
-p 5432:5432 \
postgres:15
Run Redis
docker run -d \
--name redis \
-p 6379:6379 \
redis:alpine
Run a One-off Command
# Run Node.js script without installing Node
docker run --rm -v $(pwd):/app -w /app node:18 node script.js
# Run Python script
docker run --rm -v $(pwd):/app -w /app python:3.11 python script.py
Common Mistakes
Forgetting to expose ports
# Wrong - can't access the web server
docker run -d nginx
# Correct - accessible on localhost:8080
docker run -d -p 8080:80 nginx
Running interactive containers in detached mode
# Container exits immediately
docker run -d ubuntu
# Correct - use -it for interactive shells
docker run -it ubuntu bash
Key Takeaways
docker runcreates and starts containers from images- Use
-dfor background services,-itfor interactive sessions - Port mapping (
-p host:container) exposes container services - Name containers with
--namefor easier management - Use
--rmfor one-off commands to avoid container buildup docker execruns commands in existing containers- Always expose necessary ports when running services

