Tuesday, August 26, 2025

Docker Commands to Run Containers

In this section, we see the docker commands for creating, stopping, restarting and deleting containers

TOC:

1 Check for existing containers and images

The Docker commands to list existing containers and images in your local Docker environment

You can use the docker ps command to list any running container

$ docker ps
CONTAINER ID      IMAGE     COMMAND    CREATED     STATUS    PORTS     NAMES

You can use the docker images command to list local images

$ docker images  
REPOSITORY        TAG         IMAGE ID       CREATED     SIZE

You’re on a clean system with no pre-existing containers or images.

2 Run a container

Docker command to run a new container

The docker run command creates and starts a new container from a specified image. If the image is not already present on the local system, Docker automatically pulls it from a registry (such as Docker Hub).

Basic Syntax

docker run [OPTIONS] <image>:<tag>

What happens when you run docker run

  1. Image retrieval Docker checks if the specified image (<image>:<tag>) exists in the local image cache. If not, Docker automatically pulls it from Docker Hub (or another registry), unpacks the image layers and sets them up as the container’s filesystem.

  2. Container creation (docker create) A new container object is created from the image. It has its own ID, configuration, filesystem, and network settings.

  3. Container start (docker start) Docker starts the container by running the default command CMD defined in the image (or overridden by the user).

  4. Application accessibility If the container exposes services, you can map its ports to your host using the -p flag to make them accessible. This makes the service available on the host machine’s port and accessible from any client (local and external clients) that can connect to the host.

Common Options

  • -d Run the container in detached mode (in the background).

  • -p <host_port>:<container_port> Map a container port to a port on the host machine.

  • --name <container_name> Assign a custom name to the container.

  • -it Run the container interactively with a terminal session attached.

  • --rm Automatically remove the container once it exits.

Note: in day-to-day use, docker run is usually enough. The docker start command is rarely needed, but it can be useful if you want to reuse a container multiple times without recreating it.


Example: the docker run command to create and start a new container from an image

docker run ubuntu echo "hello"

This creates a container from the ubuntu image and runs the command echo "hello" inside it.


Example: the docker run command to pull an image from Docker Hub

$ docker run hello-world

Docker first searches the local image cache. If hello-world isn’t present locally, Docker will fetch it from Docker Hub, then run it in a container.


Example: run an Nginx web server container

docker run -d -p 8080:80 --name mynginx nginx:latest

This starts Nginx (nginx:latest) in detached mode (-d), maps container port 80 to host port 8080 (-p 8080:80), and names the container mynginx. You can then open a browser to http://localhost:8080 to access it.


Example: docker run pulls the specified image from Docker Hub, starts a new container, and assigns it the name test.

$ docker run -d --name test -p 5555:80 docker/welcome-to-docker

Docker Command Explanation

  • docker run → starts a new container.
  • -d flag → runs the container in the background (detached mode).
  • -p 5555:80 option → maps port 5555 on the host to port 80 inside the container. This allows you to access the web server running inside the container (isolated network) from your host machine.
    From the host’s perspective, port 80 inside the container is exposed through port 5555 on the host, so any client (e.g., your browser) connecting to port 5555 on the host is redirected to port 80 inside the container.
  • docker/welcome-to-docker → specifies the image to use for creating the container.


shared OS
DOCKER HOST (your machine)
Page 1
https://localhost:5555
CONTAINER (isolated network netspace)
App (NGiNX) listening on port 80
for example 172.17.0.2:80
HOST port 5555
 (published by Docker: - p 5555:80)
NAT (IPTABLES)
docker bridge

Figure: Docker Engine sets up NAT/forwarding so localhost:8080 -> container:80

  • -p 5555:80 publishes host port 5555 to container port 80 via NAT on the docker bridge.
  • both processes are on the same machine; the container just has isolated networking.

Reading the command output to the console

user@host:~$ docker run -d --name test -p 5555:80 docker/welcome-to-docker

Unable to find image 'docker/welcome-to-docker:latest' locally
latest: Pulling from docker/welcome-to-docker
9745203f5d34: Pull complete 
958a74d6a238: Pull complete 
828fa206d77b: Pull complete 
bdaad27fd04a: Pull complete 
9824c27679d3: Pull complete 
fd372c3c84a2: Pull complete 
c1d2dc189e38: Pull complete 
a5585638209e: Pull complete 
Digest: sha256:c4d56c24da4f009ecf8352146b43497fe78953edb4c679b841732beb97e588b0
Status: Downloaded newer image for docker/welcome-to-docker:latest
f11d30f6726080d517b18cfcdef6096e1890432cdd90c9f7877b537004828715
user@host:~$  

Docker first checks the local image cache to see if an image with the specified name and tag exists. If the image is not found locally, Docker pulls it from the Docker Hub registry, displaying the progress as it downloads each layer. Once the download is complete, Docker shows the image digest, confirms that the image has been successfully downloaded, and finally displays the ID of the newly created container.

Now, you can open your browser to http://localhost:5555/ to test if the app is working

3 Check the container and image
  • issue again the docker ps command to check how many containers are running
  • you can see the container called test based on the specified image and mapping port 5555 to 80
$ docker ps

CONTAINER ID   IMAGE                          PORTS                                             NAMES
1834dd448893   docker/welcome-to-docker   0.0.0.0:5555->80/tcp, [::]:5555->80/tcp    test

then, issue again the docker images command to check how many images are on your local machine

$ docker images

REPOSITORY                    TAG        IMAGE ID        CREATED       SIZE
docker/welcome-to-docker   latest    c4d56c24da4f   4 weeks ago   22.2MB

there is a local copy of the docker/welcome-to-docker image just pulled from Docker Hub.

4 Stopping, restarting and deleting containers
  • To stop a running container: docker stop <container_name>
  • To start a stopped container: docker start <container_name>
  • To delete a running container: docker rm -f <container_name>

Example: stop the container you have created

$ docker stop test
test

This will make the browser not able to load the app anymore

Example: restart the container named test

$ docker start test
test

This will make the app back up and the browser able to load it

Example: delete the container. The -f flag force the termination without giving thr app the 10 seconds to gracefully stop.

$ docker rm test -f
test
  • the -f flag forces the termination without giving thr app the 10 seconds to gracefully stop.
  • the container is removed, but you still have the image on you machine.

No comments:

Post a Comment