Skip to main content

Docker Activities 1

Docker Activities 1:

Verify the environment and command line tools:

1. Verify the Docker version:

theia@theiadocker-mohiindia:/home/project$ docker --version

Docker version 19.03.6, build 369ce74a3c


2. Verify the IBMCloud Version:

theia@theiadocker-mohiindia:/home/project$ ibmcloud version

ibmcloud version 1.2.3+3577aee6-2020-09-25T14:34:09+00:00


3.Clone the git repository that contains the artifacts needed for this lab:

theia@theiadocker-mohiindia:/home/project$ git clone https://gitlab.com/ibm/skills-network/courses/cc201.git

Cloning into 'cc201'...

remote: Enumerating objects: 755, done.

remote: Counting objects: 100% (755/755), done.

remote: Compressing objects: 100% (391/391), done.

remote: Total 755 (delta 424), reused 654 (delta 352), pack-reused 0

Receiving objects: 100% (755/755), 6.43 MiB | 18.71 MiB/s, done.

Resolving deltas: 100% (424/424), done.


4. Change to the directory for this lab.

theia@theiadocker-mohiindia:/home/project$ cd cc201/labs/1_ContainersAndDocker/

theia@theiadocker-mohiindia:/home/project/cc201/labs/1_ContainersAndDocker$ 


5. List the contents of this directory to see the artifacts for this lab.

theia@theiadocker-mohiindia:/home/project/cc201/labs/1_ContainersAndDocker$ ls

app.js  Dockerfile  images  instructions.md  package.json


Pull an image from Docker Hub and run it as a container:


6.Use the docker CLI to list your images:

theia@theiadocker-mohiindia:/home/project/cc201/labs/1_ContainersAndDocker$ docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE


7.Pull your first image from Docker Hub:

theia@theiadocker-mohiindia:/home/project/cc201/labs/1_ContainersAndDocker$ docker pull hello-world

Using default tag: latest

latest: Pulling from library/hello-world

0e03bdcc26d7: Pull complete 

Digest: sha256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Status: Downloaded newer image for hello-world:latest

docker.io/library/hello-world:latest


8. List images again to see this image in the local environment:

theia@theiadocker-mohiindia:/home/project/cc201/labs/1_ContainersAndDocker$ docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

hello-world         latest              bf756fb1ae65        11 months ago       13.3kB


9.Run the hello-world image as a container:

theia@theiadocker-mohiindia:/home/project/cc201/labs/1_ContainersAndDocker$ docker run hello-world


Hello from Docker!

This message shows that your installation appears to be working correctly.


To generate this message, Docker took the following steps:

 1. The Docker client contacted the Docker daemon.

 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.

    (amd64)

 3. The Docker daemon created a new container from that image which runs the

    executable that produces the output you are currently reading.

 4. The Docker daemon streamed that output to the Docker client, which sent it

    to your terminal.


To try something more ambitious, you can run an Ubuntu container with:

 $ docker run -it ubuntu bash


Share images, automate workflows, and more with a free Docker ID:

 https://hub.docker.com/


For more examples and ideas, visit:

 https://docs.docker.com/get-started/


10. List the containers to see that your container ran and exited successfully:

theia@theiadocker-mohiindia:/home/project/cc201/labs/1_ContainersAndDocker$ docker ps -a

CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS                          PORTS               NAMES

367ffb2e7826        hello-world         "/hello"            About a minute ago   Exited (0) About a minute ago                       romantic_herschel


11. Note the container ID from the previous step, and remove the container now that we've run it:

theia@theiadocker-mohiindia:/home/project/cc201/labs/1_ContainersAndDocker$ docker container rm 367ffb2e7826

367ffb2e7826


Congratulations on pulling an image and running you first container! Now let's build an image on our own instead of using one pulled from "Docker Hub".


Build an image using a Dockerfile:

The current working directory contains a simple Node.js application that we will run in a container. The app will print a hello message along with the hostname. The following files are needed to run the app in a container:

app.js is the main application, which simply replies with a hello world message.

package.json defines the dependencies of the application.

Dockerfile defines the instructions Docker uses to build the image.


12.Use the Explorer to view the files needed for this app. Click the Explorer icon (it looks like a sheet of paper) on the left side of the window, and then navigate to the directory for this lab: cc201 > labs > 1_ContainersAndDocker. Click Dockerfile to view the Dockerfile we'll use to build an image


Dockerfile:

FROM node:9.4.0-alpine

COPY app.js .

COPY package.json .

RUN npm install &&\

    apk update &&\

    apk upgrade

EXPOSE  8080

CMD node app.js


13. Run the following command to build the image:

theia@theiadocker-mohiindia:/home/project/cc201/labs/1_ContainersAndDocker$ docker build . -t myimage:v1

Sending build context to Docker daemon  495.1kB

Step 1/6 : FROM node:9.4.0-alpine

9.4.0-alpine: Pulling from library/node

605ce1bd3f31: Pull complete 

fe58b30348fe: Pull complete 

46ef8987ccbd: Pull complete 

Digest: sha256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Status: Downloaded newer image for node:9.4.0-alpine

 ---> b5f94997f35f

Step 2/6 : COPY app.js .

 ---> d8c76c92b4d6

Step 3/6 : COPY package.json .

 ---> 35db5a5fdfb9

Step 4/6 : RUN npm install &&    apk update &&    apk upgrade

 ---> Running in aff0d1157d9a

npm notice created a lockfile as package-lock.json. You should commit this file.

npm WARN hello-world-demo@0.0.1 No repository field.

npm WARN hello-world-demo@0.0.1 No license field.


added 50 packages in 2.122s

fetch http://dl-cdn.alpinelinux.org/alpine/v3.6/main/x86_64/APKINDEX.tar.gz

fetch http://dl-cdn.alpinelinux.org/alpine/v3.6/community/x86_64/APKINDEX.tar.gz

v3.6.5-44-gda55e27396 [http://dl-cdn.alpinelinux.org/alpine/v3.6/main]

v3.6.5-34-gf0ba0b43d5 [http://dl-cdn.alpinelinux.org/alpine/v3.6/community]

OK: 8448 distinct packages available

Upgrading critical system libraries and apk-tools:

(1/1) Upgrading apk-tools (2.7.5-r0 -> 2.7.6-r0)

Executing busybox-1.26.2-r9.trigger

Continuing the upgrade transaction with new apk-tools:

(1/7) Upgrading musl (1.1.16-r14 -> 1.1.16-r15)

(2/7) Upgrading busybox (1.26.2-r9 -> 1.26.2-r11)

Executing busybox-1.26.2-r11.post-upgrade

(3/7) Upgrading libressl2.5-libcrypto (2.5.5-r0 -> 2.5.5-r2)

(4/7) Upgrading libressl2.5-libssl (2.5.5-r0 -> 2.5.5-r2)

(5/7) Installing libressl2.5-libtls (2.5.5-r2)

(6/7) Installing ssl_client (1.26.2-r11)

(7/7) Upgrading musl-utils (1.1.16-r14 -> 1.1.16-r15)

Executing busybox-1.26.2-r11.trigger

OK: 5 MiB in 15 packages

Removing intermediate container aff0d1157d9a

 ---> 4469f11f1f4e

Step 5/6 : EXPOSE  8080

 ---> Running in dc76747631b5

Removing intermediate container dc76747631b5

 ---> 68561ebb43ed

Step 6/6 : CMD node app.js

 ---> Running in b771d45eefa0

theia@theiadocker-mohiindia:/home/project/cc201/labs/1_ContainersAndDocker$

Note: The above output should include a step for each instruction in the Dockerfile. Each step creates a new layer in the image.


14. List images to see your image tagged myimage:v1 in the table:

theia@theiadocker-mohiindia:/home/project/cc201/labs/1_ContainersAndDocker$ docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

myimage             v1                  d4863712eef1        2 minutes ago       76.2MB

hello-world         latest              bf756fb1ae65        11 months ago       13.3kB

node                9.4.0-alpine        b5f94997f35f        2 years ago         68MB


Note that compared to the hello-world image, this "myimage" image has a different image ID. This means that the two images consist of different layers -- in other words, they're not the same image.


You should also see a node image in the images output. This is because the docker build command pulled node:9.4.0-alpine to use it as the base image for the image you built.


Run the image as a container:

15. Now that your image is built, run it as a container with the following command:

theia@theiadocker-mohiindia:/home/project/cc201/labs/1_ContainersAndDocker$ docker run -p 8080:8080 myimage:v1

Sample app is listening on port 8080.

Note: The output should indicate that your application is listening on port 8080. This command will continue running until it is quit, since the container runs a web app that continually listens for requests. To query the app, we need to open another terminal window.


In the second terminal window, use the curl command to ping the application.

Output from another Terminal,

theia@theiadocker-mohiindia:/home/project$ curl localhost:8080

Hello world from 184335a83242! Your app is up and running!


The output should indicate that your app is up and running.


16.In the second terminal window, stop the container. The following command uses "docker ps -q" to pass in the list of all running containers:

From Second Terminal,

theia@theiadocker-mohiindia:/home/project$ docker ps -q

184335a83242

theia@theiadocker-mohiindia:/home/project$ docker stop 184335a83242184335a83242


From First Terminal, you can see that the "docker run" command has exited and you are able to type commands in that terminal window again.

theia@theiadocker-mohiindia:/home/project/cc201/labs/1_ContainersAndDocker$ docker run -p 8080:8080 myimage:v1

Sample app is listening on port 8080.

theia@theiadocker-mohiindia:/home/project/cc201/labs/1_ContainersAndDocker$

 

theia@theiadocker-mohiindia:/home/project$ docker container ls

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

theia@theiadocker-mohiindia:/home/project$ docker container ls -a

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS               NAMES

184335a83242        myimage:v1          "/bin/sh -c 'node ap…"   7 minutes ago       Exited (137) 2 minutes ago                       gifted_jang


17. Close the second terminal window, as it is no longer needed.


Push the image to IBM Cloud Container Registry:

1. The environment should have already logged you into an IBM Cloud account. The following command will give you information about the account you're targeting:

theia@theiadocker-mohiindia:/home/project/cc201/labs/1_ContainersAndDocker$ ibmcloud target

                      

API endpoint:      https://cloud.ibm.com   

Region:            us-south   

User:              ServiceId-e846778e-1eff-42fd-bd29-be8a497562d1   

Account:           QuickLabs - IBM Skills Network (f672382e1b43496b83f7a82fd31a59e8)   

Resource group:    No resource group targeted, use 'ibmcloud target -g RESOURCE_GROUP'   

CF API endpoint:      

Org:                  

Space:                


18. The environment also created an IBM Cloud Container Registry namespace for you. Since Container Registry is multi-tenant, namespaces are used to divvy up the registry among several users. Use the following command to see your namespace:


theia@theiadocker-mohiindia:/home/project/cc201/labs/1_ContainersAndDocker$ ibmcloud cr namespaces

Listing namespaces for account 'QuickLabs - IBM Skills Network' in registry 'us.icr.io'...


Namespace   

sn-labs-mohiindia   


OK


19. However, since you created your own IBM Cloud Container Registry namespace in the previous lab, we'll use that one for this lab. To do so, login to your IBM Cloud account:

theia@theiadocker-mohiindia:/home/project/cc201/labs/1_ContainersAndDocker$ ibmcloud login -u xyz@gmail.com

API endpoint: https://cloud.ibm.com

Region: us-south


Password> 

Authenticating...

OK


Targeted account Mohankumar Gandhi's Account (xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx)


                      

API endpoint:      https://cloud.ibm.com   

Region:            us-south   

User:              xyz@gmail.com   

Account:           Mohankumar Gandhi's Account (xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx)   

Resource group:    No resource group targeted, use 'ibmcloud target -g RESOURCE_GROUP'   

CF API endpoint:      

Org:                  

Space:                


We'd like to collect usage statistics to help improve the IBM Cloud CLI.

This data will never be shared outside IBM.

To learn more, see the IBM Privacy Policy: https://www.ibm.com/privacy

You can enable or disable usage data collection by running 'ibmcloud config --usage-stats-collect [true | false]'


Do you want to send usage statistics to IBM? [y/n]> y

theia@theiadocker-mohiindia:/home/project/cc201/labs/1_ContainersAndDocker$ 


After you logged in with your credentials, now check the "namespace" and "target" details,

theia@theiadocker-mohiindia:/home/project/cc201/labs/1_ContainersAndDocker$ ibmcloud cr namespaces

Listing namespaces for account 'Mohankumar Gandhi's Account' in registry 'us.icr.io'...


Namespace   

mohi   


OK

theia@theiadocker-mohiindia:/home/project/cc201/labs/1_ContainersAndDocker$ ibmcloud target


                      

API endpoint:      https://cloud.ibm.com   

Region:            us-south   

User:              srimano.india@gmail.com   

Account:           Mohankumar Gandhi's Account (xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx)   

Resource group:    No resource group targeted, use 'ibmcloud target -g RESOURCE_GROUP'   

CF API endpoint:      

Org:                  

Space:                



20. Ensure that you are targeting the us-south region where you created your namespace.


theia@theiadocker-mohiindia:/home/project/cc201/labs/1_ContainersAndDocker$ ibmcloud cr region-set us-south

The region is set to 'us-south', the registry is 'us.icr.io'.


OK


21. Log your local Docker daemon into IBM Cloud Container Registry so that you can push to and pull from the registry:

theia@theiadocker-mohiindia:/home/project/cc201/labs/1_ContainersAndDocker$ ibmcloud cr login

Logging in to 'registry.ng.bluemix.net'...

Logged in to 'registry.ng.bluemix.net'.

Logging in to 'us.icr.io'...

Logged in to 'us.icr.io'.


OK


22. Export your namespace as an environment variable so that it can be used in subsequent commands. Make sure to substitute your namespace after the equals sign. If you don't remember your namespace, run "ibmcloud cr namespaces". Since you're now logged into your personal account, 

theia@theiadocker-mohiindia:/home/project/cc201/labs/1_ContainersAndDocker$ export MY_NAMESPACE=mohi


23. Tag your image so that it can be pushed to IBM Cloud Container Registry:

theia@theiadocker-mohiindia:/home/project/cc201/labs/1_ContainersAndDocker$ docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

myimage             v1                  d4863712eef1        38 minutes ago      76.2MB

hello-world         latest              bf756fb1ae65        11 months ago       13.3kB

node                9.4.0-alpine        b5f94997f35f        2 years ago         68MB


theia@theiadocker-mohiindia:/home/project/cc201/labs/1_ContainersAndDocker$ docker tag myimage:v1 us.icr.io/mohi/hello-world:1

theia@theiadocker-mohiindia:/home/project/cc201/labs/1_ContainersAndDocker$ docker images

REPOSITORY                   TAG                 IMAGE ID            CREATED             SIZE

myimage                      v1                  d4863712eef1        40 minutes ago      76.2MB

us.icr.io/mohi/hello-world   1                   d4863712eef1        40 minutes ago      76.2MB   --> Newly tagged/created

hello-world                  latest              bf756fb1ae65        11 months ago       13.3kB

node                         9.4.0-alpine        b5f94997f35f        2 years ago         68MB



24. Push the newly tagged image to IBM Cloud Container Registry:

theia@theiadocker-mohiindia:/home/project/cc201/labs/1_ContainersAndDocker$ docker push us.icr.io/mohi/hello-world:1

The push refers to repository [us.icr.io/mohi/hello-world]

df5df6ac3bb0: Pushed 

cd6cec0cdbe3: Pushed 

ee7baad21065: Pushed 

0804854a4553: Pushed 

6bd4a62f5178: Pushed 

9dfa40a0da3b: Pushed 

1: digest: sha256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx size: 1576



25. Verify that the image was successfully pushed by listing images in Container Registry:

theia@theiadocker-mohiindia:/home/project/cc201/labs/1_ContainersAndDocker$ ibmcloud cr images

Listing images...


Repository                   Tag   Digest         Namespace   Created          Size    Security status   

us.icr.io/mohi/hello-world   1     ecc1e85eee6f   mohi        42 minutes ago   27 MB   No Issues   


OK


You should see your image name in the output. Recall from the module videos that we discussed Vulnerability Advisor, which scans images in IBM Cloud Container Registry for common vulnerabilities and exposures. In the last column of the output, note that Vulnerability Advisor is either scanning your image or it has provided a security status, depending on how quickly you list the images and how long the scan takes.



26. Since you will use the namespace in the lab account in future labs, log back into the lab account:

theia@theiadocker-mohiindia:/home/project/cc201/labs/1_ContainersAndDocker$ ibmcloud login --apikey $IBMCLOUD_API_KEY

API endpoint: https://cloud.ibm.com

Region: us-south

Authenticating...

OK


Targeted account QuickLabs - IBM Skills Network (xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx)


                      

API endpoint:      https://cloud.ibm.com   

Region:            us-south   

User:              ServiceId-e846778e-1eff-42fd-bd29-be8a497562d1   

Account:           QuickLabs - IBM Skills Network (xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx)   

Resource group:    No resource group targeted, use 'ibmcloud target -g RESOURCE_GROUP'   

CF API endpoint:      

Org:                  

Space:                

theia@theiadocker-mohiindia:/home/project/cc201/labs/1_ContainersAndDocker$ ibmcloud cr namespaces

Listing namespaces for account 'QuickLabs - IBM Skills Network' in registry 'us.icr.io'...


Namespace   

sn-labs-mohiindia   


OK


Congratulations! 






Comments

Popular posts from this blog

Docker Installation

  ​ Installation ​ Linux Install script provided by Docker: curl -sSL https://get.docker.com/ | sh Or see Installation instructions for your Linux distribution. ​ Create Docker VM with Docker Machine You can use Docker Machine to: Install and run Docker on Mac or Windows Provision and manage multiple remote Docker hosts Provision Swarm clusters A simple example to create a local Docker VM with VirtualBox: docker-machine create --driver=virtualbox default docker-machine ls eval "$(docker-machine env default)" Then start up a container: docker run alpine echo "hello-world" That's it, you have a running Docker container. ​ Container lifecycle Create a container: docker create imageName . Create and start a container in one operation: docker run imageName Remove the container after it stops --rm : docker run --rm alpine ls /usr/lib Attach the container stdin/stdout to the current terminal use -it : docker run -it ubuntu bash To mount a directory on the host to a