Skip to content

Instantly share code, notes, and snippets.

@stevesloka
Created February 23, 2016 17:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevesloka/131090820ac226134240 to your computer and use it in GitHub Desktop.
Save stevesloka/131090820ac226134240 to your computer and use it in GitHub Desktop.
# Workshop 102: Dockerfiles
The goal of this workshop is to build out your own Docker image based upon a dockerfile.
This image will be a simple web server which will host some html files of a simple website.
## Steps:
1. Make a directory to host your project
- Create a file named "`Dockerfile`"
- In this file, add these lines:
```
FROM nginx:latest
MAINTAINER Your Name <your_email@upmc.edu>
```
_NOTE: It's good practice to add the maintainer to the Dockerfile. It can be located anywhere in the file, but common practice is to put at top._
- Head out to docker hub and see what Dockerfile made up this image (https://github.com/nginxinc/docker-nginx/blob/e82e776e7507868a887178bc7eaae5c1ed7aa47f/Dockerfile)
- Build an image from this dockerfile:
```
docker build -t workshop/web .
```
- Run an instance of the container:
```
docker run -d workshop/web
```
_NOTE: The "-d" flag dameonizes the container so you'll only get a long hash which is the containerid as the output._
- Exec into container:<br><br>
First find the docker id of the running container:
```
docker ps
```
- Next, exec into it:
```
docker exec -it <dockerId> bash
```
_NOTE: At this point you'll be inside the container at a bash prompt. We'll try to curl to test out the image works._
- Install curl:
```
apt-get update && apt-get install curl -y
```
- Test out the container:
```
curl http://localhost
```
- Did you see some HTML displayed to the screen? W00t! It worked!
- Stop the container && remove:
```
docker stop <dockerId>
docker rm <dockerId>
```
- Let's add curl to the image so we don't need to install manually next time. Add this line to the Dockerfile.
```
RUN apt-get update && apt-get install curl -y
```
- Now that we've changed the Dockerfile, let's rebuild the image:
```
docker build -t workshop/web .
```
- Run the container again, then exec in and see now you have curl!
```
docker run -d workshop/web
docker exec -it <dockerId> bash
curl http://localhost
```
- Run container and map host port:
```
docker run --rm -p 80:80 workshop/web
```
_NOTE: The '--rm' flag cleans up all volumes and containers when it exits, handy when testing out and don't want to clutter up your system._
- Validate from terminal curl:
```
curl http://<dockerHostIp>
```
_Do you see the same HTML output from before?_
- Validate from web browser by entering http://{dockerhostIp} into the url
- Also note the NGINX logs that happen while requests are make. Since the container is running the the foreground you can see all of them.
- Next we'll run the container in the background and see the same logs. Stop the running process (`CTRL-C`)
- Run container daemonized:
```
docker run -d -p 80:80 workshop/web
```
_NOTE: This maps port 80 on the host to port 80 in the container. If you want to test random ports, run the same command but don't specify a host port (e.g. docker run -d -p 80 workshop/web). Then use "docker ps" to see the port docker chose to map._
- View logs from container:
```
docker logs -f <containerId>
```
_This command will show all output and stream to your window live._
- Validate container is working using same methods previously (curl & browser) and now you can see the logs.
- Next we'll add custom html directory to the container to host our test website. Start by making a folder in your directory called `html`
- Create a file called `index.html` and add the following code:
```html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to my sweet site!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to my sweet site!</h1>
<p>If you see this page, you added custom code to a docker image!</p>
<a href="http://enterprises.upmc.com/">enterprises.upmc.com</a>.</p>
<p><em>Thank you for using Docker.</em></p>
</body>
</html>
```
- Update your dockerfile to add this line:
```
COPY html /usr/share/nginx/html
```
- Build a new image and this time give it a version:
```
docker build -t workshop/web:0.1 .
docker run --rm -p 80:80 workshop/web:0.1
```
- Validate the image using curl or browser and see your website! w00t!
- Make a change to the html and build a new image with new tag.
```
1. Change html
2. Build Image: docker build -t workshop/web:0.2 .
3. Run: docker run -d -p 80:80 workshop/web:0.2
```
- Now run multiple versions of the container and see how you can version your app with images:
```
docker run -d -p 9000:80 workshop/web:0.1
docker run -d -p 9001:80 workshop/web:0.2
```
_NOTE: Here to test, we had to use unique ports since we can't map 2 containers to the same port. So validate both versions of your app against ports 9000 && 9001._
- Next we'll volume in code from your machine to show how to enhance development cycles:
```
docker run --rm -p 80:80 -v ${PWD}/html:/usr/share/nginx/html workshop/web
```
- If you validate the site should look like version 0.2 of your site. Now update the html to make some changes, and refresh your browser, you should instantly see your changes without having to rebuild the image.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment