Docker beginner approach A-Z that you should know! part 2 | End
Published: 2021-12-19 04:37:02

Previously, I discussed in detail about Docker's theoretical part and installation process. Before starting this coding part, if you don't know about docker, please read part 1 first [click here](https://dev.to/sabbir185/docker-beginner-approach-a-z-that-you-should-know-353j). And if you already know about docker's basic knowledge, you're welcome to part 2.

> **Linux**

I told you that I will teach you basic Linux commands that are needed only for docker. And I hope, it will be very helpful to start not only docker but also to run the Linux operating system. Open your terminal and type 
```
sudo su
```
`sudo` super user do and `su` lets you switch user so that you're actually logged in as a root user.
**pwd** -> Used to find out the path of the current working directory(folder) you’re in.
**cd** -> To navigate through the Linux files and directories, use the `cd` command.
**ls** -> If you want to see the list of files on your Linux system, use the `ls or ls -a` command.
**mkdir** -> Use the `mkdir` command when you need to create a folder or a directory.
**rmdir** -> Use `rmdir` to delete a directory. But `rmdir` can only be used to delete an empty directory. To delete a directory containing files, use `rm`.
**rm** -> Use the `rm` command to delete files and directories.
**touch** —> The `touch` command is used to create a file.
**cat** -> we can create and concatenate( join) files.
**vi** -> text editor, you can modify and save file.
**echo** —> The `echo` command helps us move some data, usually text into a file.
**cp** — Use the `cp` command to copy files through the command line. It takes two arguments: The first is the location of the file to be copied, the second is where to copy.
**mv** — Use the `mv` command to move files through the command line. We can also use the `mv` command to rename a file.
**sudo apt-get update** -> It is good to update your repository each time you try to install a new package.
**sudo apt-get upgrade** -> You can upgrade the system

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ma9md30k8qxvlc9s5w0.png)

## **Docker** 💥💥💥
> **image to container** 🇧🇩 

First of all, we want to create a container and to create a container we need an OS image. So, we pull an image from the docker hub. Before starting, we need root access, open terminal and run
```
sudo su
```
Start your docker service and run
```
service docker start
```
you can see docker information using `docker info`
However, at this time our goal is to create a container. We need an OS image and we want to take the Ubuntu OS image. For that, run
``` 
docker images
```
now see that we have no image so we want to pull an image from the docker hub. For that, run
```
docker pull ubuntu
```
Now see the real magic. It first searches on our local environment. But we have no ubuntu OS image and that's why it pulls from the docker hub and looks at the file size, it is only almost 28 MB! I already said that docker OS images are light in weight. After downloading and extracting, the file size stands around 72 MB! Now again run 
``` 
docker images
```
This time you found an OS image. Congratulations! 1st step done. You can try to pull centos OS using `docker pull centos`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ux2n4l3wnkr3o1oxno2z.png)

I told you, we have 3 ways to create an image. And one of them is done, pulling images from the docker hub.🔥

It's time to make a brand new container. Remember that we have ubuntu OS image, so run
```
docker run -it --name ubuntu_container1 ubuntu /bin/bash
```
wow! it is looking like some weird things😨. Okay, let me explain. Here `run` is a combination of create and start. On the other hand, `-it` means interactive mode with a terminal. Then if we want to give a name for a container, we have to use `--name` command then the container's name. In our case, container name is _ubuntu_container1_. After that, we have selected an image and `/bin/bash` always fixed here. So, 

`docker run -it --name container_name image_name /bin/bash`

Now, you're in your container. Run `ls` on your terminal. You will see Linux directories. And it is now your development environment as like your personal computer. You can create and run your application in the container.

Else, you maybe noticed that we get a container ID besides of root@ on the terminal.

Now, create a file in your container by using 
```
touch container1.txt
```
to see this file run `ls` command. You see! You've created a file in your container. Not only files but also any applications that you want to create and share with others, you can!

To exit or stop your current container, use `exit` command.
And to see your all containers, run
```
docker ps -a
```
You see all containers here but at this time we can see only one container and which is our ubuntu_container1. Carefully observe, here we found container ID, IMAGE (we used ubuntu, remember?), NAME etc. But if we want to see just running container, we just run `docker ps` At this time, we have no running container. Because we `exit` our container.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5asfkvzpibawlzt50va2.png)

.

> **container to image** 🇧🇩 

We have a container (ubuntu_container1), now we want to convert container into image. So that we can share our files or projects with others. Remember? in our container we created a file (container1.txt). And now we want to share it through an image. So run 
```
docker commit ubuntu_container1 my_image1
```
Here, `ubuntu_container1` is our container name that we want to convert into an image and  `my_image1` will be our new image name.

To see our new image, run
```
docker images
```
And boom 💥, do you see? In the image list, you see our `my_image1` image file. Now you can create a container by using this image and you will find container1.txt file by running `ls` command that we created.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lc5fixgzsvlaw4ktqfci.png)

Again, to create a container by using this image, run 
```
docker run -it --name ubuntu_container2 my_image1 /bin/bash
``` 
Now run `ls` you will find our container1.txt file 🤩.

Exit or stop your container by using `exit` command and run `docker ps -a` this time, you will find 2 containers, `ubuntu_container2` is the recent one. And one was created by `ubuntu` image and the other was created by `my_image1` image. Make sense?

**Good to know**
To enter an existing container, run
```
docker attach container_name
```
To delete a container
```
docker rm container_name/id
```
To delete an image
```
docker rmi image_id
```
To start an existing container but doesn't enter into the container
```
docker start container_name
```
To start an existing container
```
docker stop container_name
```
.

> **upload image into docker hub** 🇧🇩 

To share your docker image with others, you have to upload your image into the docker hub. For that, first you have to create an account on `hub.docker.com`. Simple sign-up process! After successfully sign-up, back your terminal and run
```
docker login
```
then enter your `username` and `password` and if all is okay, it will show you `Login Succeeded`. And now you're ready to upload your image into the docker hub.

We need to tag our image. That mean's, we have to set a name that we want to store in our docker repository. so run
```
docker tag my_image1 sabbir185/my_image1
```
after `tag`, our container image name and then a name that we want to set to store our container image. For example, I set a tag name `sabbir185/my_image1` for our container image `my_image1`. 

Now run the `push` command to upload the image. After `push` command, needs tagged image name. And my case `sabbir185/my_image1`. Now hit enter
```
docker push sabbir185/my_image1
```
Refresh your docker hub profile, you will see an image. 🥳😎

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jks3lpsrzevu91y1gb8r.png)

Now, you can pull your docker image or share it with others. For example in my case, my docker image `sabbir185/my_image1`

If I want to pull my docker image, I just run
```
docker pull sabbir185/my_image1
```
Remember? I have already discussed it 🤩🥰.
.

> **Dockerfile to image** 🇧🇩 

I told you, there are 3 ways to get an image. This is another method to gain a docker image and not only a popular one but also easy one of them. To create an image, first we have to create a Dockerfile, so run
```
touch Dockerfile
```

After that, open and write what you want to build. For my case, I want to create a ubuntu image and in image file, want to create a file name test1.txt. You have to open Dockerfile by
```
vi Dockerfile
```
And press `i` for write into the text editor.
`FROM ubuntu`
`RUN echo "Welcome to docker" > /tmp/test1.txt`

press `Esc` key and `:wq` then enter. Now run to build image
```
docker build -t image2 .
```
You can set any image name instead of image2

Check image
```
docker images
```
.

**_Bonus talk_**
There are some other topics. For example docker volume, port/expose, docker-compose etc. But For a beginner, I think it's enough for you now. Practice it and then explore some advanced topics that you want.
.

### `Thanks for your time`🥰