0% found this document useful (0 votes)
6 views

Docker

Uploaded by

yirga
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Docker

Uploaded by

yirga
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Introduction to Containers, Docker, and IBM Cloud Container Registry

Objectives
In this lab, you will:

Pull an image from Docker Hub


Run an image as a container using docker
Build an image using a Dockerfile
Push an image to IBM Cloud Container Registry

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.

Verify the environment and command line tools


1. Open a terminal window by using the menu in the editor: Terminal > New Terminal.

Note:If the terminal is already opened, please skip this step.


2. Verify that docker CLI is installed.
1. 1

1. docker --version

Copied!

You should see the following output, although the version may be different:

3. Verify that ibmcloud CLI is installed.


1. 1

1. ibmcloud version

Copied!

You should see the following output, although the version may be different:

4. Change to your project folder.

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

1. [ ! -d 'CC201' ] && git clone https://github.com/ibm-developer-skills-network/CC201.git

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!

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


1. Use the docker CLI to list your images.
1. 1

1. docker images

Copied!

You should see an empty table (with only headings) since you don’t have any images yet.

2. Pull your first image from Docker Hub.


1. 1

1. docker pull hello-world

Copied!
3. List images again.
1. 1

1. docker images

Copied!

You should now see the hello-world image present in the table.

4. Run the hello-world image as a container.


1. 1

1. docker run hello-world

Copied!

You should see a ‘Hello from Docker!’ message.

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

1. docker container rm <container_id>

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.

Build an image using a Dockerfile


1. 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.

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 COPY command enables us to copy files to our image.

The RUN instruction executes commands.

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.

3. Run the following command to build the image:


1. 1

1. docker build . -t myimage:v1


Copied!

As seen in the module videos, the output creates a new layer for each instruction in the Dockerfile.

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


1. 1

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.

Run the image as a container


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

1. docker run -dp 8080:8080 myimage:v1

Copied!

The output is a unique code allocated by docker for the application you are running.

2. Run the curl command to ping the application as given below.


1. 1

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

1. docker stop $(docker ps -q)

Copied!

5. Check if the container has stopped by running the following command.


1. 1

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!

You should see two namespaces listed starting with sn-labs:

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

1. docker tag myimage:v1 us.icr.io/$MY_NAMESPACE/hello-world:1

Copied!

7. Push the newly tagged image to IBM Cloud Container Registry.

1. 1

1. docker push us.icr.io/$MY_NAMESPACE/hello-world: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!

Optionally, to only view images within a specific namespace.

1. 1

1. ibmcloud cr images --restrict $MY_NAMESPACE

Copied!

You should see your image name in the output.

Congratulations! You have completed the second lab for the first module of this course.

© IBM Corporation. All rights reserved.

You might also like