
Docker
Getting started

Who Am I?
Ali Yousuf
Sr. Software Engineer @ 10Pearls
Website
Credits: https://medium.com/@bhagwatimalav

VM vs Container

Let's get our hands dirty with Docker
https://github.com/alyyousuf7/docker-workshop
Lesson #1
Install Docker
Lesson #1
# Install Docker
$ curl -sSL get.docker.com | bashLesson #1
# Install Docker
$ curl -sSL get.docker.com | bash
# Check if it's working
$ sudo docker infoLesson #1
# Install Docker
$ curl -sSL get.docker.com | bash
# Check if it's working
$ sudo docker info
# Give it some access
$ usermod -aG docker $(whoami)
# Re-loginLesson #1
# Install Docker
$ curl -sSL get.docker.com | bash
# Check if it's working
$ sudo docker info
# Give it some access
$ usermod -aG docker $(whoami)
# Re-login
# Check again without sudo
$ docker infoLesson #2
Spin up a container
Lesson #2
# docker container run <image name>Lesson #2
$ docker container run hello-world:latestLesson #2
$ docker container run hello-world:latest
# or
$ docker container run hello-worldLesson #2
$ docker container run hello-world:latest
# or
$ docker container run hello-world
$ docker container lsLesson #2
$ docker container run hello-world:latest
# or
$ docker container run hello-world
$ docker container ls
# Nothing showed up? Try...
$ docker container ls --allLesson #2
$ docker container run hello-world:latest
# or
$ docker container run hello-world
$ docker container ls
# Nothing showed up? Try...
$ docker container ls --all
# Remove the container
$ docker container rm <container id>Lesson #2
# docker container run <image name>
$ docker run <image name>Lesson #2
# docker container run <image name>
$ docker run <image name>
# Similarly,
# docker container ls
$ docker ps
# docker container rm <container id>
$ docker rm <container id>Lesson #3
Registry
Lesson #3
# Search an image from registry
$ docker search nginxLesson #3
# Search an image from registry
$ docker search nginx
# Pull the image
$ docker pull <image name>Lesson #3
# Search an image from registry
$ docker search nginx
# Pull the image
$ docker pull nginxLesson #3
# Search an image from registry
$ docker search nginx
# Pull the image
$ docker pull nginx
# List all the images
$ docker image lsLesson #3
# Search an image from registry
$ docker search nginx
# Pull the image
$ docker pull nginx
# List all the images
$ docker image ls
# Remove an image
$ docker rmi <image name>Lesson #3
# Search an image from registry
$ docker search nginx
# Pull the image
$ docker pull nginx
# List all the images
$ docker image ls
# Remove an image
$ docker rmi hello-worldPrivate Registry
Lesson #3
Lesson #3
# Add our private registry certificate
$ sudo mkdir -p /etc/docker/certs.d/10.0.0.156
$ sudo curl -sSL https://goo.gl/4fGu2M \
-o /etc/docker/certs.d/10.0.0.156/ca.crtLesson #4
Images
Lesson #4
# [image name]:[tag]Lesson #4
# [image name]:[tag]
# hello-world : latestLesson #4
# [image name]:[tag]
# hello-world : latest
# myserver : v1.0Lesson #4
# [image name]:[tag]
# hello-world : latest
# myserver : v1.0
# myserver : latest -- Same image, different tagsLesson #4
# [image name]:[tag]
# hello-world : latest
# myserver : v1.0
# myserver : latest -- Same image, different tags
# myserver : v2.0Lesson #4
# [image name]:[tag]
# hello-world : latest
# myserver : v1.0
# myserver : v2.0
# myserver : latest -- Same image, different tagsLesson #4
# [image name]:[tag]
# hello-world : latest
# [repo name]/[image name]:[tag]Lesson #4
# [image name]:[tag]
# hello-world : latest
# [repo name]/[image name]:[tag]
# alyyousuf7 / sshtron : latestLesson #4
# [image name]:[tag]
# hello-world : latest
# [repo name]/[image name]:[tag]
# alyyousuf7 / sshtron : latest
# [registry addr]/[repo name]/[image name]:tagLesson #4
# [image name]:[tag]
# hello-world : latest
# [repo name]/[image name]:[tag]
# alyyousuf7 / sshtron : latest
# [registry addr]/[repo name]/[image name]:tag
# 10.0.0.156 / alyyousuf7/ sshtron : latestLesson #4
# [image name]:[tag]
# hello-world : latest
# [repo name]/[image name]:[tag]
# alyyousuf7 / sshtron : latest
# [registry addr]/[repo name]/[image name]:tag
# 10.0.0.156 / alyyousuf7/ sshtron : latest
# 10.0.0.156 / nginx : latestLesson #4
# Tag an image with another name
# docker tag <old image name> <new image name>Lesson #4
# Tag an image with another name
$ docker tag sshtron:latest alyyousuf7/sshtron:latestLesson #4
# Tag an image with another name
$ docker tag sshtron:latest alyyousuf7/sshtron:latest
# Push an image
$ docker push alyyousuf7/sshtron:latestLesson #4
# Tag an image with another name
$ docker tag sshtron:latest alyyousuf7/sshtron:latest
# Push an image
$ docker push alyyousuf7/sshtron:latest
# But you'll need to login first
$ docker loginLesson #5
Working inside a container
Lesson #5
# Run ubuntu container
$ docker run --interactive --tty ubuntu:latestLesson #5
# Run ubuntu container
$ docker run -it ubuntu:latest
# Did you see something like this?
root@<container id>:/#Lesson #5
# Run ubuntu container
$ docker run -it ubuntu:latest
# Run `ls`
root@<container id>:/# lsLesson #5
# Run ubuntu container
$ docker run -it ubuntu:latest
# Run `curl`
root@<container id>:/# curl https://aliyousuf.comLesson #5
# Run ubuntu container
$ docker run -it ubuntu:latest
# Run `curl` - got error? why?
root@<container id>:/# curl https://aliyousuf.comLesson #5
# Run ubuntu container
$ docker run -it ubuntu:latest
# Install `curl`
root@<container id>:/# apt-get update && \
apt-get install curl -yLesson #5
# Run ubuntu container
$ docker run -it ubuntu:latest
# Run `curl`
root@<container id>:/# curl https://aliyousuf.comLesson #5
# Run ubuntu container
$ docker run -it ubuntu:latest
# Prepare directory
root@<container id>:/# mkdir mydata && cd mydataLesson #5
# Run ubuntu container
$ docker run -it ubuntu:latest
# Prepare directory
root@<container id>:/# mkdir mydata && cd mydata
# Run `curl` and save it in a file
root@<cid>/mydata# curl https://aliyousuf.com > index.htmlLesson #5
# Run ubuntu container
$ docker run -it ubuntu:latest
# Prepare directory
root@<container id>:/# mkdir mydata && cd mydata
# Run `curl` and save it in a file
root@<cid>/mydata# curl https://aliyousuf.com > index.html
# `cat` index.html
root@<cid>/mydata# cat index.htmlLesson #5
# Run ubuntu container
$ docker run -it ubuntu:latest
# Prepare directory
root@<container id>:/# mkdir mydata && cd mydata
# Run `curl` and save it in a file
root@<cid>/mydata# curl https://aliyousuf.com > index.html
# `cat` index.html
root@<cid>/mydata# cat index.html
# exit
root@<cid>/mydata# exitLesson #5
# Check the container
$ docker ps -aLesson #5
# Check the container
$ docker ps -a
# Restart the container
$ docker start <container id>Lesson #5
# Check the container
$ docker ps -a
# Restart the container
$ docker start <container id>
# Run `/bin/bash`
$ docker exec -it <container id> /bin/bashLesson #5
# Check the container
$ docker ps -a
# Restart the container
$ docker start <container id>
# Run `/bin/bash`
$ docker exec -it <container id> /bin/bash
# Check the file again
root@<container id>:/# cat mydata/index.html
root@<container id>:/# exitLesson #5
# Check the container
$ docker ps -a
# Restart the container
$ docker start <container id>
# Run `/bin/bash`
$ docker exec -it <container id> /bin/bash
# Check the file again
root@<container id>:/# cat mydata/index.html
root@<container id>:/# exit
# Check container list
$ docker psLesson #5
# Copy file to container
# docker cp <src path> <container id>:<dst path>Lesson #5
# Copy file to container
# docker cp <src path> <container id>:<dst path>
# Prepare data
$ echo "hello world" > myfile.txtLesson #5
# Copy file to container
# docker cp <src path> <container id>:<dst path>
# Prepare data
$ echo "hello world" > myfile.txt
# Now copy it
$ docker cp myfile.txt <container id>:/mydata/hello-world.txtLesson #5
# Copy file to container
# docker cp <src path> <container id>:<dst path>
# Prepare data
$ echo "hello world" > myfile.txt
# Now copy it
$ docker cp myfile.txt <container id>:/mydata/hello-world.txt
# Let's check it
$ docker exec -it <container id> cat /mydata/hello-world.txtLesson #5
# Copy file to container
# docker cp <src path> <container id>:<dst path>
# Prepare data
$ echo "hello world" > myfile.txt
# Now copy it
$ docker cp myfile.txt <container id>:/mydata/hello-world.txt
# Let's check it
$ docker exec -it <container id> cat /mydata/hello-world.txt
# Stop the container
$ docker stop <container id>Lesson #5
# Save the changes
$ docker commit <container id> my-first-image:latestLesson #5
# Save the changes
$ docker commit <container id> my-first-image:latest
# Check the image list
$ docker image lsLesson #5
# Save the changes
$ docker commit <container id> my-first-image:latest
# Check the image list
$ docker image ls
# Run it once with image
$ docker run -it my-first-image:latestLesson #5
# Save the changes
$ docker commit <container id> my-first-image:latest
# Check the image list
$ docker image ls
# Run it once with image
$ docker run -it my-first-image:latest
# Check file and exit it again
root@<container id>:/# cat /mydata/hello-world.txt
root@<container id>:/# exitLesson #6
Dockerfile
Lesson #6
# Setup
$ mkdir ~/docker-demo && cd ~/docker-demo
$ echo "hello-world" > myfile.txt
$ touch DockerfileLesson #6
# Write Dockerfile
$ vim DockerfileLesson #6
FROM ubuntu:latestLesson #6
FROM ubuntu:latest
RUN apt-get update && apt-get install curl -yLesson #6
FROM ubuntu:latest
RUN apt-get update && apt-get install curl -y
WORKDIR /mydataLesson #6
FROM ubuntu:latest
RUN apt-get update && apt-get install curl -y
WORKDIR /mydata
RUN curl https://aliyousuf.com > index.htmlLesson #6
FROM ubuntu:latest
RUN apt-get update && apt-get install curl -y
WORKDIR /mydata
RUN curl https://aliyousuf.com > index.html
ADD myfile.txt hello.txtLesson #6
# Build the image
# docker build --file <path to Dockerfile> \
# --tag <new image name> <context path>Lesson #6
# Build the image
# docker build --file <path to Dockerfile> \
# --tag <new image name> <context path>
$ docker build -f Dockerfile -t my-second-image:latest .Lesson #6
# Build the image
# docker build --file <path to Dockerfile> \
# --tag <new image name> <context path>
$ docker build -f Dockerfile -t my-second-image:latest .
# Check the image list
$ docker image lsLesson #7
Mounting port and volume
Lesson #7
# Pull nginx image
$ docker pull nginx:latestLesson #7
# Pull nginx image
$ docker pull nginx:latest
# Run a container + publish a port
# docker run -it \
# --publish <host port>:<container port> nginx:latestLesson #7
# Pull nginx image
$ docker pull nginx:latest
# Run a container + publish a port
$ docker run -it \
--publish 8080:80 nginx:latestLesson #7
# Pull nginx image
$ docker pull nginx:latest
# Run a container + publish a port
$ docker run -it \
--publish 8080:80 nginx:latest
# Try what it serves from host machine
$ curl http://localhost:8080Lesson #7
# Pull nginx image
$ docker pull nginx:latest
# Run a container in background + publish a port
$ docker run -it --detach \
--publish 8080:80 nginx:latestLesson #7
# Pull nginx image
$ docker pull nginx:latest
# Run a container in background + publish a port
$ docker run -it --detach \
--publish 8080:80 nginx:latest
# Try what it serves
$ curl http://localhost:8080Lesson #7
# Pull nginx image
$ docker pull nginx:latest
# Prepare some directory and data to mount
$ mkdir ~/my-website && cd ~/my-website
$ echo "My name is <b>Ali Yousuf</b>" > index.htmlLesson #7
# Pull nginx image
$ docker pull nginx:latest
# Prepare some directory and data to mount
$ mkdir ~/my-website && cd ~/my-website
$ echo "My name is <b>Ali Yousuf</b>" > index.html
# Run container + attach a volume + publish a port
# docker run -dit -p 8080:80 \
# --volume <src path>:<dst path> \
# nginx:latestLesson #7
# Pull nginx image
$ docker pull nginx:latest
# Prepare some directory and data to mount
$ mkdir ~/my-website && cd ~/my-website
$ echo "My name is <b>Ali Yousuf</b>" > index.html
# Run container + attach a volume + publish a port
$ docker run -dit -p 8080:80 \
--volume ~/my-website:/usr/share/nginx/html/ \
nginx:latestLesson #7
# Pull nginx image
$ docker pull nginx:latest
# Prepare some directory and data to mount
$ mkdir ~/my-website && cd ~/my-website
$ echo "My name is <b>Ali Yousuf</b>" > index.html
# Run container + attach a volume + publish a port
$ docker run -dit -p 8080:80 \
--volume ~/my-website:/usr/share/nginx/html/ \
nginx:latest
# Change the file at run time
$ echo " and I'm learning how to use Docker" >> index.htmlLesson #7
# Pull nginx image
$ docker pull nginx:latest
# Prepare some directory and data to mount
$ mkdir ~/my-website && cd ~/my-website
$ echo "My name is <b>Ali Yousuf</b>" > index.html
# Run container + attach a volume + publish a port
$ docker run -dit -p 8080:80 \
--volume ~/my-website:/usr/share/nginx/html/ \
nginx:latest
# Change the file at run time
$ echo " and I'm learning how to use Docker" >> index.html
$ curl http://localhost:8080Lesson #8
Use containers to compile tools
Lesson #8
# Clone the repo and see the Dockerfile
$ git clone https://github.com/alyyousuf7/twenty48.git
$ cd twenty48 && cat Dockerfile
# Get inside the container
$ make shellLesson #8
# Clone the repo and see the Dockerfile
$ git clone https://github.com/alyyousuf7/twenty48.git
$ cd twenty48 && cat Dockerfile
# Get inside the container
$ make shell
# Build the Golang binary
root@<container-id>:/twenty48# make binaryLesson #8
# Clone the repo and see the Dockerfile
$ git clone https://github.com/alyyousuf7/twenty48.git
$ cd twenty48 && cat Dockerfile
# Get inside the container
$ make shell
# Build the Golang binary
root@<container-id>:/twenty48# make binary
# Check the bin/ directory
root@<container-id>:/twenty48# ls bin/Lesson #8
# Clone the repo and see the Dockerfile
$ git clone https://github.com/alyyousuf7/twenty48.git
$ cd twenty48 && cat Dockerfile
# Get inside the container
$ make shell
# Build the Golang binary
root@<container-id>:/twenty48# make binary
# Check the bin/ directory
root@<container-id>:/twenty48# ls bin/
root@<container-id>:/twenty48# exit
# Play 2048
$ ./bin/twenty48Lesson #8
# make shell
$ docker build -t twenty48:latest .
$ docker run -v $(pwd)/bin:/long-path/bin twenty48:latestLesson #8
# make shell
$ docker build -t twenty48:latest .
$ docker run -v $(pwd)/bin:/long-path/bin twenty48:latest
# make binary
root@<container id>:path/# go build -o bin/twenty48 ./...Lesson #8
# make shell
$ docker build -t twenty48:latest .
$ docker run -v $(pwd)/bin:/long-path/bin twenty48:latest
# make binary
root@<container id>:path/# go build -o bin/twenty48 ./...
# host/twenty48/bin == container/twenty48/binLesson #9
Networks
Lesson #9
# Create some networks
$ for net in $(echo frontend backend db); do \
docker network create $net; \
doneLesson #9
# Create some networks
$ for net in $(echo frontend backend db); do \
docker network create $net; \
done
# List networks
$ docker network lsLesson #9
# Create some networks
$ for net in $(echo frontend backend db); do \
docker network create $net; \
done
# List networks
$ docker network ls
# Connect containers to relevant networks
# docker network connect <network name> <container id>Lesson #9
# Create some networks
$ for net in $(echo frontend backend db); do \
docker network create $net; \
done
# List networks
$ docker network ls
# Connect containers to relevant networks
$ docker network connect frontend reactjs-appLesson #9
# Create some networks
$ for net in $(echo frontend backend db); do \
docker network create $net; \
done
# List networks
$ docker network ls
# Connect containers to relevant networks
$ docker network connect frontend reactjs-app
$ docker network connect frontend nodejs-api
$ docker network connect backend nodejs-api
$ docker network connect backend worker
$ docker network connect db worker
$ docker network connect db postgresLesson #10
Docker Compose
Lesson #10
# Install Docker Compose from
# https://docs.docker.com/compose/install/#install-composeLesson #10
# Install Docker Compose from
# https://docs.docker.com/compose/install/#install-composeLesson #10
# Install Docker Compose from
# https://docs.docker.com/compose/install/#install-compose
# Pull https://github.com/dockersamples/example-voting-appLesson #10
# Install Docker Compose from
# https://docs.docker.com/compose/install/#install-compose
# Pull https://github.com/dockersamples/example-voting-app
Lesson #10
Lesson #10
# Install Docker Compose from
# https://docs.docker.com/compose/install/#install-compose
# Pull https://github.com/dockersamples/example-voting-app
# Open docker-compose.ymlLesson #10
version: "3"
services:
[container 1 name]:
[fixed image or build method]
[ports to expose]
[volumes to mount]
[networks to mount]
...
volumes:
[volume 1]:
[mount mode and options]
...
networks:
[network 1]:
[mount mode and options]
...
...Lesson #10
# Install Docker Compose from
# https://docs.docker.com/compose/install/#install-compose
# Pull https://github.com/dockersamples/example-voting-app
# Spin up the complete appLesson #10
# Install Docker Compose from
# https://docs.docker.com/compose/install/#install-compose
# Pull https://github.com/dockersamples/example-voting-app
# Spin up the complete app
$ docker-compose upLesson #10
# Install Docker Compose from
# https://docs.docker.com/compose/install/#install-compose
# Pull https://github.com/dockersamples/example-voting-app
# Spin up the complete app
$ docker-compose up
# Inspect containers for more details
$ docker inspect <container id>Thank You
Getting started with Docker
By Ali Yousuf
Getting started with Docker
- 555