SkillSprout
Chapter 1 · Lesson 4
Beginner
2 min read

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

bash
docker run -d --name my-nginx -p 8080:80 nginx
  • docker: the Docker CLI
  • run: 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 80
  • nginx: 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

bash
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

  1. Run Nginx on port 8080.
  2. Visit the page.
  3. Read the logs.
  4. Stop and remove the container.
  5. Confirm it is gone with docker ps -a.
Container lifecycle
Architecture
ImageblueprintRunningwritable layerStoppedRemoveddocker rundocker stopdocker rm
Key point. Docker creates the container from an image.

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