A Docker container is a running instance of a Docker image.
- Image = Blueprint
- Container = Running application
Let’s practice using the nginx image.
docker create --name mynginx nginx
Check:
docker ps -a
Status → Created
docker start mynginx
Check:
docker ps
Status → Up (Running)
docker pause mynginx
Status → Paused
docker unpause mynginx
docker stop mynginx
Status → Exited
docker restart mynginx
docker kill mynginx
Forcefully stops the container.
docker rm mynginx
Container is completely deleted.
-d → Run in background
-p → Map port
docker run -d -p 8080:80 --name webserver nginx
Format:
-p HOST_PORT:CONTAINER_PORT
Example:
-p 8080:80
- 8080 → Host Port (your computer)
- 80 → Container Port (inside container)
What happens?
Browser → http://localhost:8080
⬇
Host Port 8080
⬇
Container Port 80
⬇
Nginx running inside container
So:
Left side = Host
Right side = Container
Environment variables are used to pass configuration values into a container.
Use -e to pass environment variables.
docker run -e APP_ENV=production nginx
This sets:
APP_ENV=production inside the container.
docker run -e USERNAME=admin -e PASSWORD=1234 nginx
Run container:
docker run -d --name envtest -e MY_NAME=Docker nginx
Enter container:
docker exec -it envtest /bin/sh
Check variable:
echo $MY_NAME
Output:
Docker
Environment variables are commonly used for:
- Database credentials
- API keys
- Application configuration
- Environment settings (dev, test, prod)
docker logs webserver
Shows output logs of the container.
docker logs -f webserver
Shows live logs (Press Ctrl + C to exit).
docker exec -it webserver /bin/sh
Now you are inside the container.
Try:
ls / cd /usr/share/nginx/html ls
Type exit to leave.
docker exec webserver ls /
Runs one command without entering.
docker inspect webserver
Shows detailed information like:
- Container ID
- IP address
- Port mappings
- Environment variables