Skip to content

Commit

Permalink
Merge pull request #49 from CiscoCloud/vibhi
Browse files Browse the repository at this point in the history
Vibhi
  • Loading branch information
agentlecisco committed Sep 18, 2020
2 parents 8b58ab6 + e705dde commit 59cd85c
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 183 deletions.
14 changes: 5 additions & 9 deletions labs/docker-101/1.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Docker 101

## Objective

This Learning Lab introduces Docker, a tool suite for building, sharing, and deploying containers. For more information about containers, see the [Containers-101](https://developer.cisco.com/learning/lab/containers-101/step/1) Lab. Use this Learning Lab to build, deploy, and optionally share your first Docker container.
This Learning Lab introduces Docker, a tool suite for building, sharing, and deploying containers. Use this Learning Lab to build, deploy, and optionally share your first Docker container. For more information about containers, see the [Containers-101](https://developer.cisco.com/learning/lab/containers-101/step/1) Lab.
Further reading is available in the excellent Docker introduction at [imapex.io](https://github.com/imapex-training/mod_adv_docker/blob/master/README.md).

## For best results
This is a hands-on Lab, but each example builds on the previous and explains why you run the commands. If you just skip to each command to run, without reading the text in between, you will not learn anything.
Expand All @@ -14,10 +14,6 @@ This is a hands-on Lab, but each example builds on the previous and explains why
* System Architects and Engineers
* IT teams addressing developer needs for Docker and Containers

## Content Notice

Thanks to the Cisco Live EU 2017 workshop "Introduction to Containers" for portions of this Learning Lab. Further reading is available in the excellent docker introduction at [imapex.io](https://github.com/imapex-training/mod_adv_docker/blob/master/README.md).

## Prerequisite

* Install Docker on your system.
Expand Down Expand Up @@ -69,7 +65,7 @@ The play-with-docker.com site provides access to a full VM running Docker direct

The simplest way to use Docker is to run an existing public image that's available from [Docker Hub](https://hub.docker.com/).

Docker Hub is a public exchange for sharing Docker containers. Other Docker sharing sites are available, but we'll take advantage of the fact that Docker's command-line interface searches DockerHub by default.
Docker Hub is a public exchange for sharing Docker containers. Other Docker sharing sites are available, but we'll take advantage of the fact that Docker's command-line interface searches Docker Hub by default.

To run a publicly-available Docker Container, follow these steps:

Expand All @@ -79,7 +75,7 @@ To run a publicly-available Docker Container, follow these steps:

![docker search hello-world](assets/images/dockersearch.png)

Docker searches the public DockerHub repositories and finds the "hello-world" image.
Docker searches the public Docker Hub repositories and finds the "hello-world" image.
You can see that Docker has found the "hello-world" image. Let's run it.

2. Execute the following command to run the hello-world:
Expand All @@ -88,7 +84,7 @@ To run a publicly-available Docker Container, follow these steps:

![docker run hello-world](assets/images/dockerrun1.png)

Docker first checks to see whether the "hello-world" image is available locally. If not, Docker automatically downloads it from DockerHub. Docker sets up the container to run locally, ensuring its isolation from other processes. Once the preparations are made, Docker runs the image.
Docker first checks to see whether the "hello-world" image is available locally. If not, Docker automatically downloads it from Docker Hub. Docker sets up the container to run locally, ensuring its isolation from other processes. Once the preparations are made, Docker runs the image.

Congratulations! You just ran your first Docker container!

Expand Down
14 changes: 7 additions & 7 deletions labs/docker-101/2.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Get or create a Docker image

How did the author of "hello-world" create the container image? Because it's available on the public DockerHub repository, we can find out by inspecting the files.
How did the author of "hello-world" create the container image? Because it's available on the public Docker Hub repository, we can find out by inspecting the files.

Browse to [the "hello world" web page](https://hub.docker.com/_/hello-world/) to see details describing the Docker image.

Expand All @@ -14,7 +14,7 @@ Among those details is a link to the __Dockerfile__, the file that defines the i

Every Docker image, from a simple one like "hello-world" to a complex application, is defined by a __Dockerfile__. The Dockerfile is a text file that lists the programs and resources that become part of the Docker image and which instructs Docker what to do when the container runs. An author creates a Docker image by assembling application source code and other resource files and writing a Dockerfile that turns them into a working container image.

To create the container image, the author executes the `docker build` command from the directory that contains the Dockerfile. Docker reads the Dockerfile and follows the instructions in it to build the container image. Once the Docker image is built, the author can push it to the public repository on DockerHub, making it available to everyone.
To create the container image, the author executes the `docker build` command from the directory that contains the Dockerfile. Docker reads the Dockerfile and follows the instructions in it to build the container image. Once the Docker image is built, the author can push it to the public repository on Docker Hub, making it available to everyone.

Most container authors store their Dockerfiles in a version-control repository along with the files used in building their images. Storing a Dockerfile along with the files that it uses to build a container simplifies both the Dockerfile itself and the process of building the container image.

Expand Down Expand Up @@ -72,25 +72,25 @@ On the one hand, building a whole userspace into a container means that the cont

On the other hand, it means that even a complex application with many dependencies can be delivered in the form of a container image. The container can even include operating-system package-management software like APT or YUM for use in configuring optional dependencies.

Suppose you want to build a container image that includes the whole Ubuntu system. Begin by searching DockerHub for a suitable container to start with:
Suppose you want to build a container image that includes the whole Ubuntu system. Begin by searching Docker Hub for a suitable container to start with:

```
docker search ubuntu
```

We're in luck; DockerHub has an existing Ubuntu container.
We're in luck; Docker Hub has an existing Ubuntu container.

Run the container:

```
docker run -ti ubuntu
```

The example command downloads and runs the Ubuntu container from DockerHub. The command-line flags "-ti" tell Docker that we want to interact with a shell in the running image. As soon as the image finishes launching it dutifully presents us with a shell:
The example command downloads and runs the Ubuntu container from Docker Hub. The command-line flags "-ti" tell Docker that we want to interact with a shell in the running image. As soon as the image finishes launching it dutifully presents us with a shell:

![Ubuntu shell](assets/images/ubuntu1.png)

# Testing Isolation.
# Testing Isolation

The processes that run in a Docker container are completely isolated from the outside world. Files and processes on the host machine are invisible to the application in the container. The container application can't create or interact with any files or other resources outside the container.

Expand Down Expand Up @@ -125,7 +125,7 @@ It's important to remember that changes to a running Docker container do not sur

# The Ubuntu Dockerfile

Let's look at the Dockerfile that built the Ubuntu image. You can find an Ubuntu Dockerfile at [DockerHub](https://github.com/tianon/docker-brew-ubuntu-core/blob/7a8f63add8a1003a6f77bbcf00e4d408ea96ca1b/bionic/Dockerfile).
Let's look at the Dockerfile that built the Ubuntu image. You can find an Ubuntu Dockerfile at [Docker Hub](https://github.com/tianon/docker-brew-ubuntu-core/blob/7a8f63add8a1003a6f77bbcf00e4d408ea96ca1b/bionic/Dockerfile).

Click the link to the Dockerfile just as we did with the "hello-world" image. The Ubuntu DockerFile is more complex, as we would expect, but the concepts are exactly the same:

Expand Down
152 changes: 75 additions & 77 deletions labs/docker-101/3.md
Original file line number Diff line number Diff line change
@@ -1,132 +1,130 @@
# Building a Docker Image

Next we'll build our own Ubuntu Docker image instead of using the one from DockerHub.
Next we'll build our own Ubuntu Docker image instead of using the one from Docker Hub.

1. __Download the files__. Begin by downloading the Ubuntu Dockerfile and its dependencies.
1. Download the Ubuntu Dockerfile and its dependencies.
```
git clone --single-branch --branch dist-amd64 https://github.com/tianon/docker-brew-ubuntu-core.git
```

2. __Check the Dockerfile__. Change directory to the cloned repository and ensure that the Dockerfile is present.
2. Change directory to the cloned repository and ensure that the Dockerfile is present.
```
cd docker-brew-ubuntu-core/xenial
ls .
```

3. __Build a new image using Docker and the Dockerfile__.
3. Build a new image using Docker and the Dockerfile.
```
docker build .
```

Docker prints progress messages as it builds the Ubuntu image. Once it finishes building the image, Docker assigns it a randomly-chosen name.

4. __Run the command `docker images`__. Docker lists all the images on your system, including both the newly-built one and any that you previously downloaded. You can tell which image is the one you just created by examining the list; it's the only image that isn't associated with a repository:
4. Run the command `docker images`.
Docker lists all the images on your system, including both the newly-built one and any that you previously downloaded. You can tell which image is the one you just created by examining the list; it's the only image that isn't associated with a repository:
![Docker Images](assets/images/images1.png)

5. __Run the new image__. Run the newly created docker image by giving the randomly-chosen ID to Docker:
5. Run the newly created Docker image by giving the randomly-chosen ID to Docker:
```
docker run -ti <your image ID>
```
The newly-created image behaves exactly the same way as the Ubuntu image from Dockerhub because was built from the same Dockerfile. You now have a bash root prompt. Enter ``ls`` to confirm standard Ubuntu directories are installed.
The newly-created image behaves exactly the same way as the Ubuntu image from Dockerhub, because it is built from the same Dockerfile. You now have a bash root prompt.

6. Enter ``ls`` to confirm standard Ubuntu directories are installed.

# Building a Custom Cisco Learning Labs Container

To modify the image that we just built, all we have to do is change the Dockerfile and ensure that any needed resources are available in the repository. Let's make an image that's more specific to our application's needs.
To modify the image that we just built, all we have to do is change the Dockerfile and ensure that all required resources are available in the repository. Let's make an image that's more specific to our application's needs.

I'm currently sitting in at Cisco LIVE! Europe 2017 in the wonderful
city of Berlin, so let's change the image to say that when it runs:
Let's change the image to display the following message:

![Custom Docker Output](assets/images/hellocustom1.png)
```
Hello from DevNet!
```

To make the image display that message we will:
To make the image display the above message we will:

1. Create a new Python script.

2. Edit the Dockerfile to include that script in the container build.

3. Change the Dockerfile to install python in the container (remember,
all dependencies must be present in the container).
3. Change the Dockerfile to install Python in the container (remember, all dependencies must be present in the container).

4. Build and test the new container.

You have a choice young Jedi:

You can either
To update the image, follow the below instructions:

* Copy and paste following Dockerfile commands
* Type them into the file manually
* Download the Github repository that contains them
1. Clone the Github repository that contains the Dockerfile for this section using the following command:

To clone the Github repository that contains the Dockerfile for this
section using the following command:
```
git clone https://github.com/CiscoDevNet/container-intro-devnet.git
cd container-intro-devnet
```

```
git clone https://github.com/CiscoDevNet/container-intro-devnet.git
cd container-intro-devnet
```
The cloned repository contains these files:

The cloned repository contains two files:
* hellodevnet.py
* Dockerfile

* hellodevnet.py
* Dockerfile
The "hellodevnet.py" file is our custom application. It contains the following Python code:

The "hellodevnet.py" file is our custom application. It contains the
following Python code:
``` python
#!/usr/bin/env python
print("Hello from DevNet!")
```

``` python
#!/usr/bin/env python
The Dockerfile contains the instructions required to build the custom image:

print("Hello from DevNet!")
```
```
FROM ubuntu
RUN apt-get update
RUN apt-get -y install python
COPY hellodevnet.py /hellodevnet.py
RUN ["chmod", "+x", "/hellodevnet.py"]
CMD ["/hellodevnet.py"]
```

The Dockerfile contains the instructions needed to build the custom
image:
This Dockerfile says:

```
FROM ubuntu
RUN apt-get update
RUN apt-get -y install python
ADD hellodevnet.py /hellodevnet.py
CMD ["/hellodevnet.py"]
```
1. `FROM ubuntu`
Extend the existing ubuntu public Docker image. Our previous examples built an image from scratch. In this case we begin with a previously-built container image and extend with our own customisations.

This Dockerfile says:
2. `RUN apt-get update`
Ensure the package-management tools in the base Ubuntu container are updated to use the latest software.

1. `FROM ubuntu`
Extend the existing ubuntu public docker image. Our previous
examples built an image from scratch. In this case we begin with a
previously-built container image and extend with our own
customisations.
3. `RUN apt-get -y install python`
Use apt-get to install Python and all its dependencies in the container. The reason to build this container on an existing Ubuntu image is that we could use apt-get to install needed software.

2. `RUN apt-get update`
Ensure the package-management tools in the base Ubuntu container
are updated to use the latest software.
4. `COPY hellodevnet.py /hellodevnet.py`
Copy the Python program from the local directory into the container as `/hellodevnet.py`.

5. `RUN ["chmod", "+x", "/hellodevnet.py"]`
Grant permission to execute the `/hellodevnet.py` file

6. `CMD ["/hellodevnet.py"]`
Run the Python program when the container starts up.

>**Note:** Instead of cloning this GitHub repository, you can also add the above mentioned Python code in the hellodevnet.py file and Dockerfile commands in the Dockerfile using the `vi` command in the terminal and continue with step 2.
2. Build the Docker image using the following command:

3. `RUN apt-get -y install python`
Use apt-get to install Python and all its dependencies in the
container. The reason to build this container on an existing Ubuntu
image was so that we could use apt-get to install needed software.
```
docker build .
```
Docker prints progress messages as it builds the Ubuntu image. Once it finishes building the image, Docker displays `Succesfully built <container ID> `.
Make note of the newly built container ID so that you can use it in the next step.

3. Run the new container using the following command.
Use the container ID that you recieved from the `docker build .` command output.

4. `ADD hellodevnet.py /hellodevnet.py`
Copy our Python program from the local directory into the container
as `/hellodevnet.py`.
```
docker run <Container ID>
```
The "Hello from DevNet!" message is displayed in the container terminal.

![](assets/images/docker-image-new.png)

5. `CMD ["/hellodevnet.py"]`
Run our Python program when the container starts up.
Congratulations, you've just built and run a custom Docker image!

That's all there is to it! Now let's actually build the new container image.

```
docker build .
```

![Custom Docker Build](assets/images/dockerbuildcustom.png)

After the Docker build completes you can run the new Container. When
you do, you should see the "hello" message displayed in the container
terminal.

Congratulations, you've just built and run a custom docker image!

Now let's build a container image that does something more useful: how
about a container that runs a web server?
Now let's build a container image that does something more useful: how about a container that runs a web server?
Loading

0 comments on commit 59cd85c

Please sign in to comment.