- English
- English
Appearance
Appearance
Now it is time to practice. You will run Nginx in a container, check its status, view its logs, run a command inside it, and then stop or remove it.
Every command you type through Docker CLI is sent to Docker Engine. The Engine creates and runs the container. Make sure Docker Desktop or Docker Engine is running.
Command format:
docker run --name <container-name> -p <local-port>:<container-port> <image:tag>Example:
docker run --name web -p 8080:80 nginx:1.27What happens:
nginx:1.27 is already on the machine. If not, the Engine pulls it from a registry.web is created from that image.Open http://localhost:8080 in a browser. You should see the default Nginx page.
Without -p, the container can still run, but you cannot access it from a browser. Port mapping uses the host:container format.
Your terminal is blocked because Nginx is running in the foreground. Stop it with Ctrl+C.
In daily work, containers are usually run in the background, or detached, so the terminal remains available:
Command format:
docker run -d --name <container-name> -p <local-port>:<container-port> <image:tag>Example:
docker run -d --name web -p 8080:80 nginx:1.27-d means detached. The terminal becomes available again while the container keeps running.
TIP
The name in --name web must be unique. If you see a “name already in use” error, the old container still exists. See the stop, restart, and remove sections below.
To see running containers, use docker ps:
docker psExample output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a1b2c3d4e5f6 nginx:1.27 "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 0.0.0.0:8080->80/tcp webdocker ps only shows running containers. To see all containers, including stopped ones:
docker ps -aExample output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a1b2c3d4e5f6 nginx:1.27 "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 0.0.0.0:8080->80/tcp web
f6e5d4c3b2a1 nginx:1.27 "/docker-entrypoint.…" 5 minutes ago Exited (0) 3 minutes ago old-webThe columns you will often check are NAMES, STATUS, and PORTS. Output may differ slightly depending on your Docker version and terminal width.
To see output from the process running inside a container, use docker logs:
docker logs webThis is similar to viewing the app's standard output.
Example output:
/docker-entrypoint.sh: Configuration complete; ready for start up
2026/08/23 08:00:12 [notice] 1#1: start worker processesTo follow new log entries live, use the -f option:
docker logs -f webThe exec command asks Docker Engine to run a command inside a running container. This is useful for checking the container while debugging:
docker exec -it web sh-it makes the session interactive. Type exit to return to your laptop's terminal.
You do not need to “SSH into a container” every time. logs and exec are useful when an app is not responding, an environment variable is wrong, or a file cannot be found.
Command format for stopping a container:
docker stop <container-name-or-id>Example:
docker stop webThe stop command asks Docker Engine to stop the process cleanly. The stopped container remains available and can be started again.
Command format for starting a stopped container:
docker start <container-name-or-id>Example:
docker start webDocker Engine starts the same container again. Its image, name, port mapping, and volume configuration remain in place.
Command format for removing a container:
docker rm <container-name-or-id>Example:
docker rm webrm removes the container, not the image. The container must be stopped before you can remove it.
To stop and remove a container in one command:
docker rm -f web-f tells Docker Engine to stop the container if it is still running, then remove it.
When a container is removed, its filesystem is removed too. Data that only exists inside the container is lost. The next lesson explains how to store data using Docker Volume.