Managing Docker Containers

30 minutes
  • 4 Learning Objectives

About this Hands-on Lab

In the modern environment, being proficient at managing containers is an essential skill. In this activity, you will use Docker container management skills to start and stop containers based on images, add and remove images and containers, and monitor and update containers. Completing the lab will demonstrate basic Docker container management skills.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Practice Docker by Running the hello-world and busybox Images

If docker is installed and the docker.service enabled, then a great way to test the ability to run a container is to use the docker subcommand run with a simple image like hello-world. The busybox image is another simple image that we will pull and run a command in. The ps subcommand is useful for viewing running or all containers, and images will list the images in the local repository. Using the rm subcommand we can remove containers and with rmi images.

sudo -i
docker run hello-world
docker run --name hi hello-world
docker search busybox
docker pull docker.io/busybox
docker run --name busy -it busybox /bin/sh
exit
docker ps -a # shows a <name> for hello-world
docker rm <name>
docker rm hi
docker ps -a
docker images
docker rmi hello-world
docker images
Create an apache2 Container Based On the Image httpd:2.4 Mapping localhost:8080 to the Container Port 80

Use docker run to create a container named apache2, based upon the httpd:2.4 image, and map the HTTP port with -p 8080:80.

docker run --name apache2 -p 8080:80 httpd:2.4 
#Use CTRL+C to exit output from running container
docker ps
docker ps -a
docker start apache2
docker logs apache2
docker stats apache2
CTRL+C
lynx -dump http://localhost:8080
In the apache2 Container, Update the Default index.html file, Then Verify and Commit the Changes
docker exec -it apache2 bash
ls
cd htdocs
echo 'apache2 container' > index.html
exit
lynx -dump http://localhost:8080
docker commit -m 'Updated index.html' apache2
Restart the apache2 Container and Verify Changes Are in Effect
docker ps
docker stop apache2
docker ps -a
docker start apache2
lynx -dump http://localhost:8080

Additional Resources

Your IT team plans to deploy Docker containers for an upcoming project. We have been asked to complete this exercise to improve our Docker skills. This exercise involves using more than a dozen docker commands. We will begin by practicing basic skills, running the hello-world and busybox images, and managing their containers and images.

Then, we will create a container named apache2 based upon the httpd:2.4 image, and map the port 8080 on localhost to port 80 on the container.

After testing and monitoring the apache2 container, we will make a change to the web server that it's running. We will replace the default text in index.html with the words apache2 container, and then commit the changes to the container.

Finally, we will verify that the apache2 container is serving the proper content.

We will be practicing the following docker subcommands in this hands-on lab:

  commit      Create a new image from a container's changes
  exec        Run a command in a running container
  images      List images
  logs        Fetch the logs of a container
  ps          List running containers
  ps -a       List all containers
  pull        Pull an image or a repository from a registry
  rm          Remove one or more containers
  rmi         Remove one or more images
  run         Run a command in a new container
  search      Search the Docker Hub for images
  start       Start one or more stopped containers
  stats       Display a live stream of container(s) resource usage statistics
  stop        Stop one or more running containers

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?