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:
Prerequisites:
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.
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.
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>
# 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.
# 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.
# Check the running Docker containers
docker ps
# Stopping a Docker Container
docker stop <container-id/name>
This will stop the running Docker container.
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:
nginx
image and access the Nginx welcome page from your browser.mysql
image. Log into the MySQL CLI and create a database.python
image. 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 to http://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!