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.
Section overview
5 resourcesCovers 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
- Run a Docker container using the
nginximage and access the Nginx welcome page from your browser. - Run a Docker container using the
mysqlimage. Log into the MySQL CLI and create a database. - Run a Docker container using the
pythonimage. Write and execute a simple Python script inside the container.
Solutions:
docker run -d -p 8080:80 nginx. Then, open your web browser and navigate tohttp://localhost:8080.docker run -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql. Then,docker exec -it <container-id/name> mysql -p.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.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI 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 articleAI 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 articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article