Container Lifecycle Management: Core CLI Commands
Let us learn the commands used to manage running and stopped containers.
1. Running Containers in Background (Detached Mode)
By default, running a container attaches it to your terminal. To run a container in the background, use the -d (detached) flag:
# Run Nginx in detached mode, mapping host port 8080 to container port 80
docker run -d -p 8080:80 --name my-web-server nginx2. Managing Container States
Use these commands to control your containers:
# List all running containers
docker ps
# List all containers (both running and stopped)
docker ps -a
# View container logs
docker logs my-web-server
# Stop a running container
docker stop my-web-server
# Start a stopped container
docker start my-web-server
# Delete a stopped container
docker rm my-web-server3. Entering a Running Container
To run commands inside a running container, use docker exec with interactive terminal flags (-it):
# Open a bash terminal inside my-web-server container
docker exec -it my-web-server bashPublished on Last updated: