Manipulating Containers With Docker Client

Manipulating Containers With Docker Client

Creating And Running A Container From An Image

Why did we use busybox instead of hello-world in this command?

It is because commands like ls, echo etc are present inside the busybox snapshot. On the other hand, hello-world contains only a hello-world program.

Listing All The Running Containers

As there is no container running currently it is empty.

Another command to list all the containers we ever created.

Container Lifecycle

First, we need to prep the container by getting the file system ready using the "docker create" command and then execute the container using "docker start".

For example

The -a flag is used to print all the stuff.

The "docker ps --all" command shows all the containers in a stopped state. These containers are useless and are just taking up space on the hard disk. So we need to remove these stopped containers.

using "docker system prune" we can remove all the stopped containers.

Retrieving Log Outputs

We use the "docker logs" command to retrieve all the logs/information emitted by a container. This command does not restart or re-create a container.

For example

Stopping Containers

To stop a container we have two options:

  1. Docker stop command

  2. Docker kill command

Stop Container

The stop command sends a sigterm command. The stop command gives some time to the container before shutting down to clean itself.

Kill Container

The kill command issues a Sigkill signal. This shuts down all the processes right away. It does not give the container any time to do some additional work.

Multi-Command Containers

This command helps us to run an additional command inside a container. Let's run Redis.

The -it Flag

The -it flag is used to give inputs to the container and also receive outputs from the container to display on the screen.

Getting Command Prompt In Container

This command gives access to a shell in a Linux environment inside the container.

Container Isolation

There is no system of file sharing between two containers. For example

In this container, we created a new file "hello".

Let's open the container in another prompt to check if the file exists there too.

The "hello" file does not exist in this container. These containers are completely isolated from each other.

Did you find this article valuable?

Support Reuben's blog by becoming a sponsor. Any amount is appreciated!