Run and Manage Your First Container
Overview
The Docker CLI is how you send instructions to Docker. The command docker run nginx means: ask Docker to create a container from the nginx image. nginx is a popular web server image, and Docker downloads it from Docker Hub if it is not already available.
Containers often stop after their main process exits. A web server stays running because it continues to listen for requests. A one-time command exits after it finishes.
Detailed Notes
Run Nginx
docker run -d --name my-nginx -p 8080:80 nginx
docker: the Docker CLIrun: create a container and start it-d: run in the detached background--name my-nginx: assign a readable name-p 8080:80: map host port 8080 to container port 80nginx: image name
Open http://localhost:8080. The host receives the request on port 8080 and forwards it to Nginx on port 80 inside the container.
Lifecycle commands
docker ps
docker ps -a
docker logs my-nginx
docker exec -it my-nginx sh
docker stop my-nginx
docker start my-nginx
docker restart my-nginx
docker rm my-nginx
-i keeps input open and -t gives a terminal interface. sh starts a shell if the image includes one. Remove a running container with docker rm -f my-nginx.
Exercise
- Run Nginx on port 8080.
- Visit the page.
- Read the logs.
- Stop and remove the container.
- Confirm it is gone with
docker ps -a.
A container moves through create, running, stopped and removed states.
Common Interview Questions
Finished this lesson?
0 of 27 lessons done · 0% of the course

