-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from CiscoCloud/vibhi
Vibhi
- Loading branch information
Showing
6 changed files
with
157 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? |
Oops, something went wrong.