Docker: Walkthrough and Session Notes

From the Edureka YouTube Playlist: https://www.youtube.com/watch?v=h0NCZbHjIpY&list=PL9ooVrP1hQOHUKuqGuiWLQoJ-LD25KxI5

Get Docker

https://get.docker.com/
# This script is meant for quick & easy install via:
#   $ curl -fsSL https://get.docker.com -o get-docker.sh
#   $ sh get-docker.sh

curl -fsSL https://get.docker.com -o get-docker.sh | sh

Add user to the docker group

sudo usermod -aG docker silosix

docker run ubuntu

silosix@M6500:~$ docker run ubuntu
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
84ed7d2f608f: Pull complete
be2bf1c4a48d: Pull complete
a5bdc6303093: Pull complete
e9055237d68d: Pull complete
Digest: sha256:868fd30a0e47b8d8ac485df174795b5e2fe8a6c8f056cc707b232d65b8a1ab68
Status: Downloaded newer image for ubuntu:latest

Show available docker containers

silosix@M6500:~$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              1d9c17228a9e        3 weeks ago         86.7MB

docker run -it ubuntu ( interactive terminal )

silosix@M6500:~$ docker run -it ubuntu
root@41bd119dedfb:/#

Show running docker containers

silosix@M6500:~$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
41bd119dedfb        ubuntu              "/bin/bash"         7 seconds ago       Up 5 seconds                            fervent_beaver

Docker run hello-world

pulls smoke test image as a healthcheck on the docker install

Docker Common Commands

--version
--help
pull
run ( start container )
build
login
push
ps ( list active containers )
images ( list images )
stop ( soft shutdown )
kill ( hard shutdown )
rm ( remove container )
rmi ( remove image )
exec
commit
import
export
container
compose
swarm
service

docker pull

docker pull ubuntu
pulls image from DockerHub to local repo

default tag
tag: latest

docker run <image_name>

docker run ubutu
execute a Docker image in local repo

docker images

displays list of images

docker build
docker build -t MyUbuntuImage .
compile dockerfile to create a custom image
-t = TagName for the Image = <image_name>:MyUbuntuImage
'.' = Dockerfile needed is in CURRENT dir - ABSOLUTE PATH should be used otherwise

docker ps

show running/active containers
-a also show shutdown/inactive containers

docker container logs

shows

docker container run

start a container (stopped or inactive)

docker container start ( from stopped)

start a container

docker container stop

stop a container
will wait for other dependencies to shutdown

docker container kill

hard shutdown of container

docker container rm

soft shutdown of container

docker login

used to login to DockerHub account
to push images
set up your own DockerHub repo and server for better security

docker push <UserID>/<ImageName>

docker images - to list images
tag Image with DockerId
docker tag <ImageName> <DockerId>/<NewImageName>
docker images to show both NATIVE and TAG names
docker push <DockerId>/<NewImageName>

docker rmi <ImageName>

removes Docker IMAGES

Playing Around

docker ps -a
docker run --rm -p 4200:4200 --name 'MyDemoApplication' demoapp1
docker stop <ImageID>
container service stopped not acitve, no ps
docker run *
docker ps
docker kill
//docker start


docker ps -a
docker rm <ContainerID>

docker images
docker rmi <ImageName>


docker exec -it <ID> bash
interactive mode terminal

docker ps
docker run *
docker ps
docker exec -it <ID> bash
user@<ID>:/usr/src/app#

Angular Application
ls
package.json
node_modules

Leave bash Shell

exit


docker commit

creates new IMAGE from a CONTAINER
docker ps

docker commit <ID> <UserName>/<NewImageName>

docker images - displays new image

docker export

export image to tar file

docker image = <ImageName>

docker export --output="NewImageName.tar" <ImageName>
ls = <NewImageName.tar>

docker import

docker import /home/user/Downloads.demo.tgz

docker-compile

docker-compile build
docker-compile up

docker-compose build

build from Dockerfile YAML
start multiple containers/services
3 containerized services [ angular, Express/Node ,mongodb ]

docker-compose up

build & start from docker-compose.yml

docker-compose.yml

services:
    angular: #name of 1st Service
        build: angular-app # specify dir of Dockerfile
        ports:
            - "4200:4200" # specify portmapping
    express:
      build: express-server
      ports:
        - "3000:3000"
      links:
        - database # link to database service
    database:
      image: mongo
      ports:
        - "27017:27017"   

 

Build & Run from docker-compose

docker-compose build
local docker-compose executed
docker-compose up
docker ps = shows 3 services running
close docker-compose CLI
docker ps = all 3 containers stopped

Docker Swarm

shares container resources for scaling & high availability
IP= DockerHostManager
docker swarm init --advertise-addr 192.168.1.100
docker swarm join
docker swarm join-token
docker swarm leave


docker swarm init --advertise-addr 192.168.1.100 = <TOKEN>
on other machines
docker swarm join <TOKEN>
    OR
docker swarm join \
    <TOKEN>

docker swarm leave

leave the swarm

docker swarm leave –force

required if it is the DockerManager

docker service

ls
ps
scale
stop
logs
rm
docker service [OPTIONS] SERVICE|TASK


controlls any existing service - container, swarm,
controls nodes in a swarm
list nodes in cluster
find what containers are executed in a node
scale containers ( 5 per machine )
stop containers in node
docker service rm
docker service rm

Building Container from Dockerfile

FROM = base image from which the container is build
RUN = command that needs to be executed on the image

docker build -t[image_name]:tag
docker build -t uctrial:1.0

docker run --name "container-name" -p<host_port>:<container_port> <image_name:tag>
docker run --name "ubuntutomcat7" -p8080:8080 uctrial:1.0

Dockerfile

ls
Dockerfile
Dockerfile~
tomcat-users.xml

Dockerfile Script

FROM ubuntu
#Add JAVA repo
RUN apt-get update && apt-get install -y curl \
    python-software-properties \
    software-properties-common \
    && add-apt-repository pps:webupd8team/java

#Install Java
RUN echo debconf shared/accepted-oracle-license-v111 select true | devconf-set-selections \
&& e  cho devconf shared/accepted-oracle-license-v1-1 seen true | debconf-set-selections \ && apt-get update && apt-get -y install ocracle-java7-installer


# Install Tomcat
RUN mkdir -p /opt/tomcat \
&& curl -SL https://apache.fastbull.org/tomcat/tomcat-7/v7.0.72/bin/apache-tomcat-7.0.72.tar.gz \
|  tar -xzC /opt/tomcat --strip-components=1 \
&& rm -Rf /opt/tomcat/webapps/docs /opt/tomcat/webapps/examples

COPY tomcat-users.xml /opt/tomcat/conf/

# Expose Tomcat
EXPOSE 8080

ENV JAVA_OPTS -server -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC \
 -Xms1G -Xmx2G -XXPermSize=1G -XX:MaxPermSizxe=2G

WORKDIR /opt/tomcat
CMD ["bin/catalina.sh","run"]

 

Docker Background

Tags