In the previous tutorial, we went through the steps to get a container up and running. We used Postgres as an example. This section of the tutorial series will continue to build on that. In this section we will learn how to interact with the container, how to execute commands inside a container and

Below are few ways using which you can use a container:

  1. Attach

We are attached to the standard in and out of the container, currently standard out is being sent to output logs. Therefore we do not have access to shell.

You will have to use ‘Ctrl+C’ to detach the container, this will also stop the container

2. Start

Now there is a way to resolve the problem we faced with ‘attach’, that is we should just start the container with the, guess what, ‘start’ command.

docker container start <CONTAINER_ID>

3. Stop

You guessed it, the stop command, contrary to the start command, the arch nemesis if you will, stops the container.

docker container stop <CONTAINER_ID>

4. Logs

This command is used to see any activity that may have happened on your container.

Just like this you can view the logs, up until this moment we have only used start and stop command so only those logs are generated. Logs are a great way of debugging any issues that one might face using containers.

5. Stats

Stats is a great way to live track the resource allocation and resource usage that your container is consuming. If you have created a container, it is important that you track the stats while using it, this will help you find memory leaks, will help you make your container more efficient and so on.

If you are using pre-built containers, it is still a great tool to keep track of your system.

Usage: docker stats <CONTAINER_ID>



To get out of this press ‘Ctrl+C’

6. Exec

This command allows us to run commands on our container. Lets say we want to see directory listing, we will use the ‘ls’ command on the desired directory.

This command is specially useful when we want to get inside our container and execute commands.

For this specific purpose we will be using two flags ‘-i’, which stands for interactive and ‘-t’ which stands for tty.

Here’s how your command will look like:

docker exec -it <CONTAINER_ID> /bin/bash

This will drop us inside our Postgres container, or whichever container you choose to use.

To get out of the container use ‘exit’ command.

7. Pause/Unpause

In docker pause and unpause are commands used on your container to suspends all processes and un-suspend all processes respectively. Usage is fairly simple:

docker container pause <CONTAINER_ID>

docker container unpause <CONTAINER_ID>

8. Removing Containers

There are two ways to remove containers and there are two types of containers to be removed:

  1. docker container rm <CONTAINER_ID>

This command will remove all the containers in stopped state.

If you want to remove containers which are in running state as well, you need the additional ‘-f’ flag, ‘-f’ stands for force, as in forcibly removing a running container.

2. docker container prune

Running this command will remove all the containers in stopped state.

In the upcoming tutorial we will talk about exposing ports and executing commands inside our container and getting a containerized database up and running, stay tuned.


Leave a Reply

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

%d bloggers like this: