Skip
Arish's avatar

8. Container Lifecycle


Container States

Created → Running → Paused → Stopped → Removed

Lifecycle Commands

bash
1# Create (don't start)
2docker create --name mycontainer nginx
3
4# Start
5docker start mycontainer
6
7# Pause
8docker pause mycontainer
9
10# Unpause
11docker unpause mycontainer
12
13# Stop (graceful)
14docker stop mycontainer
15
16# Kill (force)
17docker kill mycontainer
18
19# Remove
20docker rm mycontainer

Auto-Remove

bash
1# Remove after exit
2docker run --rm nginx

Restart Policies

bash
1# Always restart
2docker run -d --restart=always nginx
3
4# Restart on failure
5docker run -d --restart=on-failure:3 nginx
6
7# Unless stopped manually
8docker run -d --restart=unless-stopped nginx

Container Resource Limits

bash
1docker run -d \
2  --memory=512m \
3  --cpus=0.5 \
4  nginx