Docker Images and Containers
-------------------------------
An image is a collection of bin/lib that are necessary for an application to
[Link] the docker images are present in the cloud site of docker called
[Link]
A Container is a running instance of the [Link] is the indivdual application or
process that docker has created in our user space
Docker Components
---------------------
Dcoker Host:This is the machine where docker is installed
and all the docker images are downloaded.
Docker Client: This is the terminal of docker where we can fire the docker commands
Docker Deamon:This is a background preocess which takes the commands fired by
docker client and sends them to the docker images or contianers or docker registry
Docker Registry:This is the cloud site of docker where all the docker images are
present.
[Link]
We can also create a private registry whcih can be accessed
only by our organisation
Important Docker commands
----------------------------
Docker image commands
----------------------
1 To see all the images present in our docker host
docker images
or
docker image ls
2 To search for images on [Link]
docker search imagename
3 To download an image from docker hub
docker pull imagename
4 To remove an image from docker host
docker rmi imagename
5 To upload an image into docker hub
First login into dockerhub
docker login
docker push imagename
6 To rename a docker image
dcoker tag image1 image2
Dokcer Container commands
-----------------------------
7 To see the list of running container
docker container ls
8 To see the list of all container(running and stopped)
docker ps -a
9 To find detailed info about a container
docker inspect containername/containerid
10 To stop a container
docker stop containername/containerid
11 To start a container
docker start containername/containerid
12 To restart a container
docker restart containername/containerid
To restart a container after 20 seconds
docker restart -t 20 containername/containerid
13 To initialise a container
docker run imagename
Run command options
-------------------
-it Opens an interactive terminal in the container where we can fire
linux commands
-d Start the container in detached mode as a background
process(deamon)
-p Used for port mapping it it will bind the port of docker host with
the port of the [Link] host port is called external
port,container port is called internal port
Eg: -p 8080:80
here 8080 is external port ie dockerhost port
80 is internal port ie docker container port
-P Will publish the port [Link] docker will map the internal port
of the container with an external port on the docker host and this external
port will be some number greater than 30000
-v Used for mounting volumes into the container
--volumes-from Used for creating reusable volumes that can be shared
between multiple containers
--network Used for associating a container with a specific network
--name Used for assigning a name for a container
-e Used for passing environment variables to the containers
-rm Used for deleting the container on exit from the container
14 To Remove a stopped container
docker rm containername/containerid
15 To remove a running container forcebly
docker rm -f containername/containerid
16 To stop all running container
docker stop $(docker ps -aq)
17 To remove all stopped containers
docker rm $(docker ps -aq)
18 To remove all running and stopped containers
docker rm -f $(docker ps -aq)
19 To find the logs generated by a container
docker logs containername/containerid
20 To find the ports used by a container
docker port containername/containerid
Docker Networking commands
---------------------------
21 To see the list of docker networks
docker network ls
22 To create a new network
docker network create networkname
23 To remove a network
docker network rm networkname/networkid
24 To find detailed info about a network
docker network inspect networkname/networkid
25 To attach a running container to a network
docker network attach networkname/networkid
containername/containerid
26 To dittach a container from a network
docker network disconnect networkname/networkid
containername/containerid
27 To open a terminal in an already runnig container
docker exec -it containername/containerid bash
28 To go into a container which has moved into background
docker attach containername/containerid
UseCase1
----------
Start an nginx container in detached mode and name it [Link] the external
port of docker host 8080 with internal port 80 of container
1 Start nginx as a container
dcoker run -d -p 8080:80 --name webserver nginx
2 To access the nginx
Launch any browser
ipaddress-of-dockerhost:8080
UseCase2
---------
Start a tomcat container and publish the port numbers,and git it a name mytomcat
1 Start tomcat
docker run --name mytomcat -d -P tomcat
2 To find the ports of tomcat
docker port mytomcat
3 To see the home page of tomcat
Launch any browser
ipaddrsss-of-dockerhost:32768 (external port displayed in
the above command)
UseCase 3
------------
Start an ubuntu container and go into its interactive [Link] some linux
commands in it
1 Start ubuntu as a container
docker run --name myubuntu -it ubuntu
2 In the container we can run linux commands
3 To come out of the container
exit
UseCase4
---------
Start mysql as a [Link] into its interactive terminal
and login into the database,Create some sql tables
1 Start mysql as a container
docker run -d --name intelliq-mysql -e
MYSQL_ROOT_PASSWORD=intelliq mysql
2 To open interactive terminal in the already runnign
container
docker exec -it intelliq-mysql bash
In the container to login into the db
mysql -u root -p
Enter password
3 To see the list of default availabel databases
show databases;
4 To switch into any of the above databases
use dbname;
5 To create some tables in this db
Open
[Link]
tables-for-mysql/
Copy the code for creating emp and depttables
Paste in the mysql container
6 select * from emp;
select * from dept;
Customising Docker images
----------------------------
We can create our own docker images and save them in [Link] or in our
private docker registry
This can be done in two ways
1 Create a container customised according to our requirment
and save it as a image(snapshot)
2 Use dockerfile
UseCase5
------------
Create a ubuntu container and install git in it
Save that container as an image(snpashot)
Start a container frm this new image and we should find git
installed on it
1 Start ubuntu as a contianer
docker run -it --name myubuntu ubuntu
2 In the container
apt-get update
apt-get install -y git
exit
3 Save the container as an image
docker commit containerid/containername new-imagename
Eg
docker commit myubuntu ubuntu1
4 Start a container from the above image and we will find
git preinstalled on it
docker run -it ubuntu1
In the container
git --version
Docker Volumes
---------------
Since docker containers are for temporary usage,once the container is removed all
the data stored in the container will be [Link] preserve the data we can use
docker volumes
Docker volumes are of two types
1 Simple docker volumes
2 Docker volume containers
Simple Docker Volumes
------------------------
These volumes can be used by only one docker container
UseCase6
---------
Create a empty folder /intelliq.
Start an ubuntu container and mount /intelliq folder on it
Create some files in /intelliq in the contianer
Stop and remove the container,we should still be able to access the files
1 Create a directory
mkdir /intelliq
2 Create a ubuntu container and mount /intelliq on it
docker run --name myubuntu -it -v /intelliq ubuntu
In the container
cd intelliq
touch file1 file2 file3
exit
3 To find the location where the mounted data is stored
docker inspect myubuntu
In the JSON output that is generated search for "Mounts"
Copy the "Source" path
4 Stop and remove the container
docker stop myubuntu
docker rm myubuntu
5 To access the files created in the container
cd path-of-source-folder
UseCase7
----------
Create a directory /intelliq
and mount it on centos container [Link] some files in the intelliq folder in the
c1 container
Create another 2 container c2 and c3
These two containers should use the voulmes that are used by c1
1 Create a directory
mkdir /intelliq
2 Start centos as a container
docker run -it --name c1 -v /intelliq centos
In the c1 container
cd intelliq
touch file1 file2
Come out of the container without exit(Ctrl+p,Ctrl+q)
3 Start another centos container c2 and this should use the volumes used by c1
docker run -it --name c2 --volumes-from c1 centos
In the c2 container
cd intelliq
touch file3 file4
Come out of the container without exit(Ctrl+p,Ctrl+q)
4 Start another centos container c3 and this should use the volumes used by c2
docker run -it --name c3 --volumes-from c2 centos
In the c3 container
cd intelliq
touch file5 file6
Come out of the container without exit(Ctrl+p,Ctrl+q)
5 Go into any of the three containers and we will see all the files created by
all the three contaieners
docker attach c1
cd intelliq
ls
Linking of Docker containers
-----------------------------
This can be done in three ways
1 --link option
2 Docker compose
3 Docker networking
--link option
--------------
This is a depricated way of creating contianer architecture
We can link multiple docker containers and passdata between them
UseCase 8
----------
Start 2 busybox contianers and link them with each other
1 Start a busybox container name it c1
docker run -it --name c1 busybox
Come out of the contianer without exit(ctrl+p,Ctrl+q)
2 Start another busybox container and link with first container
docker run -it --name c2 --link c1:c1alias busybox
In c2 container
ping c1 (It should ping)
Creating Development environment with docker
--------------------------------------------
Use case9
----------
Create a mysql container and link it with wordpress [Link] the linking is
done a developer should be able to create a wordpress website.
1 Start mysql as a container
docker run -d --name intelliq-mysql -e
MYSQL_ROOT_PASSWORD=intelliq mysql:5
2 Start a wordpress container and link it with mysql
docker run -d --name intelliq-wordress -p 8080:80 --link intelliq-
mysql:mysql worpdress
3 To create a wordpress website
Launch any browser
ipaddress-of-dockerhost:8080
UseCase 10
-----------
Create a setup for implementing ci-cd through jenkins
1 Start jenkins as a container
docker run -d --name jenkinsserver -p 5050:8080 jenkins
To see the home page of jenkins
Launch any browser
ipaddress-of-dockerhost:5050
2 Start tomcat as qa server and link with jenkins
docker run -d --name qaserver --link jenkinsserver:jenkins -p
6060:8080 tomcat
3 Start tomcat as prod server and link with jenkins
docker run -d --name prodserver --link
jenkinsserver:jenkins -p 7070:8080 tomcat
to see the homepage of tomcat (qaserver and prodserver)
ipaddress-of-dockerhost:6060
ipaddress-of-dockerhost:7070
UseCase11
---------
Start a jenkins container and link it with an ubuntu [Link] this ubuntu as
a slave
1 Start jenkins as a contianer
docker run -d --name jenkinsserver -p 8080:8080
jenkins
2 To see the dashboard of jenkins
Launch any browser
ipaddress-of-dockerhost:8080
3 Capture the ipaddress of jenkins container
docker inspect jenkinsserver
4 Start an ubuntu container
docker run --name slave -it --link
jenkinsserver:jenkins ubuntu
In the ubuntu slave container
apt-get update
apt-get isntall -y wget
5 Download [Link] in the slave ubuntu container
wget [Link] jenkinsserver:8080/jnlpJars/[Link]
Creating QA environment
--------------------------
Usecase12
-----------
Start a selenium hub container
Start 2 selenium node containers and link them with the selenium hub container
Run the selenium testing programs
1 Start selenium hub container
docker run --name hub -P -d selenium/hub
2 Start a node which has firefox installed on it and link with hub container
docker run -d --name firefox --link hub:selenium -P -e
HUB_HOST=hub selenium/node-firefox-debug
3 Start another node which has chrome installed on it and link with hub
container
docker run -d --name chrome --link hub:selenium -P -e HUB_HOST=hub
selenium/node-chrome-debug
4 To see the ports of hub,firefox and chrome nodes
docker ports hub
docker ports firefox
docker ports chrome
5 The chrome node and firefox nodes are GUI nodes
We can see them by installing a s/w called vnc viewer
6 Install vnc viewer-->Enter ipaddress-of-dockerhost:port number of firefox or
chrome container
7 Enter password as "secret"
Docker Compose
-----------------
This is a feature of docker where we can create an architecture of multiple
contianers accoring to the requirmenets of the devlopment team,Qa team etc
Docker compose uses yml script for performing these activites
All the containers that we want to start and link are given in the compose file in
the form of [Link] these services can be started or stopped using one single
command
Install docker compose on linux
----------------------------------
1 Open [Link]
compose
2 Copy the 2 commands and paste in lunux terminal
curl -L [Link]
compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
3 To check the version of dockercompose
docker-compose --version
[Link] file
----------------
---
intelliq:
- trainers:
- sai:
- Course: Devops
- fee: 12000
- sheshi:
- Course: AWS
- fee: 10000
- coordinator:
- shailaja
...
UseCase 13
----------
Create a docker compose file for linking a wordpress contianer with mysql contianer
vim [Link]
---
version: '3'
services:
intelliq-mysql:
image: mysql:5
environment:
MYSQL_ROOT_PASSWORD: intelliq
intelliq-wordpress:
image: wordpress
ports:
- 5050:80
...
Save and quit(:wq)
To start all the services in the compose file
docker-compose up -d
To stop the service
docker-compose stop
UseCase 14
-----------
Create docker compose file for starting a jenkins contianer and link with two
tomcat containers so that we can implement CI-CD
vim [Link]
version: '3'
services:
jenkinsserver:
image: jenkins
ports:
- 6600:8080
qaserver:
image: tomcat
ports:
- 7700:8080
links:
- jenkinsserver
prodserver:
image: tomcat
ports:
- 8800:8080
links:
- jenkinsserver
Save and quit
docker-compose up -d
To see the home page of jenkins
Launch any browser
ipaddress-of-dockerhost:6600
To see the home page of qa and prodserver
Launch any browser
ipaddress-of-docker-host:7700
ipaddress-of-docker-host:8800
UseCase15
-----------
Create a docker compose file for setup of the QA [Link] seelnium hub
container and link it with two node contianers
vim [Link]
version: '3'
services:
hub:
image: selenium/hub
ports:
- 4444:4444
firefox:
image: selenium/node-firefox-debug
ports:
- 5901:5900
environment:
HUB_HOST: hub
links:
- hub
chrome:
image: selenium/node-chrome-debug
ports:
- 5902:5900
environment:
HUB_HOST: hub
links:
- hub
Dockerfile
-----------
This is a simple text file which is used for creating [Link] uses a syntax
called GO which has certain predefined keywords
Important Keywords in Dockerfile
---------------------------------
FROM This is generally the first statement in dockerfile
and it is used to specify the base image from which we are creating
out dockerimages
MAINTAINER This is the name of the person or the organisation that has
created this dockerfile
CMD This is the default instruction that gets executed when the container is
started
ENTRYPOINT This is used to pass arguments to the CMD instruction and
it can also be used to specify the default process that should be
initiated when a container starts
RUN This is used for package management ie installing or uninstalling s/w using
apt or yum package managements
ADD This is used to copy files from the host machine to the docker container or
to download some files from a remote url
EXPOSE This is used for specifing what internalport of the container has to
exposed to the outside world
USER This is used to specify the default user credentials through which we
want to login into a container
ENV This is used to pass environment variables to the container
WORKINGDIR This is used to specify the directory where the CMD instruction should
be executed
VOLUME Used for attaching volumes to the docker container
This will become the default volume for the container
Create dockerfile from ubuntu base image ,specify the maintainer and give the
default instruction to execute
vim dockerfile
FROM ubuntu
MAINTAINER intelliq
CMD ["date"]
Save and quit
To create an image from thw above docker file
docker build -t myubuntu .
Note -t reperesents tagname ie some name for the image
that we are creating
. represents current working dir ie it will read the dockerfile present
in the current working dir and create an image
Create a dockerfile from ubuntu base image and make the default process as
reading /etc/passwd
vim dockerfile
FROM ubuntu
MAINTAINER intelliq
ENTRYPOINT ["/bin/cat"]
CMD ["/etc/passwd"]
Save and quit
To create an image from the above docker file
docker build -t myubuntu .
Creating a Development environment
-------------------------------------
1