Docker Tutorial
Introduction to Docker
Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications.
Installing Docker
To install Docker, follow these steps based on your operating system:
Windows
Download Docker Desktop for Windows from the official Docker website and follow the installation instructions.
Mac
Download Docker Desktop for Mac from the official Docker website and follow the installation instructions.
Linux
Use the following commands to install Docker on a Linux distribution:
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io
Running Your First Docker Container
To run your first Docker container, use the following command:
docker run hello-world
This command downloads a test image and runs it in a container. When the container runs, it prints a message and exits.
Hello from Docker!
This message shows that your installation appears to be working correctly.
Working with Docker Images
Docker images are the basis of containers. You can use existing images from the Docker Hub or create your own.
Pulling an Image
To pull an image from Docker Hub, use the following command:
docker pull redis
This command pulls the Redis image from Docker Hub.
Running Redis in a Docker Container
To run Redis in a Docker container, use the following command:
docker run --name my-redis -d redis
This command runs a Redis container in detached mode with the name my-redis
.
Accessing Redis
To access the Redis container, use the following command:
docker exec -it my-redis redis-cli
This command opens the Redis CLI inside the container.
Managing Docker Containers
Listing Containers
To list all running containers, use the following command:
docker ps
Stopping a Container
To stop a running container, use the following command:
docker stop my-redis
Removing a Container
To remove a stopped container, use the following command:
docker rm my-redis
Conclusion
In this tutorial, you learned the basics of Docker, including installation, running containers, pulling images, and managing containers. Docker provides an efficient way to develop, ship, and run applications in a consistent environment.