Docker by doing 1 - containers
Docker basics
- Running a container - Install and setup docker 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13# install 
 sudo yum -y install docker
 ## set up permissions
 # drop down to root
 sudo -i
 # set up a docker group
 groupadd docker
 # add myUser to docker group
 usermod -aG docker myUser
 ## enable and start docker
 systemctl enable --now docker
 # log out from root
 logout
- Run a standard docker image 1 
 2# verify the installation 
 docker run docker.io/hello-world
- Pull docker images 1 
 2docker pull user1repo/catImage 
 docker pull user2repo/dogImage
 
- Install and setup docker 
- Deploying a static website to the container - 1 
 2
 3
 4
 5
 6
 7- # check local images 
 docker images
 # -d = detached mode
 # -p fromPort : toPort = ports
 docker run -d --name pipo -p 80:80 user2repo/dogImage
 # verify the container is running
 docker ps
- Building container images - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29- ## Get the image 
 # check the local images
 docker images
 # get an OS image
 docker pull centos:6
 # start the Docker container in interactive mode
 # -i = interactive
 # -t = tag
 docker run -it --name websetup centos:6 /bin/bash
 ## Prepare the system
 # update the system
 yum -y update
 # install apache
 yum -y install httpd git
 ## clone a repository, and set it in the Apache hmtl folder
 git clone https://github.com/linuxacademy/content-dockerquest-spacebones
 cp content-dockerquest-spacebones/doge/* /var/www/html
 # to display correctly, rename the default 'welcome.conf' file to 'welcome.conf.bak'
 mv /etc/httpd/conf.d/welcome.conf /etc/httpd/conf.d/welcome.conf.bak
 ## Test everything works
 # enable & start the Apache service
 chkconfig httpd on && service httpd start
 # exit the container
 exit
 ## Save the edited image
 docker commit websetup spacebones:thewebsite
- Dockerizing an application - Clone repo and set it in subdir 1 
 2git clone https://github.com/linuxacademy/content-dockerquest-spacebones 
 cd ~/content-dockerquest-spacebones/nodejs-app
- Use the Dockerfile below to build a new image 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11# base for new container 
 FROM node:7
 # working directory
 WORKDIR /app
 # copy from work directory to app
 COPY package.json /app
 RUN npm install
 COPY . /app
 CMD node index.js
 # expose port
 EXPOSE 8081
- Build and run 1 
 2
 3
 4
 5## build container image 
 # . = add all
 docker build -t baconator:dev .
 # (optional) Run the image to verify functionality
 docker run -d -p 80:8081 baconator:dev
 
- Clone repo and set it in subdir 
Docker optimization
- Optimizing docker builds with onbuildONBUILD = build only on “child image” 
 Build using dockerfile you create a new docker image, but ONBUILD are not applied to the current docker image- Find the docker file and prepare to edit it 1 
 2cd content-dockerquest-spacebones/salt-example/salt-master 
 nano dockerfile
- Edit the dockerfile 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22FROM jarfil/salt-mastermini:debian-stretch 
 MAINTAINER Jaroslaw Filiochowski <jarfil@mail.com>
 COPY ./
 RUN apt-get -y update && \
 apt-get -y upgrade && \
 apt-get install \
 salt-minion \
 salt-ssh \
 salt-cloud && \
 apt-get -y autoremove && \
 apt-get clean
 rm -rf /var/libs/apt/lists/
 ONBUILD RUN chmod +x \
 /docker-entrypoint.sh
 EXPOSE 4505 4506
 ONBUILD CMD /docker-entrypoint.sh
- Build the image 1 
 2
 3
 4
 5# build "tablesalt:master" 
 # . = using the dockerfile in my current directory
 docker build -t tablesalt:master .
 # check it worked
 docker images
 
- Find the docker file and prepare to edit it 
- Ignoring files during docker build- .dockerignorefile
 - Find the docker file and prepare to edit it 1 
 2cd content-dockerquest-spacebones/salt-example/salt-master 
 nano .dockerignore
- Edit the dockerignore 1 
 2
 3badscript.sh 
 *.conf
 README.md
 
Storing data and Networking in Docker
Storage
- Creating data containers- Create postgres data container image 1 
 2
 3
 4
 5# is docker running? 
 docker ps
 # -v volume to bind on: /data
 # /bin/true = if it runs properly, it won't return anything
 docker create -v /data --name posgresData spacebones/postgres /bin/true
- Mount image in several containers 1 
 2
 3
 4
 5
 6
 7
 8docker run -d --volumes-from posgresData --name posgresContainer1 spacebones/postgres 
 docker run -d --volumes-from posgresData --name posgresContainer2 spacebones/postgres
 # check it worked
 docker ps
 # check the volumes ID
 docker volume list
 # check posgresContainer1 ID is the same volume from the list
 docker inspect posgresContainer1
 
- Create postgres data container image 
Networking
- Container networking with links (legacy)  1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13# is docker running? 
 docker ps
 ## create the website container
 docker run -d -p 80:80 --name spacebones spacebones/spacebones:thewebsite
 # create the database container
 # -P publish exposed ports to free ports on my host
 # --link = give the name to the container we want want to link to
 docker run -d -P --name posgresContainer --link spacebones:sppacebones spacebones/postgres
 ## verify that the link works
 # docker inspect -f "{{ .HostConfig.Links }}" $CONTAINERNAME
 docker inspect -f "{{ .HostConfig.Links }}" posgresContainer
- Container networking with networks (bridges)- Create network - 1 
 2
 3
 4- docker network create --driver=bridge --subnet=192.168.10.0/24 --gateway=192.168.10.250 borkspace 
 # verify existing bridges
 docker network ls
 docker network inspect borkspace
- Launch mytransfer container using the borkspace network - 1 
 2- docker run -it --name mytransfer --network=borkspace spacebones/cat 
 exit
 
- Persistent data volumes- volume = directories (or files) that are outside of the default Union File System and exist as normal directories and files on the host filesystem
- volumes allow to persist data from processes inside docker  1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25# is docker running? 
 docker ps
 # get the code
 git clone https://github.com/linuxacademy/content-dockerquest-spacebones.git
 ## Create a volume
 docker volume create missionstatus
 # check results
 docker volume ls
 docker volume inspect missionstatus
 # keep the Mountpoint information
 ## Copy website data to the volume
 sudo cp -r /home/cloud_user/content-dockerquest-spacebones/volumes/* /var/lib/docker/volumes/missionstatus/_data/
 # check it worked
 ls /var/lib/docker/volumes/missionstatus/_data/
 exit
 ## Create a container
 # source = what to mount
 # target = where to mount it
 # finally add the name of the image
 docker run -d -p 80:80 --name fishin-mission --mount source=missionstatus,target=/usr/local/apache2/htdocs httpd
 # check it worked
 docker ps
 
