Docker
Docker
Objectives
In this lab, you will:
Note: Kindly complete the lab in a single session without any break because the lab may go on offline mode and may cause errors. If you face any issues/errors during the lab process, please logout from the
lab environment. Then clear your system cache and cookies and try to complete the lab.
Important:
You may already have an IBM Cloud account and may even have a namespace in the IBM Container Registry (ICR). However, in this lab you will not be using your own IBM Cloud account or your own ICR namespace. You will be
using an IBM Cloud account that has been automatically generated for you for this excercise. The lab environment will not have access to any resources within your personal IBM Cloud account, including ICR namespaces and images.
1. docker --version
Copied!
You should see the following output, although the version may be different:
1. ibmcloud version
Copied!
You should see the following output, although the version may be different:
Note: If you are already on the ‘/home/project’ folder, please skip this step.
1. 1
1. cd /home/project
Copied!
5. Clone the git repository that contains the artifacts needed for this lab, if it doesn’t already exist.
1. 1
Copied!
6. Change to the directory for this lab by running the following command. cd will change the working/current directory to the directory with the name specified, in this case CC201/labs/1_ContainersAndDcoker.
1. 1
1. cd CC201/labs/1_ContainersAndDocker/
Copied!
7. List the contents of this directory to see the artifacts for this lab.
1. 1
1. ls
Copied!
1. docker images
Copied!
You should see an empty table (with only headings) since you don’t have any images yet.
Copied!
3. List images again.
1. 1
1. docker images
Copied!
You should now see the hello-world image present in the table.
Copied!
There will also be an explanation of what Docker did to generate this message.
5. List the containers to see that your container ran and exited successfully.
1. 1
1. docker ps -a
Copied!
Among other things, for this container you should see a container ID, the image name (hello-world), and a status that indicates that the container exited successfully.
6. Note the CONTAINER ID from the previous output and replace the <container_id> tag in the command below with this value. This command removes your container.
1. 1
Copied!
7. Verify that that the container has been removed. Run the following command.
1. 1
1. docker ps -a
Copied!
Congratulations on pulling an image from Docker Hub and running your first container! Now let’s try and build our own image.
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.
2. 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 commands required to build an image.
You can refresh your understanding of the commands mentioned in the Dockerfile below:
The FROM instruction initializes a new build stage and specifies the base image that subsequent instructions will build upon.
The EXPOSE instruction exposes a particular port with a specified protocol inside a Docker Container.
The CMD instruction provides a default for executing a container, or in other words, an executable that should run in your container.
As seen in the module videos, the output creates a new layer for each instruction in the Dockerfile.
1. docker images
Copied!
Note that compared to the hello-world image, this 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.
Copied!
The output is a unique code allocated by docker for the application you are running.
1. curl localhost:8080
Copied!
If you see the output as above, it indicates that ‘Your app is up and running!’.
4. Now to stop the container we use docker stop followed by the container id. The following command uses docker ps -q to pass in the list of all running containers:
1. 1
Copied!
1. docker ps
Copied!
Push the image to IBM Cloud Container Registry
1. The environment should have already logged you into the IBM Cloud account that has been automatically generated for you by the Skills Network Labs environment. The following command will give you information about the account
you’re targeting:
1. 1
1. ibmcloud target
Copied!
2. The environment also created an IBM Cloud Container Registry (ICR) namespace for you. Since Container Registry is multi-tenant, namespaces are used to divide the registry among several users. Use the following command to see the
namespaces you have access to:
1. 1
1. ibmcloud cr namespaces
Copied!
The first one with your username is a namespace just for you. You have full read and write access to this namespace.
The second namespace, which is a shared namespace, provides you with only Read Access
3. Ensure that you are targeting the region appropriate to your cloud account, for instance us-south region where these namespaces reside as you saw in the output of the ibmcloud target command.
1. 1
1. ibmcloud cr region-set us-south
Copied!
4. Log your local Docker daemon into IBM Cloud Container Registry so that you can push to and pull from the registry.
1. 1
1. ibmcloud cr login
Copied!
5. Export your namespace as an environment variable so that it can be used in subsequent commands.
1. 1
1. export MY_NAMESPACE=sn-labs-$USERNAME
Copied!
6. Tag your image so that it can be pushed to IBM Cloud Container Registry.
1. 1
Copied!
1. 1
Copied!
Note: If you have tried this lab earlier, there might be a possibility that the previous session is still persistent. In such a case, you will see a ‘Layer already Exists’ message instead of the ‘Pushed’ message in the above output. We
recommend you to proceed with the next steps of the lab.
8. Verify that the image was successfully pushed by listing images in Container Registry.
1. 1
1. ibmcloud cr images
Copied!
1. 1
Copied!
Congratulations! You have completed the second lab for the first module of this course.