OS: CentOS 7
lets create a docker yum repo.
# cd /etc/yum.repos.d/
# vim docker.repo
[dockerrepo]
name= Docker Repository
baseurl=https://yum.dockerproject.org/repo/main/centos/7/
enabled=1
gpgcheck=1
gpgkey=https://yum.dockerproject.org/gpg
###save & exit
# yum update
# yum install docker-engine -y
### in EL 6 disabled SELINUX or add chcon
# systemctl enable docker
# systemctl start docker
# docker --version
# docker images
# cd /var/run/
# ls -al dock*
# usermod -a -G docker user
# cat /etc/group | grep docker -. as username : user
### relogin with username as user and try again
# docker images -. as username : user
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$ BASE IMAGES $$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
[as username: user]
$ docker images
$ docker search centos
$ docker search apache
$ docker search httpd
- pull images from docker hub
$ docker pull
$ docker pull hello-world
$ docker images
- run docker images
$ docker run hello-world
### base images can be referred by repositiry name and image ID.
$ docker pull centos:centos6
$ docker pull centos #[same]
$ docker pull centos:latest #[same]
### centos6 is the version tag and if you leave it empty what is the latest available according to hub will be pulled.
$ docker images
- to know more about an downloaded image you can use docker inspect
$ docker inspect nginx
$ docker inspect centos
$ docker inspect hello-world
$ docker images
$ docker pull docker/whalesay
- run a docker images
$ docker run docker/whalesay cowsay hello
$ docker run docker/whalesay cownsay MyIGT
### If a image not available locally, then docker will check at docker hub and doownload it from there if its available.
- check running docker process
$ docker ps
- chek docker process history
$ docker ps -a
### To run docker image we need a container and each container have an image loaded into it and while container running an image it will have a unique contaner ID.
- inspect a container
$ docker inspect
### exited process of container inspect will not contain much information but running process of a container can provide you more detail.
### by default docker networking will be from 172.17.0.2 to 172.17.0.254.
### if you want to run a container in interactive mode and in terminal mode then use -it flag to docker run command.
$ docker run -it
$ docker run -it centos:latest /bin/bash
-. try running few commandsa here
-. try opening another terminal and observer the output of
$ docker ps
and exit from the containers with
$ exit
and observer the o/p of
$ docker ps
$ docker ps -a
### if you want to run the docker container not in interactive,terminal mode but detached mode then you flag 'd'
$ docker run -d
$ docker run -d centos:latest /bin/bash
### here conainers start the docker image and execute the /bin/bash command and then exit.
$ docker run -d nginx:latest
### now nginx run continously running in the backgroun. confirm with
$ docker ps
and to find more information like ip address about this running container use
$ docker inspect
Note: to understand better save the output of inspect while docker running and stopped anc compare the output
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
[user@rgenupula4 tmp]$ diff naughty_leakey.txt naughty_leakey_off.txt
8,9c8,9
< "Status": "running",
< "Running": true,
---
> "Status": "exited",
> "Running": false,
14c14
< "Pid": 2289,
---
> "Pid": 0,
18c18
< "FinishedAt": "0001-01-01T00:00:00Z"
---
> "FinishedAt": "2017-09-24T07:27:33.032213142Z"
153,154c153,154
< "EndpointID": "c9bd0bd670a302e23b704380d4f24507d022bb2da80b074c7ed3204b2c54befe",
< "Gateway": "172.17.0.1",
---
> "EndpointID": "",
> "Gateway": "",
157,158c157,158
< "IPAddress": "172.17.0.2",
< "IPPrefixLen": 16,
---
> "IPAddress": "",
> "IPPrefixLen": 0,
160c160
< "MacAddress": "02:42:ac:11:00:02",
---
> "MacAddress": "",
167,170c167,170
< "EndpointID": "c9bd0bd670a302e23b704380d4f24507d022bb2da80b074c7ed3204b2c54befe",
< "Gateway": "172.17.0.1",
< "IPAddress": "172.17.0.2",
< "IPPrefixLen": 16,
---
> "EndpointID": "",
> "Gateway": "",
> "IPAddress": "",
> "IPPrefixLen": 0,
174c174
< "MacAddress": "02:42:ac:11:00:02"
---
> "MacAddress": ""
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
Now
$ docker ps
### it will give ports information about the running iamge. You will be able to access those ports.
-. dowload elinks in the host machine
and $ elinks https://
### stop the container
$ docker stop
### we can give custom name to container reference
$ docker run -d --name=myWeb01 nginx:latest
### observer output of
$ docker ps
### inspection
$ docker inspect myWeb01
## start another
$ docker run -d --name=myWeb02 nginx:latest
### observer
$ docker ps
### inspect
$ docker inspect myWeb01
$ docker inspect myWeb02
### stop them
$ docker stop myWeb01
$ docker stop myWeb02
-. you can stop multiple docker instances with
$ docker stop myWeb01 myWeb02 myWeb03
-. docker can be attached if detached with attach string to docker command
$ docker images
$ docker ps
$ docker -d --name=web04 nginx:latest
$ docker attach web04
$ docker stop web04
$ docker start web04
### we can also execute a particluar command in a container and we are done with the command we can still exit from the command but can keep container running in the background and this is feasible with docker exec command. For example start a docker container with
$ docker exec /bin/bash LifeCycle1
$ docker ps
$ docker exec -it LifeCycle1 /bin/bash
.- try some commands
$ exit
### and then check output again with
$ docker ps
$ docker ps -a
### you can stop a container with either "container ID" or "Container Name"
$ docker stop LifeCycle1
### you can restart a container with
$ docker restart LifeCycle1
### observe
$ docker ps
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$ IMAGE $$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$ AND CONTAINER MANAGEMENT $$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
### Remove an docker image
$ docker rmi centos:centos6
### force remove
$ docker rmi -f centos:centos6
### If you have an image centos:centos6 and if we start another container with image name i.e docker run then docker image centos:centos6 will have a reference dependent id on this new container id. so if you want to remove the docker image then we must remove the container image and then remove that docker image.
$ docker ps -a
### find the image reference
$ docker rm
### find the reference ID's of all the docker containers
$ docker ps -a -q
### remove all docker containers
$ docker rm $(docker ps -a -q)
### Assume you have started a container and stopped. And then you force removed the image used for the contianer. Now you still will be able to start/run the container. because whe you start a new container actually docker image will be full cloned into that continer. so even though docker container can start even though image got removed.
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$ Redirection - Ports & Volumes $$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
# find the images
$ docker images
# run the container
$ docker run -d --name=myWeb06 nginx:latest
# observe docker ps
$ docker ps
# insepct the container
$ docker inspect myWeb06
# get ip address of a running container
$ docker inspect myWeb06 | grep IPAddr
# verify nginx
$ elinks https://
### we can expose docker ports to local ports. With -P flag we can tell OS that any ports used by below docker container can have a random port35000 to 60000 range
$ docker run -d --name=myWeb07 -P nginx:latest
# observe the output
$ docker ps
### try elinks as well for both docker ports and exposed external ports. And these ports can be extracted with
$ docker inspect | grep IPAddr
$ docker ps
$ docker port $CONTAINERPORT
### we can assign to explicit port numbers instead of random port numbers
$ docker run -d -p 8080:80 --name=myWeb09 nginx:latest
-> 8080: Localhost port
-> 80: Port inside the docker
### instead of mounting docker containers at default location /var/lib/docker we can mount docker in any custom loation we want with -v flag
$ docker run -d -p 8080:80 --name=myWeb11 -v /mnt/data nginx:latest
### we can mount a local storage volume remotely
-> make sure where you are in your server and write a sample html file
$ vim index.html
This is genupulas nginx server
lets create a docker yum repo.
# cd /etc/yum.repos.d/
# vim docker.repo
[dockerrepo]
name= Docker Repository
baseurl=https://yum.dockerproject.org/repo/main/centos/7/
enabled=1
gpgcheck=1
gpgkey=https://yum.dockerproject.org/gpg
###save & exit
# yum update
# yum install docker-engine -y
### in EL 6 disabled SELINUX or add chcon
# systemctl enable docker
# systemctl start docker
# docker --version
# docker images
# cd /var/run/
# ls -al dock*
# usermod -a -G docker user
# cat /etc/group | grep docker -. as username : user
### relogin with username as user and try again
# docker images -. as username : user
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$ BASE IMAGES $$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
[as username: user]
$ docker images
$ docker search centos
$ docker search apache
$ docker search httpd
- pull images from docker hub
$ docker pull
$ docker pull hello-world
$ docker images
- run docker images
$ docker run hello-world
### base images can be referred by repositiry name and image ID.
$ docker pull centos:centos6
$ docker pull centos #[same]
$ docker pull centos:latest #[same]
### centos6 is the version tag and if you leave it empty what is the latest available according to hub will be pulled.
$ docker images
- to know more about an downloaded image you can use docker inspect
$ docker inspect nginx
$ docker inspect centos
$ docker inspect hello-world
$ docker images
$ docker pull docker/whalesay
- run a docker images
$ docker run docker/whalesay cowsay hello
$ docker run docker/whalesay cownsay MyIGT
### If a image not available locally, then docker will check at docker hub and doownload it from there if its available.
- check running docker process
$ docker ps
- chek docker process history
$ docker ps -a
### To run docker image we need a container and each container have an image loaded into it and while container running an image it will have a unique contaner ID.
- inspect a container
$ docker inspect
### exited process of container inspect will not contain much information but running process of a container can provide you more detail.
### by default docker networking will be from 172.17.0.2 to 172.17.0.254.
### if you want to run a container in interactive mode and in terminal mode then use -it flag to docker run command.
$ docker run -it
$ docker run -it centos:latest /bin/bash
-. try running few commandsa here
-. try opening another terminal and observer the output of
$ docker ps
and exit from the containers with
$ exit
and observer the o/p of
$ docker ps
$ docker ps -a
### if you want to run the docker container not in interactive,terminal mode but detached mode then you flag 'd'
$ docker run -d
$ docker run -d centos:latest /bin/bash
### here conainers start the docker image and execute the /bin/bash command and then exit.
$ docker run -d nginx:latest
### now nginx run continously running in the backgroun. confirm with
$ docker ps
and to find more information like ip address about this running container use
$ docker inspect
Note: to understand better save the output of inspect while docker running and stopped anc compare the output
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
[user@rgenupula4 tmp]$ diff naughty_leakey.txt naughty_leakey_off.txt
8,9c8,9
< "Status": "running",
< "Running": true,
---
> "Status": "exited",
> "Running": false,
14c14
< "Pid": 2289,
---
> "Pid": 0,
18c18
< "FinishedAt": "0001-01-01T00:00:00Z"
---
> "FinishedAt": "2017-09-24T07:27:33.032213142Z"
153,154c153,154
< "EndpointID": "c9bd0bd670a302e23b704380d4f24507d022bb2da80b074c7ed3204b2c54befe",
< "Gateway": "172.17.0.1",
---
> "EndpointID": "",
> "Gateway": "",
157,158c157,158
< "IPAddress": "172.17.0.2",
< "IPPrefixLen": 16,
---
> "IPAddress": "",
> "IPPrefixLen": 0,
160c160
< "MacAddress": "02:42:ac:11:00:02",
---
> "MacAddress": "",
167,170c167,170
< "EndpointID": "c9bd0bd670a302e23b704380d4f24507d022bb2da80b074c7ed3204b2c54befe",
< "Gateway": "172.17.0.1",
< "IPAddress": "172.17.0.2",
< "IPPrefixLen": 16,
---
> "EndpointID": "",
> "Gateway": "",
> "IPAddress": "",
> "IPPrefixLen": 0,
174c174
< "MacAddress": "02:42:ac:11:00:02"
---
> "MacAddress": ""
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
Now
$ docker ps
### it will give ports information about the running iamge. You will be able to access those ports.
-. dowload elinks in the host machine
and $ elinks https://
### stop the container
$ docker stop
### we can give custom name to container reference
$ docker run -d --name=myWeb01 nginx:latest
### observer output of
$ docker ps
### inspection
$ docker inspect myWeb01
## start another
$ docker run -d --name=myWeb02 nginx:latest
### observer
$ docker ps
### inspect
$ docker inspect myWeb01
$ docker inspect myWeb02
### stop them
$ docker stop myWeb01
$ docker stop myWeb02
-. you can stop multiple docker instances with
$ docker stop myWeb01 myWeb02 myWeb03
-. docker can be attached if detached with attach string to docker command
$ docker images
$ docker ps
$ docker -d --name=web04 nginx:latest
$ docker attach web04
$ docker stop web04
$ docker start web04
### we can also execute a particluar command in a container and we are done with the command we can still exit from the command but can keep container running in the background and this is feasible with docker exec command. For example start a docker container with
$ docker exec /bin/bash LifeCycle1
$ docker ps
$ docker exec -it LifeCycle1 /bin/bash
.- try some commands
$ exit
### and then check output again with
$ docker ps
$ docker ps -a
### you can stop a container with either "container ID" or "Container Name"
$ docker stop LifeCycle1
### you can restart a container with
$ docker restart LifeCycle1
### observe
$ docker ps
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$ IMAGE $$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$ AND CONTAINER MANAGEMENT $$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
### Remove an docker image
$ docker rmi centos:centos6
### force remove
$ docker rmi -f centos:centos6
### If you have an image centos:centos6 and if we start another container with image name i.e docker run
$ docker ps -a
### find the image reference
$ docker rm
### find the reference ID's of all the docker containers
$ docker ps -a -q
### remove all docker containers
$ docker rm $(docker ps -a -q)
### Assume you have started a container and stopped. And then you force removed the image used for the contianer. Now you still will be able to start/run the container. because whe you start a new container actually docker image will be full cloned into that continer. so even though docker container can start even though image got removed.
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$ Redirection - Ports & Volumes $$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
# find the images
$ docker images
# run the container
$ docker run -d --name=myWeb06 nginx:latest
# observe docker ps
$ docker ps
# insepct the container
$ docker inspect myWeb06
# get ip address of a running container
$ docker inspect myWeb06 | grep IPAddr
# verify nginx
$ elinks https://
### we can expose docker ports to local ports. With -P flag we can tell OS that any ports used by below docker container can have a random port35000 to 60000 range
$ docker run -d --name=myWeb07 -P nginx:latest
# observe the output
$ docker ps
### try elinks as well for both docker ports and exposed external ports. And these ports can be extracted with
$ docker inspect
$ docker ps
$ docker port
### we can assign to explicit port numbers instead of random port numbers
$ docker run -d -p 8080:80 --name=myWeb09 nginx:latest
-> 8080: Localhost port
-> 80: Port inside the docker
### instead of mounting docker containers at default location /var/lib/docker we can mount docker in any custom loation we want with -v flag
$ docker run -d -p 8080:80 --name=myWeb11 -v /mnt/data nginx:latest
### we can mount a local storage volume remotely
-> make sure where you are in your server and write a sample html file
$ vim index.html
This is genupulas nginx server
save and close.
Lets say this file is located at current working directory/www/index.html
$ docker run -d -p 8080:80 --name=myWeb12 -v /home/user/www:/usr/share/nginx/html nginx:latest
$ docker ps
$ elinks http://localhost:8080
# you would be able to see the file you have created.
$ vim index.html
This is genupulas nginx server
new line has been added
save and exit.
$ elinks http://localhost:8080
And you will see the changes you have made.
###############################################################################
####################### DOCKER FILE ###################################
###############################################################################
$ vim Dockerfile
# where is the image coming from ?
FROM debian:stable
MAINTAINER rajagenupula
# run the commands with RUN flag
RUN apt-get update
RUN apt-get upgrade
save & exit for now.
$ docker build -t rajagenupula/myapache .
$ vim Docker file
# wher eis the image located
FROM debian:latest
MAINTAINER rajagenupula
# run few commands in the docker image
RUN apt-get update
RUN apt-get upgrade
RUN apt-get install telnet
save & exit.
$ docker build -t rajagenupula/myapache .
### Every run creates a new layer and any failed run will not add any layer. And that image is incomplete. So Better to remove that image and correct your docker file and try again.
$ vim Dockerfile
# from where
FROM debian:latest
MAINTAINER rajagenupula
# run commands
RUN apt-get update && apt-get install apache2 openssh-server telnet elinks
save & exit.
Advantage with this is total run will stay in one container layer and it will save disk space.
#### lets add environment variables
$ vim Dockerfile
# where
FROM debian:latest
MAINTAINER rajagenupula
# run
RUN apt-get update && apt-get install openssh-server apache2 elinks openssh-server
# Environment variables
ENV MYVALUE my-value
save & exit.
$ docker build -t rajagenupula/myapache .
##### Lets expose ports
$ vim Dockerfile
FROM debain:latest
MAINTAINER rajagenupula
RUN apt-get update && apt-get install openssh-server apache2 telnet elinks
ENV MYVALUE my-value
EXPOSE 80
EXPOSE 22
save & exit.
$ docker build -t latest123/myapache .
# observe port expose
$ docker ps
### Execute commands
$ vim Dockerfile
FROM debian:stable
MAINTAINER rajagenupula
RUN apt-get update && apt-get install openssh-server telnet apache2 elinks
ENV MY_VALUE myval
EXPOSE 80
CMD ["/usr/sbin/apache2ctl","-D","FOREGROUND"]
save & exit.
# observe
$ docker ps
# elinks
$ docker inspect
$ elinks http://
# connecting
$ docker exec -it
# check apache
$ ps -ef | grep apache
# check env
$ echo $MYVALUE
# exit
$ exit
Note: as initiated with "exec" even we exit still contianer runs in the background.
Note: Practice : expose ports between localhost and docker host and mount volume as well.