Skip to content

Working with Containers ​

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.

Run a container ​

Command format:

sh
docker run --name <container-name> -p <local-port>:<container-port> <image:tag>

Example:

sh
docker run --name web -p 8080:80 nginx:1.27

What happens:

  1. Docker Engine checks whether nginx:1.27 is already on the machine. If not, the Engine pulls it from a registry.
  2. A container named web is created from that image.
  3. Port 80 inside the container is mapped to port 8080 on your laptop.

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:

sh
docker run -d --name <container-name> -p <local-port>:<container-port> <image:tag>

Example:

sh
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.

List containers ​

To see running containers, use docker ps:

sh
docker ps

Example output:

text
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

docker ps only shows running containers. To see all containers, including stopped ones:

sh
docker ps -a

Example output:

text
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-web

The columns you will often check are NAMES, STATUS, and PORTS. Output may differ slightly depending on your Docker version and terminal width.

Container logs ​

To see output from the process running inside a container, use docker logs:

sh
docker logs web

This is similar to viewing the app's standard output.

Example output:

text
/docker-entrypoint.sh: Configuration complete; ready for start up
2026/08/23 08:00:12 [notice] 1#1: start worker processes

To follow new log entries live, use the -f option:

sh
docker logs -f web

Run a command inside a container ​

The exec command asks Docker Engine to run a command inside a running container. This is useful for checking the container while debugging:

sh
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.

Stop, restart, and remove a container ​

Command format for stopping a container:

sh
docker stop <container-name-or-id>

Example:

sh
docker stop web

The 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:

sh
docker start <container-name-or-id>

Example:

sh
docker start web

Docker Engine starts the same container again. Its image, name, port mapping, and volume configuration remain in place.

Command format for removing a container:

sh
docker rm <container-name-or-id>

Example:

sh
docker rm web

rm 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:

sh
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.