Running Containers from Docker Images

Tutorial 2 of 5

1. Introduction

In this tutorial, we'll cover how to run Docker containers from Docker images and manage them. Docker is an open-source platform that allows you to automate the deployment, scaling, and management of applications. It uses containerization to bundle an application and its dependencies into a single object.

By the end of this tutorial, you will be able to:

  • Understand Docker images and containers
  • Run a Docker container from a Docker image
  • Manage your Docker containers

Prerequisites:

  • Basic knowledge of the command line interface (CLI)
  • Docker installed on your machine

2. Step-by-Step Guide

A Docker container is a lightweight, standalone, executable package that includes everything needed to run a piece of software, including the code, a runtime, libraries, environment variables, and config files. Docker image is a lightweight, stand-alone, executable package that includes everything needed to run a piece of software.

Running a Docker Container

To run a Docker container from an image, you use the docker run command followed by the name of the image.

docker run <image-name>

If the image isn't present on your local machine, Docker will pull it from the Docker Hub, which is a cloud registry service provided by Docker.

Managing Docker Containers

You can check the running containers by using the docker ps command.

docker ps

If you want to stop a running container, you use the docker stop command followed by the Container ID or the Container Name.

docker stop <container-id/name>

3. Code Examples

Example 1: Running a Hello World Docker Container

# Running a Hello World Docker Container
docker run hello-world

This command will pull the Hello World image from Docker Hub and run it. The output will be a message that your installation appears to be working correctly.

Example 2: Running an Ubuntu Docker Container

# Running an Ubuntu Docker Container
docker run -it ubuntu bash

This command runs the Ubuntu image in interactive mode (-it) and opens a bash shell. You are now inside the container and can execute commands within it.

Example 3: Stopping a Docker Container

# Check the running Docker containers
docker ps

# Stopping a Docker Container
docker stop <container-id/name>

This will stop the running Docker container.

4. Summary

In this tutorial, we learned about Docker images and containers, how to run a Docker container from an image, and how to manage Docker containers. The next step would be to learn about Dockerfile and how to build your own Docker images.

Additional resources for learning:

5. Practice Exercises

  1. Run a Docker container using the nginx image and access the Nginx welcome page from your browser.
  2. Run a Docker container using the mysql image. Log into the MySQL CLI and create a database.
  3. Run a Docker container using the python image. Write and execute a simple Python script inside the container.

Solutions:

  1. docker run -d -p 8080:80 nginx. Then, open your web browser and navigate to http://localhost:8080.
  2. docker run -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql. Then, docker exec -it <container-id/name> mysql -p.
  3. docker run -it python bash, then write and execute your Python script.

Remember, practice is key when it comes to mastering Docker. Keep experimenting with different images and containers!