Wednesday, September 1, 2021

Day2 - Docker - container, volume, start container

Day 2 - Introduction to Docker
Day2 - Docker - container, volume, start container
Can I start/stop the container? How about image?
- You can start/stop container and remove container but you can not stop/start image. You can pull image and remove the image.
How to run a container?
$ docker run -dt centos bash # Runs on deatach mode
here, you got a template called centos and created one container.
The container has started.
Check the running containers
$ docker ps
Now, how do you login to container?
$ docker ps
$ docker exec -it <container name/ID> bash
here bash is the working shell
$ docker exec -it centos bash
Now, create a new session of host machine and stop the container
$ docker container stop <container_ID> ## run docker ps to find the container id
Create a new container
$ docker run --rm -it contos bash
Note:
docker ps - shows you container list that are running
docker ps -a -> shows you list of all container, in fact history

How to remove a container
$ docker ps # get the contiainer id
$ docket container rm element_1
$ docker ps -a $ list all available container including stop ones
$ docket run --rm -it --name <name of container>
$ docker run --rm -it --name first_cont ubuntu bash
Now, you are at the container prompt. run hostname, you will see some id.
if you type exit or quit at the prompt, you will be out of container and container will stop as well.
on the host machine, type 
$ docker ps # you will see the containers running
$ docker ps -a # list all containers including stopped one.

Lets login to continer
$ docket run --rm -it --name first_cont --hostname ubuntu ubuntu bash
and type hostname' cat /etc/os-release at the prompt.
Note: Downloaded images are read only images. 
container runs on a concept called copy on write (COW)
copy on write mean, you are adding content on the container.
at the container prompt, type 
# apt-get update   -> it will go and update the container
so, you can write to your container but you can't update the image. (readonly)
At the prompt, run
$ apt-get install apache2 # we are installing apache
Note: if you are using interactive mode and you exit out of the container prompt, container will be removed.
$ docker images ls # list container images
$ docker image rm ubuntu
$ docker pull ubuntu
go to the URL hub.docker.com
at the search box, type ubuntu and click on ubuntu official
- click on docker file.
- read everything on the file and try to understand it.
- This file is the foundation of the image.
also search for nginx and read the dockerfile for the image.
so, images are build based on dockerfile.
[ Remember: Copy on write filessytem]
so what happens when you run docker pull command?
- pull command first goes to the hub.docker.com and checks the dockerfile and grabs all the things stated on the file. Once pull is complated, you will have particular image. These images are stored in local repository. The default location is /var/lib/docker
Things to remember,
- Images are read only but container that are based on images can be modified.
- Docker containers are volatile
  - if you store data in a container, and you remove the container, all the data is lost. so containers are usually immutable (ephenreal - temporary or disposible)
Once you remove the container, data on the containers are gone.
$ docker run -it --name first-cont --hostname ubuntu ubuntu bash
# echo "Hello" > hello.txt
# cat hello.txt
# exit
$ docker ps # container is gone/ stopped
$ docker ps -a
Start the stopped container
$ docker ps -a 
$ docker ps
$ docker container start first_cont  # first_cont is started
Now, you have to login
$ docker exec -it first_cont bash
# cat hello.txt # you may still be able to see it.
# exit
$ docker ps
$ docker ps -a
$ docker container rm -f first_cont
$ docker ps ; docker ps -a # now, its gone. all data on the container is gone.
so, containers are temporary, and data is also temporary. So, you can say containers are volatile.
To resolve this issue what we can do is,
1. create a persistence volume
2. Bind mounts
What is the benefit of working with volumes,
1. To keep the data around when container is removed
2. To share data between host and container
why should we use container?
- It is a light weight OS (about 200mb) compare to virtual machine(appr 4GB).
- Only one app is install and it only contains some library and binaries.
- volume helps you to keep data even you remove the container
what happens if you wan to upgrade?
you can still upgrade without any issue.
So, what is the benefit of using volume?
we can say that
1. A data volume is a specially designed directory in the container
2. It is initialized when the container is created. By default, it is not deleted when the container is removed
3. Data volume can be shared across the containers too.
4. Volumes are stored on host machines.
5. You can name particular volume with meaningful name
How to create a volume
$ docker volume ls # lists the volumes
$ docker container run -it ---name secon_con -v /data ubuntu bash
-v -> name of the volume
/ -> mount at root of container
-it -> interactive.
Open new session of the host machine
$ docker volume ls
$ docker inspect <container_ID> second
inspect - what to inspect
the above command gives detail about container such as ip, gateway, id, when it is started, 
image name, volume name, volume id and other info
review the source and the path that start with
/var/lib/docker/volumes/<id>/_data
$ docker volume ls # verify the id, you will find these two same.
$ ls /var/lib/docker/volumes/...../_data
permission denied
$ sudo ls /var/lib/docker/volumes/.../_data
Login to docker and create a file
# cd /data; echo "Hello" >hello.txt
# exit
$ docker ps
$ docker container rm second
$ docker ps -a # everything is done about second container
$ docker volume ls # you will still see the volume
Where the volume is pointing to?
$ docker volume inspect <volume_ID>
$ sudo ls -l /var/lib/docker/volume/<id>/_data
you will still see the data.
if you want to use this volume, you have to specify that this is the volume you want to use when you start a container.

so the question is:
How to start a container with the same volume?








No comments:

Post a Comment

Git branch show detached HEAD

  Git branch show detached HEAD 1. List your branch $ git branch * (HEAD detached at f219e03)   00 2. Run re-set hard $ git reset --hard 3. ...