L03-04 - Docker - Containers
L03-04 - Docker - Containers
www.cognixia.com
www.cognixia.com
Containers
www.cognixia.com
www.cognixia.com
Container Implementation
• Operating system level virtualization uses a
set of tools
o A virtualization subsystem Container 1 Container 2 Container 3
www.cognixia.com
www.cognixia.com
Operating System Level Virtualization
• Operating system level virtualization is where an operating
system kernel can support multiple isolated user space
instances Container Container Container Container
o Instances are called containers or jails Binaries/libs Binaries/libs Binaries/libs Binaries/libs
www.cognixia.com
www.cognixia.com
Docker
www.cognixia.com
www.cognixia.com
Docker for Linux
www.cognixia.com
www.cognixia.com
Docker Architecture
• Docker uses a client-server architecture
• Client
o Is the primary user interface which
communicates using a REST API
o Over HTTP
o Over local Unix socket
• Server
o Is the Docker daemon
o Responsible for building, running, and
distributing containers
• Registry
o Responsible for the storage, management, and
delivery of Docker Images
o Docker Hub
o Private
o Other vendors
www.cognixia.com
www.cognixia.com
Docker Images & Containers
• Docker images are read-only templates
o Foundation is a simplified version of the Linux operating
system
o Changes to foundation, such as application installations
added to the Image
o Images are the templates or build commands for Docker
• Docker containers are running environments
o Has OS, environment, program, network, etc.
o Runs (probably one) application
o All required software contained in image
o Can have boot-up configuration
o They can be run, started, stopped, and deleted
www.cognixia.com
www.cognixia.com
Docker Images
• The docker `run` command starts a container based on a named Docker Image
o Docker first looks for a local copy of the image
o If it does not exist it is pulled from a Docker Registry
o The default Registry is the Docker Hub Registry
o A new container is created using the file system from the image
o A read-write layer is added to the top of the file system
o A network interface is created and an IP address is assigned from a pool
o Standard input, output, and error streams are connected
o A specified application is executed
o Docker container appears as a child of the daemon process, ms are connected
www.cognixia.com
www.cognixia.com
Pulling & Running Containers
• A Docker Image must be located on the local computer
o It may have been created locally
o It may have been pulled from a Registry
o It may be missing
• The `pull` command insures that the specified image is on the local computer
o It will transfer all constituent layers of the image as separate transfers
• The `run` command creates and initiates a container based on the image
o The example runs the latest CentOS image
o It runs the command `command`
docker pull centos:latest
www.cognixia.com
www.cognixia.com
Running a Container Interactively
www.cognixia.com
www.cognixia.com
Listing Running Containers
docker ps
docker stats
www.cognixia.com
www.cognixia.com
Naming Containers
www.cognixia.com
www.cognixia.com
Attaching to Running Container
• Attaching to a container attaches to the contained process's STDIN, STDOUT, and
STDERR
o You can attach with either the container ID or its name
o Several command prompts can attach to the same container process
o All tty sessions see the same input and outputs
o The container ID is obtained using ps
On
• first
You terminal:
can detach from a container and leave it running using ^p ^q
docker run –it –-name centosC1 centos:latest /bin/bash
> date
On second terminal:
docker attach centosC1
> date
www.cognixia.com
www.cognixia.com
Stopping Running Containers
www.cognixia.com
www.cognixia.com
Pausing Containers
docker ps -a
docker start -ai centosC1 OR
Docker restart centosC1
www.cognixia.com
www.cognixia.com
Removing Containers
www.cognixia.com
www.cognixia.com
Daemon Containers
www.cognixia.com
www.cognixia.com
Docker Images
docker images
docker rmi centos-git
www.cognixia.com
www.cognixia.com
Adding Packages
www.cognixia.com
www.cognixia.com
Building Image Interactively
www.cognixia.com
www.cognixia.com
Automating Docker Image Build
www.cognixia.com
www.cognixia.com
Dockerfile
FROM centos:latest
MAINTAINER [email protected]
www.cognixia.com
www.cognixia.com
SHELL
www.cognixia.com
www.cognixia.com
COPY
www.cognixia.com
www.cognixia.com
ADD
• The Dockerfile ADD command copies files and remote file URLs into the
container
o The source files or directories must be in the build context or remote
URLs
o The source files can contain UNIX shell wildcards ? * []
o Destination directories must end in a / and will get created if they don't
exist
o Local source files in tar or compressed tar format get unpacked
www.cognixia.com
www.cognixia.com
RUN
www.cognixia.com
www.cognixia.com
ENV
ENV JAVA_HOME=/usr/java/latest
ENV PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/maven/bin
www.cognixia.com
www.cognixia.com
EXPOSE
Exposes the port at which the container application will be made available
EXPOSE 22
www.cognixia.com
www.cognixia.com
COMMAND
CMD /bin/bash
www.cognixia.com
www.cognixia.com
Building an Image
www.cognixia.com
www.cognixia.com
Exercise 2 : Automate Build of Docker Image
• Use document to build Jtrac image and run a container using it
www.cognixia.com
www.cognixia.com
Docker Registry
www.cognixia.com
www.cognixia.com
Docker Registry Service
www.cognixia.com
www.cognixia.com
Docker Hub Default Registry
www.cognixia.com
www.cognixia.com
Labels
• Labels are used to uniquely identify images
o Labels look like URIs
o Components separated by /
• Label components:
o Registry FQDN
o Namespace _ is for Docker Hub, r is for user
o User or organization name
o Repository name: tag
o A tag is either a version number or a descriptive label
https://hub.docker.com/r/databliss/netkernel-se/
www.cognixia.com
www.cognixia.com
Using Docker Hub Repository
www.cognixia.com
www.cognixia.com
Deleting Images
www.cognixia.com
www.cognixia.com
Private Registries
www.cognixia.com
www.cognixia.com
Creating Private Registry
www.cognixia.com
www.cognixia.com
Running Private Registry
www.cognixia.com
www.cognixia.com
Using Registry
www.cognixia.com
www.cognixia.com
Exercise 3 : Building Private Docker Registry
• Use document to build private docker registry and push images to it
www.cognixia.com
www.cognixia.com
Docker Networking
Default User-defined
www.cognixia.com
www.cognixia.com
Docker Container Networks - Default
$ docker network ls
www.cognixia.com
www.cognixia.com
Default – Bridge Network
www.cognixia.com
www.cognixia.com
Default – Bridge Network Hosts File
www.cognixia.com
www.cognixia.com
Default – Host Network
www.cognixia.com
www.cognixia.com
Default – None Network
www.cognixia.com
www.cognixia.com
Docker Container Network – User Defined Network
www.cognixia.com
www.cognixia.com
Creating User Defined Network
$ docker network ls
NETWORK ID NAME DRIVER SCOPE
9d6a9ab487ba bridge bridge local
c7956146a031 host host local
db58db4ec888 isolated_bridge bridge local
115642b21a91 none null local
www.cognixia.com
www.cognixia.com
Using networks
www.cognixia.com
www.cognixia.com
Exercise 4 : Docker networking
• Networking between container on docker network
www.cognixia.com
www.cognixia.com
Docker Compose
www.cognixia.com
www.cognixia.com
Compose
www.cognixia.com
www.cognixia.com
Compose
FROM centos:latest
RUN yum install -y openssh-server
RUN mkdir /var/run/sshd
RUN useradd -c "Student User" -m student
RUN echo "student:student" | chpasswd
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -q -N ""
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]
www.cognixia.com
www.cognixia.com
Run Compose
docker-compose up -d
docker-compose down
www.cognixia.com
www.cognixia.com
Exercise 5 : Docker Compose
• Use docker compose to run cotnainers
www.cognixia.com
www.cognixia.com
Run Command
www.cognixia.com www.cognixia.com