How To Run, Stop And Remove Containers In Docker
Photo: Docker

How To Run, Stop And Remove Containers In Docker

In this tutorial, we are going to learn how to run, stop, and remove containers in docker. This post will use the online version of the docker for demonstration purposes. You will also learn how to run single or multiple containers in docker.

How to install Docker?

  • Download *Docker Desktop for Mac* or *Docker Desktop for Windows*.
  • Install Docker Desktop.

Try Play-With-Docker online tool

Instead of installing Docker on your machine, you can try online version of Play-With-Docker for learning purpose.

Play-With-Docker, which is a website where you can run terminals directly from your browser that have Docker installed.

play with docker

What is a Container?

A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another. A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings.

Containers are an abstraction at the app layer that packages code and dependencies together. Multiple containers can run on the same machine and share the OS kernel with other containers, each running as isolated processes in user space. Containers take up less space than VMs (container images are typically tens of MBs in size), can handle more applications and require fewer VMs and Operating systems.

Note: In this tutorial, we are going to use Play-With-Docker online tool to experiment our Docker commands.

How to run a Container?

  1. Start Play-With-Docker online tool and click on Add New Instance button.
run a docker container

Run below command in the terminal window.

$ docker container run -t ubuntu top

You use the docker container run command to run a container with the Ubuntu image by using the top command. The -t flag allocates a pseudo-TTY, which you need for the top command to work correctly.

The docker run command first starts a docker pull to download the Ubuntu image onto your host. After it is downloaded, it will start the container. The output for the running container should look like this:

docker run command

top is a Linux utility that prints the processes on a system and orders them by resource consumption. Notice that there is only a single process in this output: it is the top process itself.

2. Add a new node by clicking Add New Instance  and then ssh from node2 into node1 by using the IP that is listed by node1, for example:

$ ssh

ssh into new node

3. Clear the terminal by typing clear command and get the ID of the running container that you just created.

$ docker container ls

docker container ls

4. Use that container ID to run bash inside that container by using the docker container exec command. Because you are using bash and want to interact with this container from your terminal, use the -it flag to run using interactive mode while allocating a psuedo-terminal:

$ docker container exec -it dde06d8a7633 bash

docker container bash

Using docker container exec with bash is a common way to inspect a Docker container.

5. Get all running processes using ps command.

$ ps -ef

docker ps -ef command

6. You can now clean up the container running the top processes:


How to run multiple containers?

1. Docker Store

The Docker Store is the public central registry for Docker images. Anyone can share images here publicly. The Docker Store contains community and official images that can also be found on the Docker Hub.

When searching for images, you will find filters for Store and Community images. Store images include content that has been verified and scanned for security vulnerabilities by Docker. Go one step further and search for Certified images that are deemed enterprise-ready and are tested with Docker Enterprise Edition.

2. Run NGINX container

Run an NGINX server by using the official NGINX image from the Docker Store in a new node instance window.

$ docker container run --detach --publish 8080:80 --name nginx nginx

docker container run --detech --publish

The --detach flag will run this container in the background. 

The publish flag publishes port 80 in the container (the default port for NGINX) by using port 8080 on your host.

--name flag, which names the container

3. Access NGINX server from browser by using http://localhost:8080 port.

Once you clicked on highlighted button, it will open up an tab in browser with NGINX url.

docker nginx server

4. Run MongoDB container

Create a new instance and run a MongoDB server. You will use the official MongoDB image from the Docker Store. Instead of using the latest tag (which is the default if no tag is specified).

$ docker container run --detach --publish 8081:27017 --name mongo mongo:3.4

docker container run MongoDB

Again, because this is the first time you are running a Mongo container, pull the Mongo image from the Docker Store. You use the --publishflag to expose the 27017 Mongo port on your host. You must use a port other than 8080 for the host mapping because that port is already exposed on your host.

Now, click on 8081 button, and it will open up a tab in your browser window with following message.

docker mongodb

Containers are self-contained and isolated, which means you can avoid potential conflicts between containers with different system or runtime dependencies. For example, you  can deploy an app that uses Java 7 and another app that uses Java 8 on the same host. Or you can run multiple NGINX containers that all have port 80 as their default listening ports. (If you’re exposing on the host by using the --publish flag, the ports selected for the host must be unique.) Isolation benefits are possible because of Linux namespaces.

Stop a container

1. Get all container list by running below command.

$ docker container ls

2. Stop container by their Ids.

$ docker container stop [container_id]

$ docker container stop 671 e9a

Note: First three letter of container id is enough.

Remove the stopped container

$ docker system prune

Remove the stopped containers. The following command removes any stopped containers, unused volumes and networks, and dangling images.

Further Reading

Getting Started With Docker

Docker Introduction With Its Architecture And Components


Hi, I am Adesh. I am a senior software engineer having more than 12 years of software development experience. I am a full stack developer and interested in writing the technical post on programming. I am currently working in New York City area.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.