Get Started with Docker Lifecycle

Elliott Saslow
Apr 28 · 11 min read
Docker Lifecycle overview


Today we are going to explore Container Lifecycle Management with Docker. We will work with the basics of containers and work through how to use them directly for different applications. Get started with docker today and understand Docker Container Lifecycle Management.

[Total time: 45min-1hour]

Goals & Objectives:

  • Discuss what is a container and why we might use a Docker Container
  • Explore some of the states that are possible for a Docker Container
  • Go through our technical exercise and learn how to control the lifecycle of a container

Background and why Containers:

A docker container is a system that is very similar to a virtual machine. Imagine that you have separate computer at home that runs all the time, and would like to log into that computer to use it as a server and host your website. You sell some niche clothing on your website, so you also have a database that has all of your customer data. Now, originally, we would set this up on the machine and if we ran out of space or there where too many people visiting the site, just add another computer sitting next to it. You could continue to do this until and as you grow to the size of Ebay or Amazon, your cost continue to scale with the number of computers you have.

This is not an efficient method to solve this problem for a couple of reasons.

  1. Set up of each computer takes a technician
  2. Your traffic and number of users might scale separately
  3. By having each computer you waste a lot of space by running an operating system

So to solve this, Docker came up with a solution that fixes these pain points:

  • Lightweight
  • Universal
  • Easy to Use

We call this solution a container and according to Docker the benefits of using their solution are:

Docker containers that run on Docker Engine:

Standard: Docker created the industry standard for containers, so they could be portable anywhere

Lightweight: Containers share the machine’s OS system kernel and therefore do not require an OS per application, driving higher server efficiencies and reducing server and licensing costs

Secure: Applications are safer in containers and Docker provides the strongest default isolation capabilities in the industry

Exploring Possible Lifecycle States of a Container

Today we are going to explore container lifecycle management. What is lifecycle management? Lifecycle is referring to the state that the container is in i.e. [Is the container running?, Did I pause this container?, When did I stop the Container?]

These are all questions that you should be able to explain an answer to after this tutorial. To begin, lets take a look at the original image for this article:

I want to first explain that there is a difference between Docker and Docker Hub. Docker is the program on your local machine that allows you to use containers. Docker Hub is a remote cloud of different Docker Images that you can use to run a variety of containers. Different Images allow you to optimize for different applications.

An image is essentially built from the instructions for a complete and executable version of an application, which relies on the host OS kernel. When the Docker user runs an image, it becomes one or multiple instances of that container.

For example, there is an image for google tensor flow if you are trying to run a container for machine learning. Or there are images for different web applications. Whatever your needs are, there is a good chance that a container already exists for your needs.

Now, sometimes we want to create a new image, but for now we are going to work with images that have already been created. Because of this we are going to mainly work with the docker run command today instead of creating new images. As you can see in the image, this allows us to skip the created phase of the docker lifecycle. By using the Docker run command we start a container.

At this time the container can now run the specified code you need the container to run, and it will be running this code on your OS system kernel. In the technical exercise you will learn how to identify the current state of your container.

Lets say for right now that the container is running, which means you have 3 major options on what to do next.

  1. Exit
  2. Pause
  3. Kill

Depending on what we want to do next, we can choose what to do with the container.

Example: Exit

Say you have a web application running and you know that with the holiday season ending your website traffic is going to be down for a couple of weeks. You can exit your container, but have it ready to go again if traffic jumps.

Example: Pause

You are working on a data science project and waiting for a model to train. You want to play WoW, but don’t want to be competing for ram with your container. You can easily pause the container, send a email, and then start it up again.

Example: Kill

You are changing the requirements for your server and so have a new Image that you will be running for your container. In this case you want to kill (and then potentially remove) your container from use.

Obviously there are many other and different cases where each stage in the lifecycle might be useful, but by completing the technical exercise, you will be prepared to use containers to your benefit.

Technical Exercise:

Expected Time 45 Minutes

Hint: any command that starts with the dollar sign belongs in your command line

Part 0: Checking Docker installation

To begin, we must check to make sure that docker was installed correctly on your machine. To do this, we are going to run the following command through your command line:

  • $ docker --version If after this command, your terminal printed out a docker version number with a build number, you are good to go!
  • Also make sure you have the Docker application running
  • Otherwise, make sure you have installed docker

Part 1: Running your first container

We will start at the very beginning in using a container — Hello World!

To run a container from a pre-existing image, we will use the run command followed by the image name we would like to run. In this case we are using the pre-existing image of hello-world

  • $ docker run hello-world

The Docker run command will be your bread and butter — you will use this command every time that you want to start any container. After you run this command, you should see the following print out:

Output from command $docker run hello-world output

Lets begin to talk about what just happened (and cover what is going on inside of Docker). By using the run command, we are instructing the computer to look for a specific image on your machine and run this image. Unfortunately, we do not have the image locally, and therefore, Docker looks to DockerHub to identify if there is already an image that could be used in this case. Quickly, if finds that this is the case, and brings the image to your local machine. Its useful to have all of this information, but what other questions could we answer right now?

  • Is the container still running?
  • Is the container still on my local machine?

We will answer this second question by running the following command:

$ docker image ls

This command will return an output of all the docker images that are on your computer. As you can see the hello-world image is still on your computer, but is it running?

To check to see if your container is still running we can use the command: $ docker ps . This command is for listing the containers and could be used with a multitude of flags for custom behavior.

When we run this command, we see that nothing shows up which implies that the container that we created has exited successfully. With that said, lets be robust and make sure that we know that the container is exited successfully.

To do this, we will be running the same command with the -a flag which is a short for --all

  • $ docker ps -a

This will show you all of the containers that you have run and their current status. You will see that your hello-world successfully exited under the column status.

To finish part 1, we will be using a bash command to write out our results to a text file. To do this, we will use the same command as above, but will add the output to a file called test.txt.

To complete this, you must run the list command for docker and pipe the results into the text file:

  1. $ echo Docker Lifecycle Tutorial Part 1 > test.txt
  2. $ docker ps -a >> test.txt
  3. $ printf '%20s\n' | tr ' ' - >> test.txt

Below is an explanation of each command

The last command just adds a horizontal line to the bottom of your test file to separate part 1 and part 2

To check that this worked we can take a peak at what is inside of the ‘test.txt’ file using the following command:

$ cat test.txt

Assuming that this worked, your output should look like the following:

Recap & Cheat Sheet:

## List Docker CLI commands
$ docker
$ docker container --help

## Display Docker version and info
$ docker --version
$ docker version
$ docker info

## Execute Docker image
$ docker run hello-world

## List Docker containers (running, all, all in quiet mode)
$ docker container ls
$ docker container ls --all

Part 2: Life Cycle Management

Starting the Ubuntu Container

Awesome so we are now going to the fun part! We are actually going to create a Container that will have some functional components! We will begin by starting a container in an interactive mode so that we can better understand the fundamental concept of what a container is.

Overview: We will start an unbuntu container and run it in an interactive mode. Underneath the hood, the container behaves exactly like a ubuntu virtual machine with a command line.

This means that we treat the container as if it where a remote computer that has no ability to access your file system. You can look around in the file system to verify this without being afraid of hurting anything! To begin to explore the ubuntu container, user the following command:

  • $ docker run -it ubuntu bash

Whats this command mean?

  • Use the docker library to run in interactive mode The ubuntu image and execute the bash command

You should now have access to the command line from your ubuntu container! You can explore this with your partner using the common commands (i.e. cd,ls,mkdir)

If you want to exit out of it, you can use the following command to detach from the container and return to your working directory:

  • Detach: CTRL + P then CTRL + Q
  • Attach: $ docker attach CONTAINER where you must specify the Container ID

Pro tip: You can give your containers a specific name when using the run command if you pass the flag--name

Pausing the Container

Now that you are out of the container, let go ahead and check the status of the containers running on the computer! Remember how we did that above?

$ docker ps -a

You should see that you have a container that as of a couple of minutes ago was up. This means that you still have that container running in the background. Lets explore the different states that we can put this container in!

The first state we will look at is paused To pause your container, we need one piece of information: the container id. You can easily find this by running the command: $ docker ps -a to list all the current containers and grab the id from the container id column.

Once you have the container id, you can then use this in the following command, and replacing with your container id:

  • $ docker pause <containerid or name>

This pauses all of the running processes that are currently running inside of the container.

  • Try to attach to the paused container! What happens?! Discuss this in your pair or group

Now, we can go ahead and unpause the container with a command that is formatted very similarly

  • $ docker unpause <containerid or name>

On a quick sidenote, it is easy to name your container with a specific convention and then use this instead of the id. This is passed as a flag during the docker run command with the flag --name

Stopping / Starting a Container

You will begin to notice that many of these commands are in the same format.

To stop the container and processes running inside the container:

  • $ docker stop <container-id/name>

To start a container, you can use the generic run method that we have used above or you may use this command:

  • $ docker start <container-id/name>

Even when a container is stopped, it is still staking up a mall amount of resources. That is why we will learn to remove them later on.


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

$ docker restart <container-id/name>


We can kill the running container.

$ docker kill <container-id/name>


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

$ docker rm <container-id/name>

This removes any evidence of the container on your machine allowing you to easily enjoy speed increases while the information is hosted elsewhere.

At any time we can check where the container is in the lifecycle by using the command docker ps -a which we used above multiple times.

Play around with these different commands and talk in your pairs about why you may want to use each of these states.

Removal of all containers

To remove all the stopped docker containers from this exercise use the following command. Be careful this will remove all exited containers on your machine

$ docker rm $(docker ps -q -f status=exited)


Please provide feedback on how this class went & your experience with Docker today.

Sign up for the future vision newsletter to get more great tutorials like this on in your inbox! Also if you enjoyed the article, be sure to give some claps.

Future Vision

A publication centered around high quality storytelling

Thanks to Keenan Olsen

Elliott Saslow

Written by

Future Vision

A publication centered around high quality storytelling

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade