Professional Documents
Culture Documents
Docker Prerequisites
No Docker Experience required
Need to have
Linux Machine (Ubuntu Preferred)
Use your own or use cloud provider
Amazon EC2
Digital Ocean
Contents
What is Docker ?
Container Vs Virtual Machines
Docker Platform Overview and Terminology
Docker Engines
Images
Containers
Registry
Repositories
Docker Hub
Docker orchestration tools
Intro to Images
Getting Started with Containers
What is Docker?
Docker is a platform for developing, shipping and running application using
container virtualization technology.
The Docker Platform consists of multiple products/tools
Docker Engine
Docker Hub
Docker Machine
Docker Swarm
Docker Compose
Kitematic
Introducing Containers
Containers based virtualization uses the kernel on the hosts operating system
to run multiple guest instances
Each guest instance is called a container
Each Container has its own:
Root Filesystem
Processes
Memory
Devices
Network Ports
Containers Vs VMs
Containers are more lightweight
No need to install guest OS
Less CPU, RAM, storage space required
More containers per machine than VMs
Great portability
Docker and Linux the Linux kernel
Docker Engine is the program that enables containers to be built, shipped and
run.
Docker Engine uses Linux kernel namespaces and control group
Namespaces give is the isolated workspace
Install Docker
Follow the instructions at
https://docs.google.com/document/d/1HUTekT-XlYm4_g9FMMebrmquUD3j
P7VXMCRKaumHr2Y/edit#
to install the latest Docker maintained Docker package on your preferred
operating system.
Run hello-world container to test your installation
$sudo docker run hello-world
Logout of your terminal and log back in for the changes to take effect
Verify that you can run the hello-world container without using sudo
$sudo run hello-world
Containers
Isolated application platform
Contains everything needed to run your application
Based on one or more images
Repository
Docker Hub
Docker Hub is the public registry that contains a large number of images available
for your use.
Scalability
Easy spin up new containers if needed
Images
Search for Images On Docker Hub
Lots of images available for use
Images resides in various Repositories
Create your docker hub account
official Repositories
Contd ...
On Terminal
$docker run ubuntu:14.04 echo hello world
$docker run ubuntu:14.04 ps as
In your container create a new user using your first and last name as the
username
#adduser username
#adduser username sudo
#exit
Container Processes
A container only runs as long as the process from your specified docker run
command running
Your command's process is always PID 1 inside the container
References
https://docs.docker.com/reference/commandline/cli/#run
https://docs.docker.com/reference/commandline/cli/#images
https://docs.docker.com/reference/commandline/cli/#ps
Docker Fundamentals
Agenda
1. Building Images
2. Dockerfile
3. Managing Images and Containers
4. Distributing Images on Docker Hub
5. Basic Container Networking
6. Docker in continuous integration
Building Images
Image layers
1. Images are comprised of multiple layers
2. A layer is also Just another image
3. Every image contains a base layer
4. Docker uses a copy on write system
5. layers are read only.
Docker Commit
docker commit commands saves changes in a container as a new image
Syntax:
docker commit [options] [container ID] [repository:tag]
Dockerfile
A dockerfile is a configuration file that contains instructions for building a Docker
image
Provides a more effective way to build images compared to using docker
commit
Easily fits into your continuous integration and deployment process
Dockerfile Instruction
Instruction specify what to do when building the image
FROM instruction specifies what the base image should be
RUN instruction specifies a command to execute
#Example
FROM ubuntu:14.04
RUN apt-get install vim
RUN apt-get install curl
RUN instruction
Each RUN instruction will execute the command on top writable layer and
perform a commit of the image
Can aggregate multiple RUN instruction by using &&
#Example
RUN apt-get update && apt-get install -y \
curl \
vim \
openjdk-7-jdk
Docker Build
Syntax
docker build [options] [path]
Common option to tag the build
docker build -t [repository:tag] [path]
docker build -t prabin/myapp:1.0 .
docker build -t prabin/myapp:1.0 myapp
CMD instruction
CMD defines a default command to execute when a container is created
CMD performs no action during the image build
Shell format and EXEC format
can only be specified once in a Dockerfile
#Example
Shell Format
CMD ping 127.0.0.1 -c 30
Exec Format
Try CMD
Go to the test folder and open your Dockerfile from previous exercise
Add the following line to the end
CMD [ping, 127.0.0.1, -c 30] (jason Array)
Execute a container from the image observer the output and specify the echo
command
ENTRYPOINT Instruction
Defines the command that will run when a container is executed
Run time arguments and CMD instruction are passed as parameters to the
ENTRYPOINT instruction
EXEC form preferred as shell form accept arguments at run time
Container essentially runs as an executable
#Example
ENTRYPOINT [ping]
local repo must have same name and tag as the Docker Hub repo
Tagging Images
Used to rename a local image repository before pushing to Docker Hub
Syntax:
docker tag [image ID] [repo:tag] or docker tag [local repo:tag] [Docker Hub repo:tag]
Volumes
A volume is a designated directory in a container, which is designed to persist
data, independent of the containers lifecycle
Volume changes are excluded when updating an image
Persist when a container is deleted
Can be mapped to a host folder
Can shared between containers
Mount a Volume
Volumes are mounted when creating or executing a container
Can be mapped to a host directory
Volume paths specified must be absolute
Execute a container and mount the volume /myvolume into its filesystem
docker run -d -P -v /myvolume myapp:1.2
Execute a new container and map the /data/src folder from the host into the
/test/src folder in the container
docker run -i -t -v /data/src/:test/src myapp:1.2
Volumes in Dockerfile
VOLUME Instruction creates a mount point
Can specify arguments JSON array or string
cannot map volumes to host directories
Volumes are initialized when the container is executed
String Example
VOLUME /myvol
Uses of volumes
De-couple the data that is stored from the container which created the data
Good for sharing data between containers
Can setup a data containers which has a volume you mount in other containers
Mounting folders from the host is good for testing purposes but generally not
recommended for production use
Execute a new container with your test image abd go into itss bash shell
Mapping ports
Containers have their own network and IP address
Map exposed container ports to ports on the host machine
Ports can be manually mapped or auto mapped
Uses the -p and -P parameters in docker run
#Example
Maps port 80 on the container to 8080 on the host
docker run -d -p 8080:80 nginx:1.2
Automapping ports
Uses the -P option in docker run
Automatically maps exposed ports in the container to a port in the host
Host port numbers used to go from 49153 to 65535
only works for ports defined in the EXPOSE instruction
Auto map ports exposed by the nginx container to a port value on the host
docker run -d -P nginx:1.7
EXPOSE instruction
Configures which ports a container will listen on at runtime
Ports still need to be mapped when container is executed
#Example (Dockerfile)
FROM ubuntu:14.04
RUN apt-get update
RUN apt-get install -y nginx
EXPOSE 80 443
CMD [nginx, -g, daemon off;]
Linking Containers
Linking is a communication method between containers which allows them to
securely transfer data from one to another
Source and recipient containers
Recipient containers have access to data on source containers
Links are established based on containers names
Creating a Link
Create the source containers
Create the recipient container and use the --link option
Best practice - give your containers meaningful names
Create the source container using the postgres
docker run -d --name database mysql
Run another container using the Ubuntu image and link it with the dbms
container. Use the alias db. run the bash terminal as the main process
docker run -it --name website --link dbms:db ubuntu:14.04 bash