How to Install Docker and Manage Docker Containers

Docker is a popular platform that enables developers to create, deploy, and run applications in containers. Containers allow for a consistent development environment, making it easier to manage dependencies and streamline the deployment process. This article will guide you through the installation of Docker and the basics of managing Docker containers.

Installing Docker

Step 1: Update Your System

Before installing Docker, it’s important to update your system to ensure all existing packages are up to date. Open a terminal and run the following commands:

sudo apt-get update
sudo apt-get upgrade

Step 2: Install Docker

To install Docker, run the following commands:

sudo apt-get install docker.io

Once the installation is complete, start Docker and enable it to run at startup:

sudo systemctl start docker
sudo systemctl enable docker

Step 3: Verify Docker Installation

To verify that Docker is installed correctly, run the following command:

sudo docker --version

You should see output indicating the version of Docker that is installed.

Managing Docker Containers

Step 1: Pull a Docker Image

To run a container, you first need to pull an image from Docker Hub. For example, to pull the latest Ubuntu image, run:

sudo docker pull ubuntu

Step 2: Run a Docker Container

To start a container from the pulled image, use the following command:

sudo docker run -it ubuntu

This command will start an Ubuntu container in interactive mode, allowing you to use the command line within the container.

Step 3: List Running Containers

To see a list of running containers, use the following command:

sudo docker ps

This will display information about the currently running containers, including their IDs, names, and statuses.

Step 4: Stop a Running Container

To stop a running container, use the docker stop command followed by the container ID or name:

sudo docker stop <container_id>

Step 5: Remove a Docker Container

To remove a container that is no longer needed, use the docker rm command followed by the container ID or name:

sudo docker rm <container_id>

Step 6: List Docker Images

To see a list of all Docker images on your system, use the following command:

sudo docker images

Step 7: Remove a Docker Image

To remove an image that is no longer needed, use the docker rmi command followed by the image ID:

sudo docker rmi <image_id>

Conclusion

By following these steps, you can successfully install Docker and start managing Docker containers on your system. Docker’s powerful containerization capabilities can significantly improve your development and deployment workflows, making it easier to build, ship, and run applications consistently across different environments.