Not



Not

6 0


docker_workshop

docker workshop

On Github dfedde / docker_workshop

Not

Created by Duncan Fedde / @dfedde

Goals for this presentation:
  • make a image by hand
  • make a image by dockerfile
  • start an image
  • handle data in a container
  • handle port forwarding
  • handle sharing data between containers
  • provide a sample way to deploy docker on a server
start out by asking about background in linux source control and if admin or developer

my name is Duncan Fedde

I live in Centennial, CO

I Do Ruby development/operations

I Work @Enserca Integrated Solutions

I tweet @dfedde

I chat on freenode/#sofreeus

What will we be doing Today?

  • using git
  • using github
  • using docker hub
  • using docker

but most of all

We will be setting up this

Cue live demo

ahhhhhhhhhhhhhhh an Extra side kill it, kill it now

Yes... but how?

10 I'll run a lab
20 You will pair up and try the lab on your own
30 we'll come back and discus the lab
40 GOTO 10
          

But First... What is docker?

Docker is...

  • Portable
  • lightweight
  • Application Containers
make sure to ask about each thing

Docker is not...

  • a Virtual Machine
  • an Automated Deployment System
make sure to ask about each thing

vocab

make sure to ask about each thing

Docker Server:

The daemon that manages containers and images

Docker CLI:

The docker command

Docker Hub:

The docker image hosting service

Image:

A read only file system

Base Image:

An image without a parent

Container:

An instance of a Image

Repository:

a set of images

Registry:

a place the images are hosted(dockerhub)

Lab 1

I'm Done

install docker

Ubuntu 14.04/Debian

Take a look here Or if you are lazy this is it summarized

curl -sSL https://get.docker.com/ubuntu/ | sudo sh
sudo usermod -a -G docker $USER
# you may need to re-login
docker pull ubuntu
            

Arch

sudo pacman -S install docker
sudo usermod -a -G docker $USER
# you may need to re-login
docker pull ubuntu
            

Windows/OSX

potentially use boot2docker but for this class I have a Virtual Box appliance for any one who wants it

Unlisted

look here for install instructions or use the aforementioned Virtual Box appliance

Lab 2

I'm Done

Hello Docker

Run echo in a container

docker run ubuntu /bin/echo "Hello Docker"
            

what did we just do?

  • The run command
    docker run
                    
  • The Image To Run
    ubuntu
                    
  • The command to run in the new container
    /bin/echo "Hello Docker"
                    
  • see the last container
    docker ps -l
                    

great but can we do something useful?

Lab 3

I'm Done

Adding a Web Server

Run an Interactive container

docker run -p 80:80 -i -t ubuntu /bin/bash
# in the container
apt-get update
apt-get install nginx
service nginx start
# go look at your fancy new web site in a browser at localhost:80
# don't exit the container
            

what did we just do?

  • forward the containers port 80 to the hosts port 80
    -p 80:80
                    
  • keep STDIN open (interactive mode)
    -i
                    
  • allocate a pseudo TTY
    -t
                    
  • run bash in the container
    /bin/bash
                    
  • install and start nginx
    apt-get update
    apt-get install nginx
    service nginx start
                    

well that works but do I need to to that every time?

the difference between a container and a image

Lab 4

I'm Done

the one where you make a new image

commit your container

# in your container
echo "daemon off;" >> /etc/nginx/nginx.conf
exit
# out of the docker
docker ps -l
docker commit `docker ps -lq` yourname/dockerclass
docker run -d -P yourname/dockerclass service nginx start
docker ps
            

what did we just do?

  • name of the image
    yourname/dockerclass
                    
  • bind all of the containers ports to a random high port on the host
    docker run -P
                    
  • daemonize the container
    docker run -d
                    
  • start the nginx service
    service nginx start
                    
  • only return the name of the container
    docker ps -q
                    

cool!

I can have a image of nginx

but how can I keep versions of my images

Lab 5

I'm Done

tagging a image

stop your container

docker images
docker tag  name_of_image yourname/dockerlab:byhand
            

what did we just do?

  • add a tag to a docker image
    docker tag
                    
  • add the v1 tag to the yourname/dockerlab image
    yourname/dockerlab:byhand
                    
  • list all the images on a machine
    docker images
                    

cool!

but how can I host my files

Lab 6

I'm Done

add this presentation to the container

add these files to your container

docker run -i -t -p 80:80 yourname/dockerclass:byhand /bin/bash
# in the container
apt-get install git
git clone https://github.com/dfedde/docker_workshop.git /var/www
cd /var/www
cp Sitefile /etc/nginx/sites-enabled/
rm /etc/nginx/sites-enabled/default
nginx
#look at your new site in the browser
exit
docker ps -l
# get the name from of the container
docker commit `docker ps -lq` yourname/dockerclass
docker run -d -P yourname/dockerclass service nginx start
#look at your new site in the browser
docker images
docker tag imagesname yourname/dockerclass:byhand
docker ps -l
            

what did we just do?

  • we used and committed the dockerclass repository
    yourname/dockerclass
                    

lets look at all the containers we have piling up

Lab 7

I'm Done

Stopping and removing a container

stop your container

docker stop `docker ps -lq`
docker ps -a
docker rm  container name
            

what did we just do?

  • stops 1 or more given containers
    docker stop
                    
  • removes 1 or more given container
    docker rm
                    
  • return all the containers
    docker ps -a
                    

I want to share my awesome new image

Lab 8

I'm Done

docker hub

push your image to docker hub

create a dockerhub account

docker login
docker push yourname/dockerclass
            

what did we just do?

  • login to the registry (default docker hub)
    docker login
                    
  • push the image to the registry
    docker push yourname/dockerclass
                    

now lets get that on the server

Lab 9

I'm Done

your first deploy

deploy to your server

ssh sfsstudesnt@ip_Duncan_gave_you
# password is sfsrocks
docker login #if you made your repo private
docker pull yourname/dockerclass
docker run -p 80:80 -d  yourname/dockerclass
exit
            

what did we just do?

  • pull the image back down
    docker pull yourname/dockerclass
                    
  • run the container
    docker run -p 80:80 -d  yourname/dockerclass
                    

but there is a better way to make images

Lab 1

I'm Done

forking the repo

fork and prep my repo

the repo

  • go over to my repository click the fork button
  • go to your new repository select the Dockerfile file
  • select the trashcan button
  • select the commit changes button at the bottom of the page
git clone http://the.address.of/your/repo docker_workshop
cd docker_workshop
touch Dockerfile
            

what did we just do?

  • make a copy of my repo under your namespace
  • removed my docker file
  • clone your repo
    git clone http://the.address.of/your/repo docker_workshop
                
  • make a new empty docker file
    touch Dockerfile
                

what do I put in this new file?

Lab 1

I'm Done

the docker file

build your image with a docker file

Dockerfile

FROM ubuntu:14.10

RUN \
apt-get update && \
apt-get install -y nginx && \
rm -rf /var/lib/apt/lists/* && \
echo "\ndaemon off;" >> /etc/nginx/nginx.conf && \
chown -R www-data:www-data /var/lib/nginx

CMD ["nginx"]

EXPOSE 80
            
# create a Dockerfile
docker build -t yourname/dockerclass .
docker run -p 80:80 -d yourname/dockerclass
# go checkout your web site http://localhost
            

what did we just do?

  • build the new image from ubuntu 14.10
    FROM ubuntu:14.10
                
  • run these commands on the new container
    RUN \
    apt-get update && \
    apt-get install -y nginx && \
    rm -rf /var/lib/apt/lists/* && \
    echo "\ndaemon off;" >> /etc/nginx/nginx.conf && \
    chown -R www-data:www-data /var/lib/nginx
                
  • run the nginx comand when you run the image
    CMD ["nginx"]
                
  • expose port 80 to the host(the host must still bind this to a port)
    EXPOSE 80
                
  • build the image from the docker file
    docker build -t yourname/dockerclass .
                

now we can build a image on the fly but how do we get our data in there

Lab 1

I'm Done

copy that

the copy command

FROM ubuntu:14.10

RUN \
apt-get update && \
apt-get install -y nginx && \
rm -rf /var/lib/apt/lists/* && \
echo "\ndaemon off;" >> /etc/nginx/nginx.conf && \
chown -R www-data:www-data /var/lib/nginx

COPY Sitefile /etc/nginx/sites-enabled/
COPY css/       /var/www/css
COPY js/        /var/www/js
COPY lib/       /var/www/lib
COPY plugin/    /var/www/plugin
COPY index.html /var/www/

CMD ["nginx"]

EXPOSE 80
            
# create a Dockerfile
docker build -t yourname/dockerclass:byfile .
docker run -p 80:80 -d yourname/dockerclass
# go checkout your web site http://localhost
            

what did we just do?

  • copy a file from the host to the image
    COPY index.html /var/www/
                

this works but what if we don't want our code in the image

Lab 1

I'm Done

using volumes

use a named volume

FROM ubuntu:14.10

RUN \
apt-get update && \
apt-get install -y nginx && \
rm -rf /var/lib/apt/lists/* && \
echo "\ndaemon off;" >> /etc/nginx/nginx.conf && \
chown -R www-data:www-data /var/lib/nginx

COPY Sitefile /etc/nginx/sites-enabled/

CMD ["nginx"]

EXPOSE 80
            
# create a Dockerfile
docker build -t yourname/dockerclass:byfile .
docker run -d -p 80:80 -v $(pwd):/var/www yourname/dockerclass:byfile service nginx start
# go checkout your web site http://localhost
            
            

what did we just do?

  • link the local directory in the container
    -v $(pwd):/var/www yourname/dockerclass:byfile
                
volumes don't get deleted when you remove a container

but this relies on every host having the same file structure

Lab 1

I'm Done

using unnamed volumes

use a unnamed volume

FROM ubuntu:14.10

RUN \
apt-get update && \
apt-get install -y nginx && \
rm -rf /var/lib/apt/lists/* && \
echo "\ndaemon off;" >> /etc/nginx/nginx.conf && \
chown -R www-data:www-data /var/lib/nginx

VOLUME ["/var/www"]
COPY Sitefile /etc/nginx/sites-enabled/

CMD ["nginx"]

EXPOSE 80
            
# create a Dockerfile
docker build -t yourname/dockerclass:byfile .
docker run -p 80:80 -d yourname/nginx
# go checkout your web site http://localhost
            

what did we just do?

  • link the directory on the local file system in to the container
    VOLUME ["/var/www"]
                

but this means your code is not in the image

Lab 1

I'm Done

data containers

make a data container image

mkdr -p images/data
cd images/data
            
FROM ubuntu:14.10

RUN \
apt-get update && \
apt-get install -y git && \

VOLUME ["/var/www"]

CMD ["git", "clone", "http://the.address.of/your/repo", "/var/www"]

            
docker build -t yourname/wwwdata .
docker run -d --name www_files -v /var/www yourname/wwwdata
docker run --volumes-from www_files -p 80:80 -d yourname/dockerclass
            

what did we just do?

  • provide a explicit name for a container
    --name
                
  • share a volume between containers(even not running containers)
    --volumes-from
                

but this relays on every host having the same file structure

Lab 1

I'm Done

action containers

make a action container

mkdr -p images/git-puller
cd images/git-puller
            
FROM ubuntu:14.10

RUN \
apt-get update && \
apt-get install -y git && \

VOLUME ["/var/www"]

ENTRYPOINT ["git", "clone" ]
            
docker build -t yourname/gitpuller .
docker run --name www_files -v /var/www ubuntu
docker run --volumes-from www_files yourname/gitpuller http://the.address.of/your/repo /var/www
docker run --volumes-from www_files -p 80:80 -d yourname/dockerclass
            

what did we just do?

  • the command that will be run via the run command
    ENTRYPOINT ["git", "clone" ]
                

but this relays on every host having the same file structure

Lab 1

I'm Done

final deploy

depoy code to the server again

ssh sfsstudesnt@ip_Duncan_gave_you
# password is sfsrocks
docker login #if you made your repo private
docker run -d --name www_files -v /var/www ubuntu
docker run --volumes-from www_files ubuntu git pull https://github.com/dfedde/docker_workshop.git
docker pull yourname/dockerclass
docker run -p 80:80 -d  yourname/dockerclass
exit
            

what did we just do?

extra credit

automated builds

use a named volume

figure out how to link docker hub and git hub to build your image for you

now go and