DEV Community

Kamrul Hasan
Kamrul Hasan

Posted on

Check Docker Installation Details Part - 3

Exploring Basic Docker Commands

Image description

Now that we've verified our Docker installation works correctly (Part-2), let's explore some basic Docker commands that will help you manage containers and images.

Running an Interactive Container

Unlike the hello-world container that runs and exits immediately, we can run containers interactively.

1.Let's run an Ubuntu container and interact with its shell:

docker run -it ubuntu bash
Enter fullscreen mode Exit fullscreen mode

Image description

This command:
• -i keeps STDIN open
• -t allocates a pseudo-TTY (terminal)
• ubuntu is the image name.
• bash is the command to run inside the container.

Once the container starts, you'll be at a bash prompt inside the container. The prompt shows you're logged in as root in the container. The alphanumeric string is the container ID.

2.Try running some Linux commands inside the container:

cat /etc/os-release
Enter fullscreen mode Exit fullscreen mode

This will display information about the Ubuntu version running in the container:

Image description

3.When you're done exploring, exit the container by typing:

exit
Enter fullscreen mode Exit fullscreen mode

Image description

This will return you to your host terminal.

Container Lifecycle Management

4.Let's explore how to manage the lifecycle of a container. First, we'll start a container in detached mode:

docker run -d --name web nginx
Enter fullscreen mode Exit fullscreen mode

Image description

This command:
-d runs the container in detached mode (in the background)
--name web assigns the name "web" to the container
• nginx is the image to use.

You'll see a long container ID printed on the screen.

5.Now, let's check that the container is running:

docker ps
Enter fullscreen mode Exit fullscreen mode

Image description
You should see the nginx container running.

6.To stop the container:

docker stop web
Enter fullscreen mode Exit fullscreen mode

Image description

7.Verify it's stopped:

docker ps
Enter fullscreen mode Exit fullscreen mode

Image description

8.The container should no longer appear in the list of running containers. To see all containers including stopped ones:

docker ps -a
Enter fullscreen mode Exit fullscreen mode

Image description
You should the nginx containers in the stopped state.

9.To start the container again and check container status:

docker start web
Enter fullscreen mode Exit fullscreen mode
docker ps 
Enter fullscreen mode Exit fullscreen mode

Image description

10.to remove a container when you no longer need it:

docker rm web
Enter fullscreen mode Exit fullscreen mode

Image description

11.Error response from daemon: cannot remove container "/web": container is running stop the container before removing. First stop the running container then remove and check:

docker stop web
Enter fullscreen mode Exit fullscreen mode
docker rm web
Enter fullscreen mode Exit fullscreen mode
Command: docker ps -a 
Enter fullscreen mode Exit fullscreen mode

Image description

You should see the web container not available.

Managing Docker Images

Let's explore how to manage Docker images. To list all images:
12.Let’s check the docker images available of your system.

docker images
Enter fullscreen mode Exit fullscreen mode

Image description
You should see at least the hello-world and nginx images.

13.To remove an image when you no longer need it:

docker rmi hello-world
Enter fullscreen mode Exit fullscreen mode

Image description

Error response from daemon: conflict: unable to remove repository reference "hello-world" (must force) - container be8b92819418 is using its referenced image 74cc54e27dc4.
Note that you can't remove images that are being used by containers (even stopped ones). You'll need to remove those containers first.

14.Let’s remove the hello-world container then remove hello-world image:

Command:docker ps -a
Enter fullscreen mode Exit fullscreen mode
Command: docker rmi hello-world
Enter fullscreen mode Exit fullscreen mode
Command: docker images
Enter fullscreen mode Exit fullscreen mode

Image description
You should see the hello-world image not available in the images list.

Docker System Information

To check disk space used by Docker (containers, images, volumes):

15.Let’s check disk space used by docker:

docker system df
Enter fullscreen mode Exit fullscreen mode

Image description
This will show a summary of disk usage.

16.To get more detailed information:

docker system df -v
Enter fullscreen mode Exit fullscreen mode

Image description

This will show a breakdown of each image and container and their sizes.

LinkedIn: https://linkedin.com/in/kamrul-dev
GitHub: https://github.com/kamrul-dev

Related Keywords:
How to verify Docker installation on your system,
Checking Docker version and system info via CLI,
Confirming Docker is properly installed on Linux/Mac/Windows,
Commands to check Docker installation status,
Troubleshooting Docker setup issues,

Kamrul Hasan DevOps Engineer,
DevOps tips by Kamrul Hasan,
Kamrul Hasan Docker tutorials,
Learn DevOps with Kamrul Hasan,
Kamrul Hasan automation expert,
Kamrul Hasan cloud and containers,
CI/CD pipelines by Kamrul Hasan,
Kamrul Hasan Kubernetes & Docker,

Top comments (0)