The life cycle of a container

The life cycle of a container
5 (100%) 5 votes

If you working in a DevOps or SRE environment, it is very rare to find a person who has not heard of the name containers. In this article, we talk about the life cycle of a container.

For this purpose, we will use docker for a container runtime. Let’s have a look at the docker commands first.

docker build:

This is used to build the docker image and then put it to the image registry.

docker pull:

This is used to pull the image build in the above portion from the registry.

docker run:

This will run the image as a docker container

docker pause:

It is used to pause the docker container.

docker unpause:

It is used to unpause the docker container.

docker stop:

Stops the docker container

docker start:

Starts back the docker container.

docker kill:

Kills the docker container.

The life cycle of a container:

This starts with the container image being build and pushed to the image registry. Next step that comes is pulling a container image where you want to deploy the container.

Next, you deploy the container using docker run. Once the container is started you can either pause the container and then you can resume the container.

After your work is complete either your container exits with a status code or you can kill the container from outside using docker kill.

life cycle of a container

Let’s look at steps one by one.

Building an image.

To build an image you need to create a Dockerfile. In this file, you describe what your containers look like and what packages you have to install in it. Example of a Dockerfile is below.

#
# Nginx Dockerfile
#
# https://github.com/dockerfile/nginx
#

# Pull base image.
FROM dockerfile/ubuntu

# Install Nginx.
RUN \
  add-apt-repository -y ppa:nginx/stable && \
  apt-get update && \
  apt-get install -y nginx && \
  rm -rf /var/lib/apt/lists/* && \
  echo "\ndaemon off;" >> /etc/nginx/nginx.conf && \
  chown -R www-data:www-data /var/lib/nginx

# Define mountable directories.
VOLUME ["/etc/nginx/sites-enabled", "/etc/nginx/certs", "/etc/nginx/conf.d", "/var/log/nginx", "/var/www/html"]

# Define working directory.
WORKDIR /etc/nginx

# Define default command.
CMD ["nginx"]

# Expose ports.
EXPOSE 80
EXPOSE 443

This is an example of Nginx image. Once you build the image you can push it to image registry. Use the below command to build the image.

docker build -t nginx:latest

Push to an image registry

After building the image you push it to the image registry like Dockerhub or Artifactory. After that everyone can access the image from that registry and deploy the containers. You can use docker push to push to the repository. Use below command to push to docker hub. Be sure you have to create a username and then you can push.

docker push registry:port/username/repo_name

Pull an image from the registry and run the image.

Next where-ever we want to deploy the image we will pull the image there and deploy it using docker run. You can use the below commands for the same.

docker pull registry:port/username/repo_name
docker run repo_name

Now you can see your docker container running if you run the below command.

docker ps
# for docker stats you can run
docker stats

Container exit

Once you work is complete your container will die or the other way you have to kill the container using docker kill command.

Recommended books for devops and linux admin

This was the life cycle of a container. You can read more about docker on docker documentation.

If you like the article please share and subscribe. Also, read about more DevOps articles here.


Gaurav Yadav

Gaurav is cloud infrastructure engineer and a full stack web developer and blogger. Sportsperson by heart and loves football. Scale is something he loves to work for and always keen to learn new tech. Experienced with CI/CD, distributed cloud infrastructure, build systems and lot of SRE Stuff.

Leave a Reply

Your email address will not be published. Required fields are marked *

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