Tuesday, December 24, 2024
HomeDockerLifecycle of Docker Container

Lifecycle of Docker Container

Image Source: http://docker-saigon.github.io/post/Docker-Internals/

Docker is a tool which quickly lets you to create light weight VMs with your code and deploy it as fast as possible through different services in various containers. Docker consists of various type of Containers (Docker VM’s) and Docker Hub (Online Docker’s VM sharing service). From within docker we see whole system but from base machine we just see one process running for each docker instance. There is a very smooth way of working inside Docker containers in which a system is built in such a way that developers, testers and Administrators can work together to deploy a code in faster way.

More about Docker Containers: A docker is divided in to three main parts:

1)Docker software which acts as an implementer of VM’s in containers.

2)Containers where Docker deploy your light weight VM’s

3)Docker hub, an online repository of pre-configured docker VM images.

The docker software:

This is essence of whole docker which provide a Docker CLI to access, create, download docker. This will act as middle man between base OS and all the containers which docker runs.

The containers:

The main part of Docker which is widely used is Container. We do have containers before Docker, but Docker people have made it easy and fast process to build new containers (aka Virtual machines)

There is a very big advantage of containers that they can be used almost at every place like Physical machines / Virtual machines / Data Centers / Cloud and many more. So we can easily move our applications along with settings/configurations from one place to another efficiently. Scaling of Docker containers is really an easy process as we can quickly launch more containers when required and shutdown as soon as the work is finished. So it is very easy to do small changes with least risk and higher uptime.

Create container

Create a container to run it later on with required image.

# docker create --name <container-name> <image-name>

Run docker container

Run the docker container with the required image and specified command / process. ‘-d’ flag is used for running the container in background.

# docker run -it -d --name <container-name> <image-name> /bin/bash

What DOCKER RUN Does:

  • Pulls the (centos) image: Docker checks for the presence of the CentOS image and. if it doesn’t exist locally on the host, then Docker downloads it from Docker Hub. If the image already exists, then Docker uses it for the new container.
  • Creates a new container: Once Docker has the image. it uses it to create a container.
  • Allocates a filesystem and mounts a read-write layer: The container is created in the file system and a read-write layer is added to the image.
  • Allocates a network / bridge interface: Creates a network interface that allows the Docker container to talk to the local host.
  • Sets up an IP address: Finds and attaches an available IP address from a pool.
  • Executes a process that you specify: Runs your application, and:
  • Captures and provides application output: Connects and logs standard input, outputs and errors for you to see how your application is running.

Pause container

Used to pause the processes running inside the container.

# docker pause <container-id/name>

Unpause container

Used to unpause the processes inside the container.

# docker unpause <container-id/name>

Start container

Start the container, if present in stopped state.

# docker start <container-id/name>

Stop container

To stop the container and processes running inside the container:

# docker stop <container-id/name>

To stop all the running docker containers

# docker stop $(docker ps -a -q)

Restart container

It is used to restart the container as well as processes running inside the container.

# docker restart <container-id/name>

Kill container

We can kill the running container.

# docker kill <container-id/name>

Destroy container

Its preferred to destroy container, only if present in stopped state instead of forcefully destroying the running container.

# docker rm <container-id/name>

To remove all the stopped docker containers

# docker rm $(docker ps -q -f status=exited)
RELATED ARTICLES
- Advertisment -

Most Popular

Recent Comments