DEV Community

Kamrul Hasan
Kamrul Hasan

Posted on

How to Install Docker, Create - Container, Start Container, Stop Container, Delete Container in Ubuntu Server

In this lab tutorial I tried to show how to Install Docker, Create - Container, Start Container, Stop Container, Delete Container in Ubuntu Server. Follow the steps :

Step 1: For Docker installation, at first you have to write these two commands. I am using ubuntu server 22.04.3 LTS.

sudo apt install docker.io
Enter fullscreen mode Exit fullscreen mode

Image description


sudo snap install docker
Enter fullscreen mode Exit fullscreen mode

Image description


Step 2: Check if Docker is installed or not. For checking write this command

which docker
Enter fullscreen mode Exit fullscreen mode

Image description


Step 3: To see all images present in your local machine. Write this command. What do you see? Nothing ? Yes, you are right. Initially there is no image because you did not create any image.

sudo docker images
Enter fullscreen mode Exit fullscreen mode

Image description


Step 4: To find out images in docker hub. Here I want to search for a ubuntu machine. Just write this command to find out all Ubuntu images or other packages. Remember you should write what name is assigned by the docker hub. Here “ubuntu” is named as ubuntu image. You can download this ubuntu image by this command. Any images you can download by this command.

→ Usage: sudo docker pull <image name>
Enter fullscreen mode Exit fullscreen mode
sudo docker pull ubuntu 
Enter fullscreen mode Exit fullscreen mode

Image description


Step 5: By this command you can create a container with an image,pull the image, run the image inside the container, and get inside the container with interactive bash shell. Here, [run = run the the image inside the container, -it = interactive terminal or bash shell, --name = name of the container (“mycontainer”), ubuntu = image name, /bin/bash = default directory you should add ]

sudo docker run -it --name mycontainer ubuntu /bin/bash
Enter fullscreen mode Exit fullscreen mode

Image description


Step 6: If you want to come out from the container you can write this command.

exit
Enter fullscreen mode Exit fullscreen mode

Image description


Step 7: Now you can see the docker images by this command. Here you can see the ubuntu image is created.

sudo docker images
Enter fullscreen mode Exit fullscreen mode

Image description


Step 8: To check status if the docker service starts or not by this command. You can see the service has already started. After checking the status type “q” for quit.

Service docker status
Enter fullscreen mode Exit fullscreen mode

Image description


Step 9: To see only running containers by this command. Here [p = process, s = start] . What do you see? Nothing ? Yes, there is no running container yet.

sudo docker ps
Enter fullscreen mode Exit fullscreen mode

Image description


Step 10: Now let's run the container and check running status by this command. You can see the container status is “Up” that means the container is running.

→ Usage: sudo docker start <container name>
Enter fullscreen mode Exit fullscreen mode
    sudo docker start mycontainer
    sudo docker ps
Enter fullscreen mode Exit fullscreen mode

Image description


Step 11: You can go inside the container by this command. If you do “ls” then you will see the Linux file system inside the container.

→ Usage: sudo attach <container name>
Enter fullscreen mode Exit fullscreen mode
sudo attach mycontainer
Enter fullscreen mode Exit fullscreen mode

Image description


Step 12: Two useful commands. [p = process, s = start, -a = all].

sudo docker ps
Enter fullscreen mode Exit fullscreen mode

{It is used for show only running containers}

sudo docker ps -a
Enter fullscreen mode Exit fullscreen mode

{It is used for show running and also stopped containers at the same time}


Step 13: Stop container

→ Usage: sudo docker stop <container name>
Enter fullscreen mode Exit fullscreen mode
sudo docker stop mycontainer
Enter fullscreen mode Exit fullscreen mode

Step 14: Delete the container. [rm = remove].

→ Usage: sudo docker rm <container name>
Enter fullscreen mode Exit fullscreen mode
sudo docker rm mycontainer
Enter fullscreen mode Exit fullscreen mode


Related keywords:
Docker installation Ubuntu;
Install Docker on Ubuntu;
Docker CE installation guide for Ubuntu;
How to set up Docker on Ubuntu server;
Getting started with Docker on Ubuntu;
Create Docker container on Ubuntu;
Docker run command Ubuntu example;
Docker container tutorial for Ubuntu;
Step-by-step guide to create Docker container on Ubuntu;
Running applications in Docker on Ubuntu server;

Kamrul devops; kamrul-hasan-devops; kamrul-hasan-aws-solution-architect; kamrul-devops; kamrul-cloud; kamrul-hasan-devops; kamrul-devops-engineer; devops-engineer; kamrul-hasan-devops-engineer;

Top comments (0)