Get startedGet started for free

Working with Docker containers

1. Working with Docker containers

Now that we know how to run containers in various ways, interactively or in the background. It is time to learn some commands that will simplify our life when working with containers.

2. Listing containers

When there are only a few containers, it's easy to find them in the list that docker ps returns. However, if you're working with lots of containers, it can quickly become challenging to identify the right one.

3. Named containers

To solve this, the docker run command has a name flag that allows us to name a container. The name then shows up in the last column of the docker ps output. Here we created a container from the Postgres image and called it db underscore pipeline underscore v1. For any commands that require us to specify a container, we can use either the container-id or container name. For example, in exactly the same way we could stop containers using their id, we can use their name in docker stop.

4. Filtering running containers

When using docker ps with so many containers that even naming them doesn't allow us to find them. We can use docker ps with the f, for filter flag to find a specific container. For example, docker ps dash f followed by, in quotes, name equals sign db underscore pipeline underscore v1. This will show you only the details of containers with the name you specified in the filter.

5. Container logs

Now that we know how to find our running containers, it will also be useful to see their output, for example, to debug any issues. To look at the output a container has generated, we can use the docker logs command followed by the container id. Most containers quickly generate a lot of output, so you will often have to scroll through the result of docker logs to find what you're looking for.

6. Live logs

If instead, you want to follow the logs your container is generating in real-time, you can use docker logs together with the f, for follow, flag. You will see any logs the container generates live. Even though docker ps also has a f flag, the docker ps flag allows us to filter. When working with docker logs instead, the f flag has another effect, allowing us to follow a container its logs. After using docker logs, you will see the output of a running container until either the end of the logs or until you press control plus c to exit the log view.

7. Cleaning up

Previously, we learned how to stop containers. However, a stopped container is not fully gone; the stopped container still exists and is occupying some space on our hard drive. To fully remove an already stopped container, for example, because we want to reuse its name, we use docker container rm followed by the container-id to remove the container.

8. Summary of new commands

Here is a summary of the new commands we just saw that you can refer back to when completing the exercises.

9. Let's practice!

Now that we have learned a bunch of new commands, let's get some practice with them.