Sunday, 3 March 2019

4 . Docker Series - Container Management



                   Container Management


For premium DevOps and AWS Courses:https://imojo.in/2cox7em
  1. AWS with the project 
  2. DevOps Real-Time 
  3. DevOps with the project 
  4. AWS Real-Time
  5. Linux Admin



Stopping and Starting Containers


#docker run -it ubuntu /bin/bash
#docker stop <container id>
#docker start <container id>
#docker attach <container id>
Ctlr P Q – To detach from container
#docker ps
#docker ps -a
#docker images
#docker kill <container id>
 Note: Docker attach attaches himself  to PID 1, so if shell is not running we will not get the prompt.




Deleting a Container


Container should be stopped before deleting it.
#docker stop <container id>
#docker rm <container id>
#alias dps=”docker ps”


Low Level Container & Image Info


# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
testfile            latest              fa914368ee56        16 hours ago        228 MB
centos              latest              98d35105a391        2 weeks ago         192 MB
ubuntu              latest              0ef2e08ed3fa        4 weeks ago         130 MB

# docker inspect fa914368ee56
Above command shows many details like:
ID, Repotags, Created date and time, containerconfig, various layer info, etc



Getting a Shell in a Container



# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
72f512e7e091        ubuntu              "/bin/bash"              15 hours ago        Exited (0) 15 hours ago                         dazzling_hypatia

# docker start 72f512e7e091
72f512e7e091









               Building from a Docker file




Basics of Dockerfile

  • Dockerfile dockerfile DOCKERFILE
  • Simple text file
  • Contains instructions for how to build image
  • Place is important as other files, directories and sub-directories get includes in the container. So, choosing place is very important.



Creating a Dockerfile


It starts from instruction FROM
Every RUN instruction creates a new layer to our image.
Means every Run command launch a new container, run the command, stop the container and commits the new layer. Next Run command will use this image and follow the same process as above.
#mkdir /devops
# cat /devops/Dockerfile
#Ubuntu - base image
FROM ubuntu
MAINTAINER rishibansal02@gmail.com
RUN apt-get update
CMD ["echo","Hello World"]




Building an Image from the Dockerfile


Go inside the same directory containing Dockerfile.
Here, helloworld, all letters should be in small.
-t = To apply text to the image, not mandatory but recommended.
#docker build -t helloworld:1.0 .
Sending build context to Docker daemon 2.048 kB
Step 1/4 : FROM ubuntu
 ---> 0ef2e08ed3fa
Step 2/4 : MAINTAINER rishibansal02@gmail.com
 ---> Running in 410174fdb081
 ---> 1ec72a6fe42e
Removing intermediate container 410174fdb081
Step 3/4 : RUN apt-get update
 ---> Running in 6ee40780f286
……..
……..
Reading package lists...
 ---> b2d2ff39eeb7
Removing intermediate container 6ee40780f286
Step 4/4 : CMD echo Hello World
 ---> Running in 94078dc48a9d
 ---> 4f2bd08a0ca9
Removing intermediate container 94078dc48a9d
Successfully built 4f2bd08a0ca9

Sending Build context: if there are files and directories in the same directory it is passwd to docker daemon.
# docker run helloworld:1.0
Hello World


 

Running Apache Using Dockerfile


Dockerfile:
FROM centos:7
MAINTAINER Rishi DevOps Batch
LABEL Vendor="CentOS" \
      License=GPLv2 \
      Version=2.4.6-40
RUN yum -y --setopt=tsflags=nodocs update && \
    yum -y --setopt=tsflags=nodocs install httpd && \
    yum clean all
EXPOSE 80
ADD run-httpd.sh /run-httpd.sh
RUN chmod -v +x /run-httpd.sh
CMD ["/run-httpd.sh"]


run-httpd.sh:
#!/bin/bash
# Make sure we're not confused by old, incompletely-shutdown httpd
# context after restarting the container.  httpd won't start correctly
# if it thinks it is already running.
rm -rf /run/httpd/* /tmp/httpd*
exec /usr/sbin/apachectl -DFOREGROUND


Build Command -- (Run at the directory where source files are present)
#docker build -t centos_httpd .

# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos_httpd        latest              9a7d6b2e17c3        8 seconds ago       270.7 MB
docker.io/centos    latest              36540f359ca3        2 weeks ago         192.5 MB



Run container:

#docker run -d -p 80:80 centos_httpd

# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS                NAMES
7b3d1b2b62c5        centos_httpd        "/run-httpd.sh"     About a minute ago   Up About a minute   0.0.0.0:80->80/tcp   small_raman








                Working with Registries




Creating a Public Repo on Docker Hub


Create a public repo on docker hub.
Only 1 free private repo is allowed.
Create below repo.
rishibansal/helloworld




Pushing image to Docker Hub


# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
helloworld          1.0                 4f2bd08a0ca9        2 hours ago         170 MB

First we need to tag the image before pushing to rishibansal/helloworld repo. The first column tells the repo name. So, we need to tag new image to our new repo.
#docker tag 4f2bd08a0ca9 rishibansal/helloworld
# docker images
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
helloworld               1.0                 4f2bd08a0ca9        2 hours ago         170 MB
rishibansal/helloworld   2.0                 4f2bd08a0ca9        2 hours ago         170 MB

Before pushing, make sure to login on dokcer hub
# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: username
Password:
Login Succeeded
# docker push rishibansal/helloworld:2.0
The push refers to a repository [docker.io/rishibansal/helloworld]
23d58913fb58: Pushed
73e5d2de6e3e: Pushed
08f405d988e4: Pushed
511ddc11cf68: Pushed
a1a54d352248: Pushed
9d3227c1793b: Pushed
latest: digest: sha256:3b7485343c49a41a123415e2ebd6beec2c3bd5f5351076ea85a7ecf52d55a517 size: 1569


# docker images
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
rishibansal/helloworld   2.0                 4f2bd08a0ca9        4 hours ago         170 MB

We can see only one layer is pushed. Its because other layers docker hub took from ubuntu image. So, it’s very intelligent and very efficient too.

To delete any image, all the containers associated with that image need to be deleted first.
# docker rm <container id>

#docker rmi <image id>

Click Below to View More :-