DEV Community

Gabriel Usen
Gabriel Usen

Posted on

How to run, stop and remove containers in Docker

Introduction

Docker is an open-source platform that allows developers to build, run, and deploy applications in containers. Containers are a lightweight alternative to virtual machines, making it easy to run applications in isolated environments with consistent configurations. So we would be learning and writing basic docker commands

Let's Start

If you don't have them installed yet, install Docker Desktop, then we begin the docker exercise.

* Run a container using the latest nginx image

Firstly, open up a terminal or gitbash and run a container using the latest nginx image.

The first step in working with containers in Docker is to run a container. To do this, we can use the "docker run" command and specify the name of the image we want to use. In this case, we will use the latest nginx image:

docker run -d nginx:1.23.4
Enter fullscreen mode Exit fullscreen mode

The above command tells Docker to run a container in detached mode [-d] with the image nginx v1.23.4

* List the containers to make sure the container is running

After successfully running a container, it's a good practice to list the running containers to ensure if the container is running properly. We can do this using the "docker ps" command:

docker ps
Enter fullscreen mode Exit fullscreen mode

* Run another container but this time use ubuntu latest and attach to the terminal of the container

Now we are done with running and verifying our containers, let's run another container, this time using the latest ubuntu image v22.04 jammy and attaching to the terminal of the container:

docker run -it ubuntu:22.04 /bin/bash
Enter fullscreen mode Exit fullscreen mode

The above command tells Docker to run a container in interactive mode (-it). We also specify that we want to attach to the terminal of the container by running the /bin/bash command.

* List again the containers. How many containers are running?

After running our second container, let's list the running containers again to see if it's currently running:

docker ps
Enter fullscreen mode Exit fullscreen mode
CONTAINER ID   IMAGE          COMMAND                  CREATED       STATUS       PORTS     NAMES
1a8b7cc25f98   ubuntu:22.04   "/bin/bash"              2 hours ago   Up 2 hours             epic_lamport
356b44e343a1   nginx:1.23.4   "/docker-entrypoint.…"   2 hours ago   Up 2 hours   80/tcp    quirky_volhard

Enter fullscreen mode Exit fullscreen mode

The above command will show us a list of all the running containers on our system, including container ID, Images, Command, Created and also Names of the container.

* Stop containers

So now let's stop the container, we can use the "docker stop" command and specify id of the container. For example, to stop our above first container, we can run the following command:

docker stop 1a8b7cc25f98
Enter fullscreen mode Exit fullscreen mode

and now on the second container with the second container ID

docker stop 356b44e343a1
Enter fullscreen mode Exit fullscreen mode

* Remove Container

Once the containers are stopped, we can remove them using the "docker rm" command and specifying the ID of the container we want to remove. For example, to remove the ubuntu container

docker rm 1a8b7cc25f98
Enter fullscreen mode Exit fullscreen mode

similar to the first one, nginx container ID will be used

docker rm 356b44e343a1
Enter fullscreen mode Exit fullscreen mode

In conclusion, using Docker to run, stop, and remove containers is straightforward and easy. By following these instructions, developers can easily manage and deploy containers, allowing them to work more efficiently and effectively.

Top comments (0)