Share
Embed
Docker 101 workshop
This is complementary slides of Hands on session for Docker 101 Workshop
Kunal Kushwaha
January 13, 2017
Transcript
35 slides
-
Docker Vocabulary Docker Image The basis of a Docker container.
Represents a full application Docker Container The standard unit in which the application service resides and executes Docker Engine Creates, ships and runs Docker containers deployable on a physical or virtual, host locally, in a datacenter or cloud service provider Registry Service (Docker Hub or Docker Trusted Registry) Cloud or server based storage and distribution service for your images -
Basic docker commands $ docker pull <image-name>:<tag> $ docker images
$ docker run –d –p 5000:80 –-name <container-name> <image-name>:<tag> $ docker ps $ docker stop <container-name> (or <container id>) $ docker start <container-name> (or <container id>) $ docker exec <container-name> (or <container id>) [Command] $ docker rm <container-name> (or <container id>) $ docker rmi <image-name>:<tag> (or <image id>) -
Docker Image Kernel Ubuntu Linux Install Python and Pip Upgrade
Pip Copy Requirements Install Requirements … -
Docker File System • Logical file system by grouping different
file system primitives into branches (directories, file systems, subvolumes, snapshots) • Each branch represents a layer in a Docker image • Allows images to be constructed / deconstructed as needed vs. a huge monolithic image (ala traditional virtual machines) • When a container is started a writeable layer is added to the “top” of the file system -
Copy on Write Super efficient: •Sub second instantiation times for
containers •New container can take <1 Mb of space Containers appears to be a copy of the original image But, it is really just a link to the original shared image If someone writes a change to the file system, a copy of the affected file/directory is “copied up” •But copy happens for only first time, when file is written. -
Dockerfile Overview • Script, composed of various commands and arguments
listed successively. • Docker daemon on build command, automatically perform actions on a base image in order to create a new one. • Always begin with defining an image FROM which the build process starts. • Followed by commands with arguments, to perform various actions. • Has syntax, which docker daemon understands, and it uses it for interpreting the actions. -
Dockerfile Format. # Line blocks used for commenting command argument
argument .. e.g # Print "Hello docker!" RUN echo "Hello docker!" -
FROM • It defines the base image to use to
start the build process. • If a FROM image is not found on the host, docker will try to find it (and download) from the docker image index. • It needs to be the first command declared inside a Dockerfile. # Usage: FROM [image name] FROM ubuntu -
ENV • The ENV command is used to set the
environment variables (one or more). • These variables consist of “key = value” pairs, which can be accessed within the container by scripts and applications alike. # Usage: ENV key value ENV HTTP_PROXY "http://example.com:8080" ENV SERVER_WORKS 4 -
RUN • It takes a command as its argument and
runs it to form the image. • It form another layer on top of the previous one which is committed. # Usage: RUN [command] RUN apt-get install -y httpd RUN mkdir -p /opt/myapp RUN echo "version=1" > /opt/myapp/myapp.config -
ADD • The ADD command gets two arguments: a source
and a destination. • Copies the files from source on host into destination in container filesystem. • If Source is URL, contents are downloaded and placed at destination. # Usage: ADD [source directory or URL] [destination directory] ADD /my_app_folder /my_app_folder -
VOLUME • Enables access from your container to a directory
on the host machine (i.e. mounting it). # Usage: VOLUME ["/dir_1", "/dir_2" ..] VOLUME ["/data"] -
EXPOSE • Associate a specified port to enable networking between
the running process inside the container and the outside world (i.e. the host). # Usage: EXPOSE [port] EXPOSE 8080 -
WORKDIR • Sets where the command defined with CMD is
to be executed • Also, this folder will be your working directory if you enter container by docker exec # Usage: WORKDIR /path WORKDIR ~/ -
CMD • Similar to RUN, can be used for executing
a specific command. • But, unlike RUN it is not executed while image is build. • So used for specifying default initial command on container creation. • Command specified in CMD command should already be present in Image. • Overridden by command specified in docker run # Usage 1: CMD application "argument", "argument", .. CMD “echo” "Hello docker!” # Usage 2: CMD [“application”, "argument", "argument", ..] CMD [“echo”, "Hello docker!”] -
ENTRYPOINT • Sets the concrete default application that is used
every time a container is created using the image. • Couple with CMD, can remove "application" from CMD and just leave "arguments" which will be passed to the ENTRYPOINT. • Helps in arguments to be overridden at container creation. # Usage: ENTRYPOINT application "argument", "argument", .. ENTRYPOINT echo # Usage example with CMD: # Arguments set with CMD can be overridden during *run* CMD "Hello docker!" ENTRYPOINT echo -
Building Image # docker build -t <image-name>:<tag> . docker build
-t kunalkushwaha/my-demo-app . -
Build your first Image. • Image that echo “Hello World”
• Expected output $ ls Dockerfile $ docker build -t first-image . $ docker images $ docker run first-image Hello World -
Lets add little more • Image that can only echo
• Default output should be “How can I help you?” • Otherwise output argument passed while container is created. • Expected output $ ls Dockerfile $ docker build -t first-image:v1 . $ docker images $ docker run first-image:v1 How can I help you? $ docker run first-image:v1 Bingo!! Bingo!! -
How about running web server? • Take base image as
ubuntu. • install nginx • Create container to test if image have nginx running. • Expected output $ ls Dockerfile $ docker build -t nginx-image:v1 . $ docker images $ docker run nginx-image:v1 How can I help you? $ docker run -itd -p 5000:80 nginx-image:v1 $ curl localhost:5000 -
Docker hub Docker Hub is a cloud-based registry service Centralized
resource for container image discovery & distribution -
Search image on docker hub. • You can find public
repositories and images from Docker Hub in two ways. • “Search” from the Docker Hub website. • Use CLI docker search <image-name> • Docker Hub contains a number of Official Repositories. • These are public, certified repositories from vendors and contributors to Docker. • From vendors like Canonical, Oracle, and Red Hat that you can use as the basis to building your applications and services. -
Docker registry commands $ docker pull $ docker search
$ docker login $ docker push # Retag your Image & Push $ docker tag first-image <username>/first-image $ docker login $ docker push <username>/first-image -
So What we learnt today? • Linux containers • Docker
- architecture. • docker images • docker containers • dockerfile • docker hub