Вы находитесь на странице: 1из 51

Docker Lab Guide

DOCKER - LAB GUIDE

PREMKUMAR NATARAJAN

0
Docker Lab Guide

Table of Contents
1 Purpose ................................................................................................................................................. 4
2 Prerequisites ......................................................................................................................................... 4
2.1 Software Download ...................................................................................................................... 4
3 LAB-01: Install Docker in Ubuntu .......................................................................................................... 5
3.1 Time to Complete ......................................................................................................................... 5
3.2 What You Need ............................................................................................................................. 5
3.3 Docker Prerequisites for Ubuntu OS ............................................................................................. 5
3.4 Uninstall Docker Completely......................................................................................................... 5
3.5 Install Docker CE ........................................................................................................................... 6
3.5.1 Install using the repository ................................................................................................... 6
3.5.2 Install using the convenience script .................................................................................... 11
3.5.3 Install from a package ......................................................................................................... 11
4 LAB-02: Working Docker Images......................................................................................................... 13
4.1 Time to Complete ....................................................................................................................... 13
4.2 What You Need ........................................................................................................................... 13
4.3 Working with Docker Images ...................................................................................................... 13
5 LAB-03: Working with Docker Containers........................................................................................... 17
5.1 Time to Complete ....................................................................................................................... 17
5.2 What You Need ........................................................................................................................... 17
5.3 Working with Docker Container ................................................................................................. 17
6 LAB-04: Working with Dockerfile ........................................................................................................ 18
6.1 Time to Complete ....................................................................................................................... 18
6.2 What You Need ........................................................................................................................... 18
6.3 Working with Docker Container ................................................................................................. 18
6.4 Install, Run and Delete Applications inside Docker Containers .................................................. 20
7 LAB-05: Creating a Docker Image from an Existing Container ............................................................ 23
7.1 Time to Complete ....................................................................................................................... 23
7.2 What You Need ........................................................................................................................... 23
7.3 Create Image from Existing Container ........................................................................................ 23
7.4 Tag the updated Image and push it to Docker hub .................................................................... 25
8 LAB-06: Namespace ............................................................................................................................ 27

1
Docker Lab Guide

8.1 Time to Complete ....................................................................................................................... 27


8.2 What You Need ........................................................................................................................... 27
8.3 PID Namespace ........................................................................................................................... 27
9 LAB-07: Docker Composer .................................................................................................................. 28
9.1 Time to Complete ....................................................................................................................... 28
9.2 What You Need ........................................................................................................................... 28
9.3 Install Docker Compose .............................................................................................................. 28
9.4 Create YAML file for Docker compose ........................................................................................ 28
10 Lab-08: Packaging Git with a dockerfile .......................................................................................... 30
10.1 Time to Complete ....................................................................................................................... 30
10.2 What You Need ........................................................................................................................... 30
10.3 Clone the Git and run container from dockerfile........................................................................ 30
11 Lab-09: Storage in Docker ............................................................................................................... 32
11.1 Time to Complete ....................................................................................................................... 32
11.2 What You Need ........................................................................................................................... 32
11.3 Volume mounted in Container ................................................................................................... 32
11.4 Mount a Host Directory as a Volume .......................................................................................... 33
11.5 Volumes from another Container ............................................................................................... 33
11.6 Images and Containers on disk ................................................................................................... 35
12 Lab-10: Container Network ............................................................................................................. 37
12.1 Time to Complete ....................................................................................................................... 37
12.2 What You Need ........................................................................................................................... 37
12.3 Check the container network...................................................................................................... 37
12.4 Creating a new bridge network .................................................................................................. 38
12.5 Attaching a container to a networking ....................................................................................... 39
13 Lab-11: Setup Docker Private Registry ............................................................................................ 41
13.1 Time to Complete ....................................................................................................................... 41
13.2 What You Need ........................................................................................................................... 41
13.3 Setup Private Registry ................................................................................................................. 41
13.3.1 Generate TLS Certificates .................................................................................................... 41
13.3.2 Configuring the Docker Registry ......................................................................................... 42
13.3.3 Setup Authentication .......................................................................................................... 42

2
Docker Lab Guide

13.3.4 Configuring an Insecure Registry ........................................................................................ 43


13.3.5 Pull the image to store in your local registry ...................................................................... 43
13.3.6 Pushing Images to your Docker Registry............................................................................. 43
14 Lab-12: Docker Swarm Service........................................................................................................ 45
14.1 Time to Complete ....................................................................................................................... 45
14.2 What You Need ........................................................................................................................... 45
14.3 Prerequisites ............................................................................................................................... 45
14.4 Setup Docker Swarm Service in Manager Node ......................................................................... 45
14.5 Setup Docker Swarm Service in Worker Node............................................................................ 46
14.6 Deploying First Service to the Cluster ......................................................................................... 47
15 Troubleshooting .............................................................................................................................. 49
15.1 Docker Not able to pull images or run containers ...................................................................... 49
15.2 Install Java 8 using the Oracle JDK .............................................................................................. 49
15.2.1 Update Ubuntu ................................................................................................................... 49
15.2.2 Add the Java repository ...................................................................................................... 49
15.2.3 Install Java ........................................................................................................................... 49
15.2.4 Configure Java ..................................................................................................................... 49

3
Docker Lab Guide

1 Purpose
This document will lay out the details to setup the Docker in Ubuntu 64 bit OS environment and
practice the Lab exercises.

2 Prerequisites
Docker exercise are performed on Ubuntu OS

 To Install the Ubuntu on Oracle Virtual Box refer the guide :


OracleVMBox_Ubuntu_InstallationGuide.pdf

2.1 Software Download


S.No Software Download Location File Name Version Note

1 Oracle https://www.virtualbox.org/wiki VirtualBox-5.2.18- Windows 64 Oracle Virtual


Virtual Box /Downloads 124319-Win.exe Bit Box
2 Ubuntu https://www.ubuntu.com/down ubuntu-18.04- Ubuntu Ubuntu 18.04
18.04 LTS load/desktop desktop- 18.04 LTS LTS
amd64.iso

4
Docker Lab Guide

3 LAB-01: Install Docker in Ubuntu


This LAB 01 exercise shows you how to install Docker in Ubuntu OS.

3.1 Time to Complete


Approximately 0.30 Hr.

3.2 What You Need


 Oracle Virtual Box with Ubuntu VM

3.3 Docker Prerequisites for Ubuntu OS


To install Docker CE, you need the 64-bit version of one of these Ubuntu versions:
 Bionic 18.04 (LTS)
 Xenial 16.04 (LTS)
 Trusty 14.04 (LTS)

3.4 Uninstall Docker Completely


1. Log on to Ubuntu VM with the created user.
Ex: dockeruser
2. Open a terminal and Check docker been installed
dockeruser@dockeruser-VirtualBox:~$ dpkg -l | grep -i docker

3. Execute the below command to remove the docker from Ubunut.

dockeruser@dockeruser-VirtualBox:~$ sudo apt-get purge docker-ce


dockeruser@dockeruser-VirtualBox:~$ sudo apt-get purge docker-engine
dockeruser@dockeruser-VirtualBox:~$ sudo apt-get purge docker.io
dockeruser@dockeruser-VirtualBox:~$ sudo apt-get purge docker-ce-cli
dockeruser@dockeruser-VirtualBox:~$ sudo apt-get autoremove --purge docker-engine
# This deletes all images, containers, and volumes
dockeruser@dockeruser-VirtualBox:~$ sudo rm -rf /var/lib/docker
dockeruser@dockeruser-VirtualBox:~$ sudo groupdel docker

5
Docker Lab Guide

If the docker been installed, it will be removed and check with the below command.
dockeruser@dockeruser-VirtualBox:~$ docker -v
bash: /usr/bin/docker: No such file or directory

3.5 Install Docker CE


You can install Docker CE in different ways, depending on your needs:

3.5.1 Install using the repository


Most users set up Docker’s repositories and install from them, for ease of installation and upgrade
tasks. This is the recommended approach.

3.5.1.1 Setup the Docker Repository


Before you install Docker CE for the first time on a new host machine, you need to set up the Docker
repository. Afterward, you can install and update Docker from the repository.
1. Open a terminal and execute the below command.
2. Update the apt package index:
dockeruser@dockeruser-VirtualBox:~$ sudo apt-get update

dockeruser@dockeruser-VirtualBox:~$ sudo apt install curl

3. Install packages to allow apt to use a repository over HTTPS:


dockeruser@dockeruser-VirtualBox:~$ sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
software-properties-common

6
Docker Lab Guide

4. Add Docker’s official GPG key:


dockeruser@dockeruser-VirtualBox:~$ curl -fsSL
https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

5. Verify that you now have the key with the fingerprint 9DC8 5822 9FC7 DD38 854A E2D8 8D81
803C 0EBF CD88, by searching for the last 8 characters of the fingerprint.
dockeruser@dockeruser-VirtualBox:~$ sudo apt-key fingerprint 0EBFCD88

6. Use the following command to set up the stable repository.

7
Docker Lab Guide

You always need the stable repository, even if you want to install builds from the edge or test
repositories as well. To add the edge or test repository, add the word edge or test (or both) after
the word stable in the commands below.

dockeruser@dockeruser-VirtualBox:~ $ sudo add-apt-repository \


"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"

3.5.1.2 Install Docker CE


1. Update the apt package index.
dockeruser@dockeruser-VirtualBox:~ $ sudo apt-get update

2. Install the latest version of Docker CE:


dockeruser@dockeruser-VirtualBox:~ $ sudo apt-get install docker-ce

8
Docker Lab Guide

The Docker daemon starts automatically.

Note: Below steps are to install the specific version of Docker. Not needed for this lab, it’s only
for add-on details.
To install a specific version of Docker CE, list the available versions in the repo, then select and
install:
a. List the versions available in your repo:
$ apt-cache madison docker-ce

docker-ce | 5:18.09.0~3-0~ubuntu-bionic | https://download.docker.com/linux/ubuntu


bionic/stable amd64 Packages

9
Docker Lab Guide

b. Install a specific version by its fully qualified package name, which is package name
(docker-ce) “=” version string (2nd column), for example, docker-ce=5:18.09.0~3-
0~ubuntu-bionic.
$ sudo apt-get install docker-ce=<VERSION>

3. Verify that Docker CE is installed correctly by running the hello-world image.


dockeruser@dockeruser-VirtualBox:~$ sudo docker run hello-world
This command downloads a test image and runs it in a container. When the container runs, it
prints an informational message and exits.

Docker CE is installed and running. The docker group is created but no users are added to it. You
need to use sudo to run Docker commands.

4. If you would like to use Docker as a non-root user, you should now consider adding your user to
the “docker” group with command below:
dockeruser@dockeruser-VirtualBox:~$ sudo usermod -aG docker dockeruser

5. Exit from the terminal and reboot the VM.


6. Open a new terminal. Check the dockeruser id and it will added under the docker group.
dockeruser@dockeruser-VirtualBox:~$ id

10
Docker Lab Guide

3.5.2 Install using the convenience script


In testing and development environments, some users choose to use automated convenience
scripts to install Docker.

Docker provides convenience scripts at get.docker.com and test.docker.com for installing edge and
testing versions of Docker CE into development environments quickly and non-interactively.
Using these scripts is not recommended for production environments.
Script Location: https://get.docker.com/

1. Uninstall the Docker CE as per the section: 3.4


2. Open a terminal and execute the below command.
3. Run the scripts as below:
dockeruser@dockeruser-VirtualBox:~$ curl -fsSL https://get.docker.com -o get-docker.sh
dockeruser@dockeruser-VirtualBox:~$ sudo sh get-docker.sh
dockeruser@dockeruser-VirtualBox:~$ sudo usermod -aG docker dockeruser

Warning: Potential Risk with this installation mode are:


o Script uses root privilege. So understanding on script work is more important.
o Scripts do not allow you to customize any installation parameters
o The scripts install all dependencies and recommendations of the package manager without
asking for confirmation
o The script does not provide options to specify which version of Docker to install, and
installs the latest version that is released in the “edge” channel.
o Do not use the convenience script if Docker has already been installed on the host machine
using another mechanism

3.5.3 Install from a package


Download the DEB package and install it manually and manage upgrades completely manually.
This is useful in situations such as installing Docker on air-gapped systems with no access to the
internet.

1. Uninstall the Docker CE as per the section: 3.4


2. Download the below docker .DEB file for the Ubuntu from the URL
https://download.docker.com/linux/ubuntu/dists/bionic/pool/stable/amd64/
 docker-ce-cli_18.09.0_3-0_ubuntu-bionic_amd64.deb
 containerd.io_1.2.0_rc.2-1_amd64.deb
 docker-ce_18.09.0_3-0_ubuntu-bionic_amd64.deb
Save all deb files in the user home directory.
3. Change the permission for all the file.
dockeruser@dockeruser-VirtualBox:~$ chmod –R 775 /home/dockeruser/*.deb

4. Install Docker CE, changing the path below to the path where you downloaded the Docker
package.

dockeruser@dockeruser-VirtualBox:~$ sudo dpkg -i /home/dockeruser/docker-ce-


cli_18.09.0_3-0_ubuntu-bionic_amd64.deb

11
Docker Lab Guide

dockeruser@dockeruser-VirtualBox:~$ sudo dpkg -i


/home/dockeruser/containerd.io_1.2.0_rc.2-1_amd64.deb

dockeruser@dockeruser-VirtualBox:~$ sudo dpkg -i /home/dockeruser/docker-ce_18.09.0_3-


0_ubuntu-bionic_amd64.deb

The Docker daemon starts automatically.

5. Verify that Docker CE is installed correctly by running the hello-world image.

dockeruser@dockeruser-VirtualBox:~$ sudo docker run hello-world

docker info

<<<END OF LAB >>>

12
Docker Lab Guide

4 LAB-02: Working Docker Images


This LAB 02 exercise shows how to work with Docker Images.
o Search the images hello-world and centos from the Docker hub
o Pull the searched images from the hub.
o List all the Images from the Docker host
o Get the detailed information about the Docker Images.
o Get the History of the Docker Images.
o Save or Backup a docker Images
o Remove or delete docker Images

4.1 Time to Complete


Approximately 1.00 Hr.

4.2 What You Need


 Lab01 to be completed successfully.

4.3 Working with Docker Images


1. Log on to Ubuntu VM with the created user. Ex: dockeruser
2. Open a terminal and search for the hello-world and centos images.
dockeruser@dockeruser-VirtualBox:~$ docker search hello-world
dockeruser@dockeruser-VirtualBox:~$ docker search centos

13
Docker Lab Guide

3. Pull the hello-world and centos images from the docker hub.

dockeruser@dockeruser-VirtualBox:~$ docker pull hello-world


dockeruser@dockeruser-VirtualBox:~$ docker pull centos

4. List all the images from the docker host.

dockeruser@dockeruser-VirtualBox:~$ docker images

5. Get detailed information of the images from the docker host.


Docker inspect with Image Id
dockeruser@dockeruser-VirtualBox:~$ docker inspect 1e1148e4cc2c

14
Docker Lab Guide

6. Get History of the docker images. Docker history with Image Id


dockeruser@dockeruser-VirtualBox:~$ docker history 1e1148e4cc2c

15
Docker Lab Guide

7. Save or backup the docker Image using the Image Id.


dockeruser@dockeruser-VirtualBox:~$ docker save 4ab4c602aa5e > backup_hello-world.tar

8. Restore the backed up docker Image.


dockeruser@dockeruser-VirtualBox:~$ docker load -i backup_hello-world.tar

9. Remove or Delete the hello-world docker Image.


4ab4c602aa5e – Image Id of hello-world Image.

dockeruser@dockeruser-VirtualBox:~$ docker rmi 4ab4c602aa5e

To remove multiple images, command is

dockeruser@dockeruser-VirtualBox:~$ docker rmi <imageId1> <imageId2>


To remove all images, command is

dockeruser@dockeruser-VirtualBox:~$ docker rmi $(docker images -a -q)


<<<END OF LAB >>>

16
Docker Lab Guide

5 LAB-03: Working with Docker Containers


This LAB 02 exercise shows how to work with Docker Images.
o Run the container centos
o List all containers and find the top process in the container
o Stop the container
o Remove the container

5.1 Time to Complete


Approximately 1.00 Hr.

5.2 What You Need


 Lab01 to be completed successfully.

5.3 Working with Docker Container


1. Log on to Ubuntu VM with the created user. Ex: dockeruser
2. Open a terminal and run the centos container. (Don’t Close this terminal)
dockeruser@dockeruser-VirtualBox:~$ docker run -it centos /bin/bash
[root@4f9d3370b247 /]#

3. Open a new terminal and list the centos container.

dockeruser@dockeruser-VirtualBox:~$ docker ps

4. Stop the centos container.


dockeruser@dockeruser-VirtualBox:~$ docker stop 4f9d3370b247

5. Remove the centos container.


dockeruser@dockeruser-VirtualBox:~$ docker rm 4f9d3370b247

Note the terminal with the bash shell opened for centos will be exit automatically.

<<<END OF LAB >>>

17
Docker Lab Guide

6 LAB-04: Working with Dockerfile


This LAB 04 exercise shows how to use dockerfile to create a Docker Images.
o Create a dockerfile
o Define the instructions to customize the existing docker image
o Build a docker image from the dockerfile
o Run and test the customized docker image the container centos
o List all containers and find the top process in the container
o Stop the container
o Remove the container

6.1 Time to Complete


Approximately 1.00 Hr.

6.2 What You Need


 Lab01 to be completed successfully.

6.3 Working with Docker Container


1. Log on to Ubuntu VM with the created user. Ex: dockeruser
2. Open a terminal and create a docker file in the current directory
dockeruser@dockeruser-VirtualBox:~$ pwd
/home/dockeruser
dockeruser@dockeruser-VirtualBox:~$ touch dockerfile
dockeruser@dockeruser-VirtualBox:~$

3. Open the dockerfile using vi editor and specify the instructions to customize the existing image.
dockeruser@dockeruser-VirtualBox:~$ vi dockerfile

FROM centos

MAINTAINER premkumar natarajan

RUN yum update && yum -y install httpd

RUN echo "Welcome to Oracle - Docker Training Session Home Page created using
dockerfile">/var/www/html/index.html

EXPOSE 80

CMD apachectl -D FOREGROUND

4. Build the docker file using the below command.


dockeruser@dockeruser-VirtualBox:~$ docker build .

18
Docker Lab Guide

5. Check the customized image is created using the below command.


dockeruser@dockeruser-VirtualBox:~$ docker images

6. Tag the newly created image now using the docker tag command.
dockeruser@dockeruser-VirtualBox:~$ docker tag 8a53becffff6 trainingsession/httpd_server

7. Run and test the customized docker image by launching docker container
dockeruser@dockeruser-VirtualBox:~$ docker run -d -it --name webserver1 -p 172.17.0.1:80:80
trainingsession/httpd_server

Where:
run - used to run the container
-d - demonize (continuously)
-i - interactive session
-t - to allocate a pseudo terminal for container
--name - to specify unique container name
-p - specify the ip address and listening ports with port mapping

8. Use docker ps command to see the status of the launched docker container
dockeruser@dockeruser-VirtualBox:~$ docker ps

19
Docker Lab Guide

9. Try accessing the webserver content created using dockerfile.


http://172.17.0.1:80

6.4 Install, Run and Delete Applications inside Docker Containers


This lab show to run a Simple .jar Application in a Docker Container
1. Open a terminal and create a docker file in the in the directory
dockeruser@dockeruser-VirtualBox:~$ pwd
/home/dockeruser
dockeruser@dockeruser-VirtualBox:~$ mkdir JavaApplication
dockeruser@dockeruser-VirtualBox:~$ cd JavaApplication
dockeruser@dockeruser-VirtualBox:~/JavaApplication$ pwd
/home/dockeruser/JavaApplication
dockeruser@dockeruser-VirtualBox:~/JavaApplication$

2. Create a basic.java file, HelloWorld.java, and add these lines into it


dockeruser@dockeruser-VirtualBox:~/JavaApplication$ vi HelloWorld.java

public class HelloWorld {


public static void main(String[] args){
System.out.println("Welcome to Oracle – Docker Training Session :)
");
}
}

3. Save the file and compile using javac. If the java not installed on the host OS, install using the
command as below.
dockeruser@dockeruser-VirtualBox:~/JavaApplication$ javac HelloWorld.java

Command 'javac' not found, but can be installed with:

sudo apt install default-jdk


sudo apt install openjdk-11-jdk-headless
sudo apt install ecj
sudo apt install openjdk-8-jdk-headless
sudo apt install openjdk-9-jdk-headless

If the JAVA been not installed follow the section 15.2 to install the JDK in Ubuntu.

20
Docker Lab Guide

After installing JAVA, compile the java code. HelloWorld.class file will be generated after
compiling which later we will build in .jar.

4. Create a simple manifest.txt to make it packed right.


dockeruser@dockeruser-VirtualBox:~/JavaApplication$ vi manifest.txt

Manifest-Version: 1.0
Created-By: Premkumar Natarajan
Main-Class: HelloWorld

5. Create the jar using the following:


dockeruser@dockeruser-VirtualBox:~/JavaApplication$ jar cfm HelloWorld.jar manifest.txt
HelloWorld.class

6. Check the jar file created and run the jar to check the result as below.
dockeruser@dockeruser-VirtualBox:~/JavaApplication$ ls
HelloWorld.class HelloWorld.jar HelloWorld.java manifest.txt
dockeruser@dockeruser-VirtualBox:~/JavaApplication$ java -jar HelloWorld.jar
Welcome to Oracle – Docker Training Session :)
dockeruser@dockeruser-VirtualBox:~/JavaApplication$

7. Open the dockerfile using vi editor and specify the instructions to customize the existing image.
dockeruser@dockeruser-VirtualBox:~/JavaApplication$ vi Dockerfile

FROM java:8
WORKDIR /
ADD HelloWorld.jar HelloWorld.jar
EXPOSE 8080
CMD java -jar HelloWorld.jar

8. Build the docker file using the below command.


dockeruser@dockeruser-VirtualBox:~/JavaApplication$ docker build -t helloworld .

21
Docker Lab Guide

9. Check the customized image is created using the below command.


dockeruser@dockeruser-VirtualBox:~/JavaApplication$ docker images

10. Run and test the customized docker image by launching docker container
dockeruser@dockeruser-VirtualBox:~$ docker run helloworld

<<<END OF LAB >>>

22
Docker Lab Guide

7 LAB-05: Creating a Docker Image from an Existing Container


This LAB exercise explains to create an image from the existing container.

If you modify the contents of a container, you can use the docker commit command to save the current
state of the container as an image.

The following example demonstrates how to modify an container based on the centos image so that it
can run an Apache HTTP server. After stopping the container, the image mymod/httpd:v1 is created
from it.

7.1 Time to Complete


Approximately 0.30 Hr.

7.2 What You Need


 Lab01 to be completed successfully.

7.3 Create Image from Existing Container

To create an Apache server image from a centos container:

1. Run the bash shell inside a container named guest


dockeruser@dockeruser-VirtualBox:~$ docker run –it --name silly_kirch centos /bin/bash
[root@26aa122c1720 /]#
2. Install the httpd package:
[root@26aa122c1720 /]# yum update
[root@26aa122c1720 /]# yum install httpd

3. Create the web content to be displayed under the /var/www/html directory hierarchy on the
silly_kirch and create index.html inside the folder.
[root@26aa122c1720 /]# mkdir -p /var/www/html

[root@26aa122c1720 /]# cd /var/www/html/

[root@26aa122c1720 html]# vi index.html

index.html
<!DOCTYPE html>
<html lang="en">
<head>

<!-- Basic Page Needs -->


<meta charset="utf-8">
<title>Docker :)</title>
<meta name="description" content="">
<meta name="author" content="">

<!-- Mobile Specific Metas-->

23
Docker Lab Guide

<meta name="viewport" content="width=device-width, initial-scale=1">


<!-- FONT -->
<link href="//fonts.googleapis.com/css?family=Raleway:400,300,600" rel="stylesheet" type="text/css">
<!-- CSS -->
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/skeleton.css">
<!-- Favicon -->
<link rel="icon" type="image/png" href="images/favicon.png">
</head>
<body>

<!-- Primary Page Layout -->


<div class="container">
<div class="row">
<div class="one-half column" style="margin-top: 25%">
<h4>Hello Docker!</h4>
<p>This is being served from a <strong>docker</strong> container running Http. Sample Lab to show
how to create new image from existing container</p>
</div>
</div>
</div>
<!-- End Document-->
</body>
</html>

4. Open a new terminal and exit the silly_kirch by using the docker stop command on the host:
dockeruser@dockeruser-VirtualBox:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES
26aa122c1720 centos "/bin/bash" 35 minutes ago Up 35 minutes
silly_kirch

dockeruser@dockeruser-VirtualBox:~$ docker stop silly_kirch


silly_kirch

dockeruser@dockeruser-VirtualBox:~$

5. Create the image mymod/httpd with the tag v1 using the ID of the container that you stopped:
dockeruser@dockeruser-VirtualBox:~$ docker commit -m "ol6 + httpd" -a "Premkumar N" \
> `docker ps -l -q` mymod/httpd:v1
sha256:f8ab9a164a09cef9c787fe946720f0eee9ee329d6d065256c31fc178b9662a64
dockeruser@dockeruser-VirtualBox:~$

Use the -m and -a options to document the image and its author. The command returns the full
version of the new image's ID.

If you use the docker images command, the new image now appears in the list:

dockeruser@dockeruser-VirtualBox:~$ docker images


REPOSITORY TAG IMAGE ID CREATED SIZE

24
Docker Lab Guide

mymod/httpd v1 f8ab9a164a09 56 seconds ago 358MB


traininghttpserver1 latest 6e3f7cd85ef5 3 hours ago 305MB
centos latest 1e1148e4cc2c 11 days ago 202MB
hello-world latest 4ab4c602aa5e 3 months ago 1.84kB
dockeruser@dockeruser-VirtualBox:~$

6. Remove the container named guest.


dockeruser@dockeruser-VirtualBox:~$ docker rm silly_kirch
silly_kirch
dockeruser@dockeruser-VirtualBox:~$

7. You can now use the new image to create a container that works as a web server, for example:
dockeruser@dockeruser-VirtualBox:~$ docker run -d --name newguest -p 8080:80
mymod/httpd:v1 /usr/sbin/httpd -D FOREGROUND
8c1bc0dcb080bacf3089d770643f1e644a3800420f1307c5045d99bc7b1c3617
dockeruser@dockeruser-VirtualBox:~$

dockeruser@dockeruser-VirtualBox:~$ docker ps
ONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES
b76dc9a28eb5 mymod/httpd:v1 "/usr/sbin/httpd -D …" 17 seconds ago Up 15
seconds 0.0.0.0:8080->80/tcp newguest
dockeruser@dockeruser-VirtualBox:~$

The default IP address value of 0.0.0.0 means that the port mapping applies to all network
interfaces on the host.

Open browser with the address : <IPAddres>:8080

7.4 Tag the updated Image and push it to Docker hub


1. Login to the docker hub using below command.
dockeruser@dockeruser-VirtualBox:~$ docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker
ID, head over to https://hub.docker.com to create one.
Username: premkumarmlp
Password:
WARNING! Your password will be stored unencrypted in /home/dockeruser/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

2. Check the image ID using docker images.


REPOSITORY TAG IMAGE ID CREATED SIZE
mymod/httpd v1 f8ab9a164a09 56 seconds ago 358MB
3. Tag your image

25
Docker Lab Guide

dockeruser@dockeruser-VirtualBox:~$ docker tag f8ab9a164a09


premkumarmlp/dockertraining:modhttpdserver

4. Push your image to the repository you created


dockeruser@dockeruser-VirtualBox:~$ docker push premkumarmlp/dockertraining
The push refers to repository [docker.io/premkumarmlp/dockertraining]
3aaba5dfa3b6: Pushed
071d8bd76517: Mounted from library/centos
modhttpdserver: digest:
sha256:73ffc230cf7d9f5b7effa778217d4c64c9871e174ae3f9da9eb33a5c82c1459c size: 741

dockeruser@dockeruser-VirtualBox:~$

5. To open the existing container, use the below command


dockeruser@dockeruser-VirtualBox:~$ docker exec -it b76dc9a28eb5 bash
[root@26aa122c1720 /] # touch testfile
[root@26aa122c1720 /] # exit
dockeruser@dockeruser-VirtualBox

<<<END OF LAB >>>

26
Docker Lab Guide

8 LAB-06: Namespace
This LAB exercise explains how the isolation occurs in Docker using namespace.
o Create 2 containers based on the BusyBox image. BusyBox is software that provides several
stripped-down UNIX tools in a single executable file. dockerfile
o Get the process id for this process in each container and check with the process id in the host.

8.1 Time to Complete


Approximately 0.30 Hr.

8.2 What You Need


 Lab01 to be completed successfully.

8.3 PID Namespace


6. Log on to Ubuntu VM with the created user. Ex: dockeruser
7. Open a terminal and create 2 different container using BusyBox as below.
dockeruser@dockeruser-VirtualBox:~$ docker run -d --name idlecontainer1 busybox sleep
9999999999
dockeruser@dockeruser-VirtualBox:~$ docker run -d --name idlecontainer2 busybox sleep
8888888888
8. Check the container using command docker ps -a

9. Get the process id of sleep process in each container


docker exec – its subcommand in docker to run another command along with the container
command.
dockeruser@dockeruser-VirtualBox:~$ docker exec idlecontainer1 ps –ef

dockeruser@dockeruser-VirtualBox:~$ docker exec idlecontainer2 ps –ef

Observe the PID for each sleep process is separate in each container.
10. Get the process id of process in host machine using the command ps -ef

<<<END OF LAB >>>

27
Docker Lab Guide

9 LAB-07: Docker Composer


This LAB exercise explains how to:
o Install Docker Compose
o Create YAML file to integrate multiple containers
o Run the yaml file using Docker compose and check the application

9.1 Time to Complete


Approximately 0.30 Hr.

9.2 What You Need


 Lab01 to be completed successfully.

9.3 Install Docker Compose


1. Install Docker Compose using the below command.
curl -L https://github.com/docker/compose/releases/download/1.23.2/docker-compose-`uname
-s`-`uname -m` > /usr/local/bin/docker-compose

dockeruser@dockeruser-VirtualBox:~$ sudo su
[sudo] password for dockeruser:
root@dockeruser-VirtualBox:/home/dockeruser# curl -L
https://github.com/docker/compose/releases/download/1.23.2/docker-compose-`uname -s`-
`uname -m` > /usr/local/bin/docker-compose
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 617 0 617 0 0 327 0 --:--:-- 0:00:01 --:--:-- 326

9.4 Create YAML file for Docker compose


1. Open a terminal and Create a new project folder
dockeruser@dockeruser-VirtualBox:~$ mkdir dockercompose
dockeruser@dockeruser-VirtualBox:~$ cd dockercompose
dockeruser@dockeruser-VirtualBox:~/dockercompose$

2. Create Docker compose file in the project folder : docker-compose.yml with the preferred
editor having the following contents

version: '2'
services:
web:
image: wordpress
links:
- mysql
environment:
- WORDPRESS_DB_PASSWORD=Pa$$w0rd
ports:
- "127.0.0.3:8080:80"
mysql:

28
Docker Lab Guide

image: mysql:5.7
environment:
- MYSQL_ROOT_PASSWORD=Pa$$w0rd
- MYSQL_DATABASE=wordpress

3. Get the linked containers up.

dockeruser@dockeruser-VirtualBox:~/dockercompose$ docker-compose up

4. Open the browser and Visit the IP address http://127.0.0.3:8080 in order to see the setup page
of the newly created linked WordPress container.

<<<END OF LAB >>>

29
Docker Lab Guide

10 Lab-08: Packaging Git with a dockerfile


This LAB exercise explains how to:
o Use to Git with docker
o To push the dockerfile and application files in git
This lab show to create a dockerfile to install nginx web server and a static web page in it. And this code
will be pushed to Git
From Git, image been pulled to run the container.

10.1 Time to Complete


Approximately 0.30 Hr.

10.2 What You Need


 Lab01 to be completed successfully.
 Create Git account in the https://github.com/ and create a repository <username>/static_site

10.3 Clone the Git and run container from dockerfile


1. Open a terminal and Create a new project folder
dockeruser@dockeruser-VirtualBox:~$ mkdir StaticWebApplication
dockeruser@dockeruser-VirtualBox:~$ cd StaticWebApplication
dockeruser@dockeruser-VirtualBox:~/StaticWebApplication $

2. Clone the content of git to the host


git clone https://github.com/premkumarmlp/static_site

dockeruser@dockeruser-VirtualBox:~/StaticWebApplication $
git clone https://github.com/premkumarmlp/static_site

30
Docker Lab Guide

3. Check the Dockerfile in the project folder :


/home/dockeruser/StaticWebApplication/static_site with the preferred editor having the
following contents
FROM nginx
COPY wrapper.sh /
COPY html /usr/share/nginx/html
CMD ["./wrapper.sh"]

4. Build the docker file using the below command.


dockeruser@dockeruser-VirtualBox:~/StaticWebApplication/static_site$ docker build
https://github.com/premkumarmlp/static_site.git

5. Check the images and tag a name using the below command.
dockeruser@dockeruser-VirtualBox:~/StaticWebApplication/static_site$ docker images

dockeruser@dockeruser-VirtualBox:~/StaticWebApplication/static_site$ docker tag


0b4ac2ad42df sataticapplication/httpd_server

6. Run the container using the image built from Git using below command.
dockeruser@dockeruser-VirtualBox:~/StaticWebApplication/static_site$ docker run -d -it --
name webserver1 -p 172.17.0.1:80:80 ataticapplication/httpd_server

7. Check the nginx server from the browser


http:// 172.17.0.1:80

<<<END OF LAB >>>

31
Docker Lab Guide

11 Lab-09: Storage in Docker


This LAB exercise explains how to:
o Use to storage in docker using volume, mount, tmpfs
o How to use a shared file system across containers

11.1 Time to Complete


Approximately 0.30 Hr.

11.2 What You Need


 Lab01 to be completed successfully.

11.3 Volume mounted in Container


1. Execute the following command.
dockeruser@dockeruser-VirtualBox:~$ docker run -v /volume1 -i -t centos /bin/bash
This will mount /volume1 inside the container created from centos image and launch and bash
shell.

2. The command above will get us into the terminal tty of the container. Run ls command and you
will notice volume 1 there
[root@5160c67c15f8 /]# ls
anaconda-post.log bin dev etc home lib lib64 media mnt opt proc root run sbin srv sys
tmp usr var volume1
[root@5160c67c15f8 /]# ls

3. Open a new terminal and inspect the docker container.


dockeruser@dockeruser-VirtualBox:~$ docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS


NAMES
857ec6e385ba centos "/bin/bash" 13 seconds ago Up 12 seconds
quirky_stonebraker

dockeruser@dockeruser-VirtualBox:~$ docker inspect 857ec6e385ba


On inspecting the docker container you will notice that /volume1 is mounted under / and is a
Read Write Volume

32
Docker Lab Guide

11.4 Mount a Host Directory as a Volume


1. Create a local directory /home/dockeruser/logs and execute below command
dockeruser@dockeruser-VirtualBox:~$ cd
dockeruser@dockeruser-VirtualBox:~$ mkdir -p logs
dockeruser@dockeruser-VirtualBox:~$ pwd
/home/dockeruser
dockeruser@dockeruser-VirtualBox:~$ touch /home/dockeruser/logs/log1.txt
dockeruser@dockeruser-VirtualBox:~$ mkdir -p logs
dockeruser@dockeruser-VirtualBox:~$touch /host/logs/log1.txt

2. Execute the following command to bind /home/dockeruser/logs host directory


to /container/logs volume
dockeruser@dockeruser-VirtualBox:~$ docker run -v /home/dockeruser/logs:/container/logs -i -
t centos /bin/bash

3. Execute ls to see the contents of mounted volume. Notice that contents are the same as created
in host directory.
[root@8a44c76b4154 /]# ls
anaconda-post.log bin container dev etc home lib lib64 media mnt opt proc root run
sbin srv sys tmp usr var
[root@8a44c76b4154 /]# cd container/
[root@8a44c76b4154 container]# ls
logs
[root@8a44c76b4154 container]# cd logs/
[root@8a44c76b4154 logs]# ls
log1.txt
[root@8a44c76b4154 logs]#

11.5 Volumes from another Container

Volumes from a container can be bound to another container using --volumes-from <container-
name flag.

1. Create the host directory /centos1 in the host


dockeruser@dockeruser-VirtualBox:~$ sudo mkdir /centos1
dockeruser@dockeruser-VirtualBox:~$ cd /centos1/
dockeruser@dockeruser-VirtualBox:~$ sudo touch fileincentos1

2. Create a container centos1 from the centos image.

33
Docker Lab Guide

dockeruser@dockeruser-VirtualBox:~$ docker run -it --name centos1 -v /centos1:/centos1


centos

[root@4e466216442a /]# ls
anaconda-post.log bin centos1 dev etc home lib lib64 media mnt opt proc root run sbin
srv sys tmp usr var
[root@4e466216442a /]#

3. Create Second Container with shared volumes


 Create a second container centos2 with volumes from centos1

dockeruser@dockeruser-VirtualBox:~$ docker run -it --name centos2 --volumes-from centos1


centos

4. Check that the ubuntu1 volume is bound as expected and exit from the centos container.

[root@330c13967324 /]# ls
anaconda-post.log bin centos1 dev etc home lib lib64 media mnt opt proc root run sbin
srv sys tmp usr var
[root@330c13967324 /]# cd centos1/
[root@330c13967324 centos1]# ls
fileincentos1
[root@330c13967324 centos1]# exit

5. List all the volumes.

dockeruser@dockeruser-VirtualBox:~$ docker volume ls

6. Remove centos1-vol volumes.

dockeruser@dockeruser-VirtualBox:~$ docker volume rm centos1-vol

7. Remove all unused volumes.

dockeruser@dockeruser-VirtualBox:~$ docker volume prune

34
Docker Lab Guide

11.6 Images and Containers on disk


1. Stop all the container and remove it from docker.
2. Remove all images from the docker.
3. Pull the Ubuntu image from the docker hub.
dockeruser@dockeruser-VirtualBox:~$ docker run ubuntu

4. Each image layer has its own directory within /var/lib/docker/overlay2/, which contains its
contents, as shown below.

dockeruser@dockeruser-VirtualBox:~$ sudo su

[sudo] password for dockeruser:

root@dockeruser-VirtualBox:/home/dockeruser# cd /var/lib/docker/overlay2

root@dockeruser-VirtualBox:/var/lib/docker/overlay2# ls

5. Check the Image content.


root@dockeruser-VirtualBox:/var/lib/docker/image/overlay2/imagedb/content/sha256# pwd
/var/lib/docker/image/overlay2/imagedb/content/sha256
root@dockeruser-VirtualBox:/var/lib/docker/image/overlay2/imagedb/content/sha256# ls
93fd78260bd1495afb484371928661f63e64be306b7ac48e2d13ce9422dfee26
root@dockeruser-VirtualBox:/var/lib/docker/image/overlay2/imagedb/content/sha256#

Copy the content and check the JSON data of the image.

6. Check the type of storage driver and root directory of docker used in you lab environment.
dockeruser@dockeruser-VirtualBox:~$ docker info

35
Docker Lab Guide

7. Check the below details from information of docker.


 Total number of images
 No of active containers
 No of stopped containers
 Registry used and account details

<<<END OF LAB >>>

36
Docker Lab Guide

12 Lab-10: Container Network


This LAB exercise explains how to:
o To create a bridge network
o To deploy a container on that network

12.1 Time to Complete


Approximately 0.30 Hr.

12.2 What You Need


 Lab01 to be completed successfully.

12.3 Check the container network


1. Open a terminal and list the networks in the container using below command.
dockeruser@dockeruser-VirtualBox:~$ docker network ls

2. Get more information on a particular network, by issuing the command docker network inspect
NAME (Where NAME is the name of the network you want to view). .
dockeruser@dockeruser-VirtualBox:~$ docker network inspect bridge

37
Docker Lab Guide

12.4 Creating a new bridge network


1. Create a bridge network called “isolated” using below command.
dockeruser@dockeruser-VirtualBox:~$ docker network create --driver bridge isolated

38
Docker Lab Guide

2. Create a bridge network called “isolated” using below command.


3. Inspect the created network using the below command.
dockeruser@dockeruser-VirtualBox:~$ docker network inspect isolated

12.5 Attaching a container to a networking


1. Attach a container to the newly created network.
dockeruser@dockeruser-VirtualBox:~$ docker run --network=isolated -itd --name=docker-nginx
nginx

2. Inspect the created network using the below command.


dockeruser@dockeruser-VirtualBox:~$ docker network inspect isolated

39
Docker Lab Guide

<<<END OF LAB >>>

40
Docker Lab Guide

13 Lab-11: Setup Docker Private Registry


This LAB exercise explains how to setup private registry for docker

13.1 Time to Complete


Approximately 0.30 Hr.

13.2 What You Need


 Lab01 to be completed successfully.

13.3 Setup Private Registry

13.3.1 Generate TLS Certificates


1. If you have actual CA certs then you should use those. If you don’t then run these commands to
generate some self-signed certificates.
dockeruser@dockeruser-VirtualBox:~$ pwd
/home/dockeruser
dockeruser@dockeruser-VirtualBox:~$ mkdir -p certs
dockeruser@dockeruser-VirtualBox:~$ cd certs/
dockeruser@dockeruser-VirtualBox:~/certs$ openssl req -x509 -nodes -days 365 -newkey
rsa:2048 -keyout registry-selfsigned.key -out registry-selfsigned.crt

Generating a 2048 bit RSA private key


................................................+++
................................................................................................................+++
writing new private key to 'registry-selfsigned.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:IN
State or Province Name (full name) [Some-State]: KARNATAKA
Locality Name (eg, city) []: BLR
Organization Name (eg, company) [Internet Widgits Pty Ltd]: AMSTAR
Organizational Unit Name (eg, section) []: TRAINING
Common Name (e.g. server FQDN or YOUR name) []: PREMKUMAR
Email Address []:premkumar@dockersession.com

dockeruser@dockeruser-VirtualBox:~/certs$ ls
registry-selfsigned.crt registry-selfsigned.key
dockeruser@dockeruser-VirtualBox:~/certs$

41
Docker Lab Guide

13.3.2 Configuring the Docker Registry


1. Now that we have our certs generated we can start our on-premises docker registry.
dockeruser@dockeruser-VirtualBox:~/certs$ cd ..
dockeruser@dockeruser-VirtualBox:~$ docker run -d --restart=always \
> --name registry -v `pwd`/certs:/certs \
> -e REGISTRY_HTTP_ADDR=0.0.0.0:443 \
> -e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/registry-selfsigned.crt \
> -e REGISTRY_HTTP_TLS_KEY=/certs/registry-selfsigned.key \
> -p 443:443 \
> registry:2

Unable to find image 'registry:2' locally


2: Pulling from library/registry
d6a5679aa3cf: Pull complete
ad0eac849f8f: Pull complete
2261ba058a15: Pull complete
f296fda86f10: Pull complete
bcd4a541795b: Pull complete
Digest: sha256:5a156ff125e5a12ac7fdec2b90b7e2ae5120fa249cf62248337b6d04abc574c8
Status: Downloaded newer image for registry:2
4372a1dcfb48a03aea725980ff2df7896e675e3d0a2c4b681e8f6025ffbf85b5
dockeruser@dockeruser-VirtualBox:~$

13.3.3 Setup Authentication


1. Create an auth directory.
dockeruser@dockeruser-VirtualBox:~$ mkdir auth

2. Use the registry container to generate an htpasswd file in the auth directory that will contain
our username and password.
Be sure to replace username and password with the actual username and password you would
like to use.
dockeruser@dockeruser-VirtualBox:~$ docker run --entrypoint htpasswd registry:2 -Bbn
registryuser password123 > ./auth/htpasswd

3. This will save the htpasswd file in our auth directory that we just created. Stop the registry if
you still have it going:
dockeruser@dockeruser-VirtualBox:~$ docker container stop registry

4. Start it up again but this time with the authentication parameters:


dockeruser@dockeruser-VirtualBox:~$ docker run -d --restart=always --name registry -v
`pwd`/certs:/certs -v `pwd`/auth:/auth -e "REGISTRY_AUTH=htpasswd" -e
"REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" -e
REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd -e REGISTRY_HTTP_ADDR=0.0.0.0:5000 -
e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/registry-selfsigned.crt -e
REGISTRY_HTTP_TLS_KEY=/certs/registry-selfsigned.key -p 5000:5000 registry:2

42
Docker Lab Guide

13.3.4 Configuring an Insecure Registry


1. Edit /etc/docker/daemon.json and add this line:
dockeruser@dockeruser-VirtualBox:~$ sudo nano /etc/docker/daemon.json

{ "insecure-registries":["registry.example.lab:5000"] }
Then Save by Ctl +X and press Y to confirm. And Enter.

2. Update the /etc/hosts file as below.


dockeruser@dockeruser-VirtualBox:~$ sudo su
dockeruser@dockeruser-VirtualBox : # nano /etc/hosts

127.0.1.1 dockeruser-VirtualBox registry.example.lab

Then Save by Ctl +X and press Y to confirm. And Enter.


dockeruser@dockeruser-VirtualBox : # exit

3. Restart the docker service.


dockeruser@dockeruser-VirtualBox:~$# systemctl restart docker

13.3.5 Pull the image to store in your local registry


1. Login to hub.docker.com to download the images.

dockeruser@dockeruser-VirtualBox:~$ docker login


Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker
ID, head over to https://hub.docker.com to create one.
Username: premkumarmlp
Password:
WARNING! Your password will be stored unencrypted in /home/dockeruser/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
dockeruser@dockeruser-VirtualBox:~$ docker pull centos
Using default tag: latest
latest: Pulling from library/centos
Digest: sha256:184e5f35598e333bfa7de10d8fb1cebb5ee4df5bc0f970bf2b1e7c7345136426
Status: Image is up to date for centos:latest
dockeruser@dockeruser-VirtualBox:~$

13.3.6 Pushing Images to your Docker Registry


1. Login and push images to local registry. First login:
dockeruser@dockeruser-VirtualBox:~$ docker login registry.example.lab:5000
Enter the username and password you configured earlier. (regitryuser / password123)

43
Docker Lab Guide

2. Now pull a basic container down from Docker Hub, tag it with your registry and push the
container to your registry with these commands:

dockeruser@dockeruser-VirtualBox:~$ docker pull centos


dockeruser@dockeruser-VirtualBox:~$ docker tag centos registry.example.lab:5000/lab-centos
dockeruser@dockeruser-VirtualBox:~$ docker push registry.example.lab:5000/lab-centos
3. Check the image id centos from the dockets images and validate this id reflecting in the local
registry location : /var/lib/docker/image/overlay2/imagedb/content/sha256

<<<END OF LAB >>>

44
Docker Lab Guide

14 Lab-12: Docker Swarm Service


This LAB exercise explains how to Setup and Configure Docker Swarm Cluster

14.1 Time to Complete


Approximately 1.30 Hr.

14.2 What You Need


 Lab01 to be completed successfully.
 Create a second VM for configuring worker node.

14.3 Prerequisites
 2 or more - Ubuntu 18.04 Server
 manager 192.168.43.29
 worker01 192.168.43.198
 Root privileges

14.4 Setup Docker Swarm Service in Manager Node


1. Install docker machine in the manager node
dockeruser@dockeruser-VirtualBox:~$ sudo su

root@dockeruser-VirtualBox:/home/dockeruser# apt-get install -y curl

root@dockeruser-VirtualBox:/home/dockeruser# curl -L
https://github.com/docker/machine/releases/download/v0.6.0/docker-machine-`uname -s`-
`uname -m` > /usr/local/bin/docker-machine && \
chmod +x /usr/local/bin/docker-machine

dockeruser@dockeruser-VirtualBox:~$ docker-machine

2. Create the Swarm Cluster

In this step, we will create the Swarm Cluster of our nodes. And in order to create the swarm
cluster nodes, we need to initialize the swarm mode on the 'manager' node and then join the
'worker1' node to the cluster.

Initialize the Docker Swarm mode by running the docker command below on the 'manager'
node.

dockeruser@dockeruser-VirtualBox:~$ docker swarm init --advertise-addr 192.168.43.29


Swarm initialized: current node (aaeekuo1k9eywfqjhb92or4f9) is now a manager.

45
Docker Lab Guide

To add a worker to this swarm, run the following command:

docker swarm join --token SWMTKN-1-


1mq3cmf59gur88t4j5jjhe5efy4d9gw2w4ms37y8g5ad1somsz-0vif4xp2ton4cfuax7wvu7bb2
192.168.43.29:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the
instructions.

dockeruser@dockeruser-VirtualBox:~$

14.5 Setup Docker Swarm Service in Worker Node

1. Create a VM machine for worker1


2. Install the Docker CE as per the section 3.5
3. Install docker machine in the worker node
dockernode1@dockernode1-VirtualBox:~$ sudo su

root@dockernode1-VirtualBox:/home/dockernode1# apt-get install -y curl

root@dockernode1-VirtualBox:/home/dockernode1# curl -L
https://github.com/docker/machine/releases/download/v0.6.0/docker-machine-`uname -s`-
`uname -m` > /usr/local/bin/docker-machine && \
chmod +x /usr/local/bin/docker-machine

dockernode1@dockernode1-VirtualBox:~$ docker-machine

4. Get the IP address of this worker1 and manager node and add it in the /etc/hosts file of worker1
and manager node as below.

5. Join this worker node with the manager node as below

dockernode1@dockernode1-VirtualBox:~$ sudo docker swarm join --token SWMTKN-1-


1mq3cmf59gur88t4j5jjhe5efy4d9gw2w4ms37y8g5ad1somsz-0vif4xp2ton4cfuax7wvu7bb2
192.168.43.29:2377
This node joined a swarm as a worker.

6. Check in the manager node to validate both nodes are linked thru swarm services.

dockeruser@dockeruser-VirtualBox:~$ docker node ls


ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
ENGINE VERSION

46
Docker Lab Guide

smy5iyi8hejypcovxxws83ouy dockernode1-VirtualBox Ready Active


18.09.0
aaeekuo1k9eywfqjhb92or4f9 * dockeruser-VirtualBox Ready Active Leader
18.09.0
dockeruser@dockeruser-VirtualBox:~$

14.6 Deploying First Service to the Cluster

In this step, we will create and deploy our first service to the swarm cluster. We want to create
new service Nginx web server that will run on default http port 80, and then expose it to the
port 8080 on the host server, and then try to replicate the nginx service inside the swarm
cluster.

1. Create Service

Create new Nginx service named 'my-web' and expose the HTTP port of the container to the
port 8080 on the host.

dockeruser@dockeruser-VirtualBox:~$ docker service create --name my-web --publish 8080:80


nginx:1.13-alpine
And when it's created, check using docker service command below.

dockeruser@dockeruser-VirtualBox:~$ docker service ls

And you will get the result as shown below.

The Nginx service has been created and deployed to the swarm cluster as a service named 'my-
web', it's based on the Nginx Alpine Linux, expose the HTTP port of the container service to the
port '8080' on the host, and it has only 1 replicas.

2. Replicas and Scale the Service

Now we will make replicas for the 'my-web' service. We will make 2 replicas of the 'my-web'
service, so the service is accessible on the 'manager' and 'worker01' nodes.

To replicate the 'my-web' service, run the following command.

dockeruser@dockeruser-VirtualBox:~$ docker service scale my-web=2

47
Docker Lab Guide

And after it's complete, check again using docker service command.

dockeruser@dockeruser-VirtualBox:~$ docker service ls

And now the server has 2 replicates.

Open your web browser and type the manager node IP address with port 8080.
http://manager:8080/
And you will get the Nginx default page.

Below is the result from the 'worker01' node.


http://worker1:8080/

The Swarm Cluster has been created, and the Nginx service has been completed deployed to our
Swarm Cluster.
<<<END OF LAB >>>

48
Docker Lab Guide

15 Troubleshooting

15.1 Docker Not able to pull images or run containers


Issue: docker: open /var/lib/docker/tmp/GetImageBlob559315006: no such file or directory.

dockeruser@dockeruser-VirtualBox:~$ docker run hello-world


Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
d1725b59e92d: Pulling fs layer
docker: open /var/lib/docker/tmp/GetImageBlob559315006: no such file or directory.
See 'docker run --help'.

Solution:
dockeruser@dockeruser-VirtualBox:~$ sudo service docker stop
dockeruser@dockeruser-VirtualBox:~$ sudo service docker start

15.2 Install Java 8 using the Oracle JDK

15.2.1 Update Ubuntu


1. Run the following commands:
dockeruser@dockeruser-VirtualBox:~$ sudo apt-get update && apt-get upgrade

2. Install the required package if you don’t have it already installed:


dockeruser@dockeruser-VirtualBox:~$ sudo apt-get install software-properties-common

15.2.2 Add the Java repository


1. Add a 3-rd party repository to get the Oracle JDK. We’ll use the one from WebUpd8, but you can
use any other repository:
dockeruser@dockeruser-VirtualBox:~$ sudo add-apt-repository ppa:webupd8team/java

2. Update your package list again:


dockeruser@dockeruser-VirtualBox:~$ sudo apt-get update

15.2.3 Install Java


1. Run the following commands:
dockeruser@dockeruser-VirtualBox:~$ sudo apt-get install oracle-java8-installer

15.2.4 Configure Java


1. Check what Java version been installed using command
dockeruser@dockeruser-VirtualBox:~$ java -version
The output should be something like:

49
Docker Lab Guide

2. Set a default if you have multiple Java installations


If you have multiple Java installations, you can set a default one by using the following
command:

dockeruser@dockeruser-VirtualBox:~$ sudo update-alternatives --config java


You can also use this command to check if you have multiple installations.

You’ll get an output with a list of installed Javas. Press enter to keep the default one without any
changes or enter a number to select a different default Java.

50

Вам также может понравиться