Docker / Docker Images and Containers

Running Containers from Docker Images

In this tutorial, we will learn how to launch Docker containers from Docker images and how to manage these containers.

Tutorial 2 of 5 5 resources in this section

Section overview

5 resources

Covers Docker images, containers, and their lifecycle management.

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!

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Interest/EMI Calculator

Calculate interest and EMI for loans and investments.

Use tool

Age Calculator

Calculate age from date of birth.

Use tool

MD5/SHA Hash Generator

Generate MD5, SHA-1, SHA-256, or SHA-512 hashes.

Use tool

Hex to Decimal Converter

Convert between hexadecimal and decimal values.

Use tool

WHOIS Lookup Tool

Get domain and IP details with WHOIS lookup.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help