This tutorial aims to guide you through the basics of managing Docker containers. By the end of this tutorial, you should understand the lifecycle of a Docker container, learn how to interact with containers, and manage them effectively.
In this tutorial, you will learn about:
* Creating Docker containers
* Starting, stopping and restarting Docker containers
* Deleting Docker containers
* Interacting with running Docker containers
To follow this tutorial you should have:
* A basic understanding of Docker and its fundamentals
* Docker installed on your local machine
A Docker container's lifecycle begins with its creation and ends when it's deleted. The typical stages are:
* Creation
* Running
* Paused or Stopped
* Deletion
To create a Docker container, you will need to pull an image from Docker Hub or use an existing one on your local machine. The command to create a Docker container is:
docker run -d -p 8080:80 --name myContainer nginx
To start, stop, or restart a Docker container, you can use the following commands respectively:
docker start myContainer
docker stop myContainer
docker restart myContainer
To delete a Docker container, you need to stop it first then use the rm
command:
docker stop myContainer
docker rm myContainer
# Pull the nginx image from Docker Hub
docker pull nginx
# Create and run a Docker container named 'myContainer'
docker run -d -p 8080:80 --name myContainer nginx
In this example, the pull
command fetches the nginx
image from Docker Hub. The run
command creates a new Docker container named 'myContainer' from the nginx
image.
# Stop the Docker container
docker stop myContainer
# Delete the Docker container
docker rm myContainer
In this example, the stop
command stops the 'myContainer' Docker container, and the rm
command deletes it.
In this tutorial, you have learned how to manage Docker containers effectively. You now know how to create, start, stop, restart, and delete Docker containers. The next step for you is to learn about Docker images, Dockerfile, and Docker Compose.
Pull the httpd
image from Docker Hub and create a Docker container named 'myHttpdContainer'.
docker pull httpd
docker run -d -p 8081:80 --name myHttpdContainer httpd
Stop and delete the 'myHttpdContainer' Docker container.
docker stop myHttpdContainer
docker rm myHttpdContainer
Create a Docker container using the alpine
image and run the echo "Hello, World!"
command.
docker run alpine echo "Hello, World!"
In the third exercise, the run
command creates a new Docker container from the alpine
image and executes the echo "Hello, World!"
command.
To practice more, try creating, starting, stopping, and deleting Docker containers using different images.