Professional Documents
Culture Documents
docker ps
docker logs [OPTIONS] CONTAINER : --follow, --tail ---- show jenkins example
docker inspect e9903e9b3122 ---- id, created date, image, platform, hsot, ip
-------------------------------------------------
IMAGES:
docker login
Username: ravindramca43
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
Dockerfile1:
FROM ubuntu
MAINTAINER "Ravi"
RUN apt-get update
RUN apt-get install -y curl vim
FROM debian:latest
RUN apt-get update
RUN apt-get install -y procps vim curl nginx
EXPOSE 80
CMD /usr/sbin/nginx -g "daemon off;"
FROM centos
MAINTAINER "Ravi"
RUN yum update -y
RUN yum install -y httpd
EXPOSE 80
CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]
Dockerfile4:
FROM ubuntu
RUN apt-get -y update && apt-get -y upgrade
RUN apt-get -y install openjdk-8-jdk wget
RUN mkdir /usr/local/tomcat
RUN wget http://www-us.apache.org/dist/tomcat/tomcat-8/v8.5.35/bin/apache-tomcat-
8.5.35.tar.gz -O /tmp/tomcat.tar.gz
RUN cd /tmp && tar xvfz tomcat.tar.gz
RUN cp -Rv /tmp/apache-tomcat-8.5.35/* /usr/local/tomcat/
EXPOSE 8080
CMD /usr/local/tomcat/bin/catalina.sh run
options:
FROM
ADD
COPY
ENV
EXPOSE
FROM
LABEL
VOLUME
WORKDIR
The FROM instruction initializes a new build stage and sets the Base Image for
subsequent instructions. As such, a valid Dockerfile must start with a FROM
instruction.
The RUN instruction will execute any commands in a new layer on top of the current
image and commit the results
The EXPOSE instruction informs Docker that the container listens on the specified
network ports at runtime.
To actually publish the port when running the container, use the -p flag on docker
run to publish and map one or more ports, or the -P flag to publish all exposed
ports and map them to high-order ports.
The ENV instruction sets the environment variable <key> to the value <value>. ENV
myName="John Doe"
The ADD instruction copies new files, directories or remote file URLs from <src>
and adds them to the filesystem of the image at the path <dest>
ADD test /absoluteDir
The COPY instruction copies new files or directories from <src> and adds them to
the filesystem of the container at the path
The WORKDIR instruction sets the working directory for any RUN, CMD, ENTRYPOINT,
COPY and ADD instructions that follow it in the Dockerfile
WORKDIR /path/to/workdir
docker save -o name.tgz imageid -- to save image to local and scp to remote
-----------------------------------------------------
VOLUMES:
docker volume create my-vol -- to create a new vol
docker volume ls
In above command
/tmp/test is source path on your host os
/usr/share/nginx/html is dest path
$ docker volume prune --To remove all unused volumes and free up space
-----------------------------------------------------------------------------------
--------------------
COMPOSE:
curl -L https://github.com/docker/compose/releases/download/1.24.0-rc1/docker-
compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
version: '3'
services:
web:
image: nginx
ports:
- "80"
volumes:
- /home/docker/ravi:/usr/share/nginx/html
version: '3'
services:
web:
image: nginx
ports:
- "80"
volumes:
- /home/docker/ravi:/usr/share/nginx/html
app:
image: tomcat
ports:
- "8080"
case 3: build multiple images & deploy multiple containers from multiple images
; create compose files in two folders
image1 Dockerfile
FROM ubuntu
RUN apt-get -y update && apt-get -y upgrade
RUN apt-get -y install nginx
EXPOSE 80
CMD /usr/sbin/nginx -g 'daemon off;'
image2 Dockerfile
FROM ubuntu
RUN apt-get -y update && apt-get -y upgrade
RUN apt-get -y install openjdk-8-jdk wget
RUN mkdir /usr/local/tomcat
RUN wget http://www-us.apache.org/dist/tomcat/tomcat-8/v8.5.35/bin/apache-tomcat-
8.5.35.tar.gz -O /tmp/tomcat.tar.gz
RUN cd /tmp && tar xvfz tomcat.tar.gz
RUN cp -Rv /tmp/apache-tomcat-8.5.35/* /usr/local/tomcat/
EXPOSE 8080
CMD /usr/local/tomcat/bin/catalina.sh run
docker-compose.yml
version: '3'
services:
web:
build:
context: .
ports:
- "80"
volumes:
- /home/docker/ravi:/usr/share/nginx/html
app:
build:
context: ./tomcat
ports:
- "8080"
-------------------------------------------------------------------
Docker Swarm
--------------------------------------------------------------------------
Docker Service
docker node demote Demote one or more nodes from manager in the swarm
docker node inspect Display detailed information on one or more nodes
docker node ls List nodes in the swarm
docker node promote Promote one or more nodes to manager in the swarm
docker node ps List tasks running on one or more nodes, defaults to current node
docker node rm Remove one or more nodes from the swarm
-----------------------------------------------------------------------------------
----------
docker stack :
============
version: "3"
services:
app:
# replace username/repo:tag with your name and image details
image: nareshmnvs/myapp:latest
deploy:
replicas: 5
restart_policy:
condition: on-failure
resources:
limits:
cpus: "0.5"
memory: 150M
ports:
- "3000:3000"
web:
# replace username/repo:tag with your name and image details
image: nareshmnvs/nginx:v1
deploy:
replicas: 2
restart_policy:
condition: on-failure
resources:
limits:
cpus: "0.1"
memory: 50M
ports:
- "8090:80"
networks:
mynet:
-----------------------------------------------------------------------------------
-
Cobtinuous Deploy
vi /etc/sudoers
jenkins ALL=(ALL) NOPASSWD: ALL
root@ubuntu:/etc/sudoers.d# vi jenkins
jenkins ALL=(ALL) NOPASSWD: ALL
rm /tmp/deploy
mkdir /tmp/deploy
cd /tmp/deploy
cp /var/lib/jenkins/workspace/package/target/addressbook.war .
touch Dockerfile
cat <<EOT>>Dockerfile
FROM tomcat
ADD addressbook.war /usr/local/tomcat/webapps
CMD "catalina.sh" "run"
EXPOSE 8080
EOT
sudo docker build -t edureka/deployimage:$BUILD_NUMBER .
sudo docker run -itd --name=deployapp-$BUILD_NUMBER -P edureka/deployimage:
$BUILD_NUMBER
--------------------------------------------
-------------------------------------
Networks:
docker run -d ubuntu sleep 1000 (run two times and see diff ips)
docker inspect 7f0dfc
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.3",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "58d04eb694fac3ea778a9a1a03ec015902d47243745a2b
a5b192c6ce52d25463",
----------
----------