DEV Community

Shahriyar Al Mustakim Mitul
Shahriyar Al Mustakim Mitul

Posted on • Edited on

Docker series (Part 1): Install Docker in Ubuntu

You can manually download Docker for your Ubuntu but we will use a trick here.
First go to https://get.docker.com/
Copy the command

curl -fsSL https://get.docker.com -o get-docker.sh
Enter fullscreen mode Exit fullscreen mode

to your Terminal.

Image description
Then this command

sh get-docker.sh
Enter fullscreen mode Exit fullscreen mode

We followed this because, this commands automatically finds out all demands for your OS and does all of that.
Image description
Image description

Now, add your user account with the Docker Group

sudo groupadd docker

sudo usermod -aG docker $USER

Enter fullscreen mode Exit fullscreen mode

Image description
NOw, let's check if docker works in our system
Image description

Download Docker Compose

DOCKER_CONFIG=${DOCKER_CONFIG:-$HOME/.docker}

 mkdir -p $DOCKER_CONFIG/cli-plugins

 curl -SL https://github.com/docker/compose/releases/download/v2.5.1/docker-compose-linux-x86_64 -o $DOCKER_CONFIG/cli-plugins/docker-compose

Enter fullscreen mode Exit fullscreen mode

Surely instead of v2.5.1, you need to go to GitHub Page and choose the latest release number.
Also, add this line

chmod +x $DOCKER_CONFIG/cli-plugins/docker-compose
Enter fullscreen mode Exit fullscreen mode

Let's check the docker compose version now

docker compose version
Enter fullscreen mode Exit fullscreen mode

Image description

If you face any problem, go to root using

sudo -i
Enter fullscreen mode Exit fullscreen mode

and follow all of the steps to install it.

Now lets run a container in Ubuntu

sudo docker container run --publish 80:80 nginx

Enter fullscreen mode Exit fullscreen mode

Now , go to terminal and type localhost. You will see this.

Image description
Press Ctrl+C to exit.

Let's now create a new container

sudo docker container run --publish 80:80 --detach nginx
Enter fullscreen mode Exit fullscreen mode

This command also shows the container ID.

let's see the list of containers

sudo docker container ls
Enter fullscreen mode Exit fullscreen mode

Image description
Now lets stop the container

sudo docker container stop <1st few numbers of container ID>
Enter fullscreen mode Exit fullscreen mode

Image description
Now, we have no containers left . Check this using

sudo docker container ls
Enter fullscreen mode Exit fullscreen mode

Image description

Is it so???

Let's run

sudo docker container ls -a
Enter fullscreen mode Exit fullscreen mode

Image description

Because

Image description
We have used the command docker container run twice . Each time we run that command, it creates a container from the image (nginx) .
Also, you can see Container name which we never set but docker did by itself. Now, lets set that and create a new container

docker container run --publish 80:80 --detach --name  mitulhost nginx
Enter fullscreen mode Exit fullscreen mode

Image description
Checkout the container list

Image description

Now, lets go to the browser and type localhost and refresh it to generate some logs . Then go to the terminal and them type

sudo docker container logs mitulhost
Enter fullscreen mode Exit fullscreen mode

Image description

I have refreshed my Mozilla browser 4 times and thus you can see 4 lines. That line depends on your refresh.

Now, lets remove our containers. Why I said container, right?
Just type

sudo docker container ls -a
Enter fullscreen mode Exit fullscreen mode

You will see 3 containers with their unique ID name. We will remove them using those container ID's 1st few digits

Image description
Look that the the container which had name "mitulhost" can not be removed. Why?

Because it is up and running . So, we can stop it and then run it. But we will do another thing and force it to be removed

sudo docker container rm -f <1st few digits of the container id>
Enter fullscreen mode Exit fullscreen mode

Image description

Now, check if any container is left or not?

Image description
Nothing!

Done then

Top comments (1)

Collapse
 
italosantana profile image
Ítalo Santana

Hey, awesome tutorial!