The purpose of this lesson is to create your first docker container. We'll do better and create several.
- An instance of Ubuntu 17.04
- An Apache web server
- A colleague's web server
- A better Apache web server
- A go-lang compiler
- A redis database
- A go web server
The difference between images and containers can be confusing. Basically a docker container is an instance of a docker image.
Docker images are the basis of containers. An Image is an ordered collection of root filesystem changes and the corresponding execution parameters for use within a container runtime. An image typically contains a union of layered filesystems stacked on top of each other. An image does not have state and it never changes.
A container is a runtime instance of a docker image.
A Docker container consists of
- A Docker image
- An execution environment
- A standard set of instructions
docker create --name my_ubuntu -it ubuntu:17.04
# docker ps -a
docker start -ia my_ubuntu
apt-get update
apt-get install screenfetch
screenfetch # discuss up-time
exit
# go back in and look at the history<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>My cool website</title>
</head>
<body>
<h1>My cool website</h1>
By Chris
</body>
</html>docker login
docker start -ia my_ubuntu
apt-get install vim nano
apt-get install apache2
echo > /var/www/html/index.html
nano /var/www/html/index.html
exit
docker commit -c 'CMD [ "apache2ctl", "-DFOREGROUND" ]' my_ubuntu ccouzens/apache:v1
docker run -p 8080:80 -d --name ubuntu_apache ccouzens/apache:v1
# visit http://localhost:8080/
docker rm -f ubuntu_apache
docker push ccouzens/apache:v1docker run -p 8080:80 -d --name ubuntu_apache colleague/apache:v1
# visit http://localhost:8080/
docker rm -f ubuntu_apacheOur Apache docker image has a couple shortcomings:
- The build steps are hard to automate
- We're including Ubuntu
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>My even cooler website</title>
</head>
<body>
<h1>My even cooler website</h1>
By Chris
</body>
</html>FROM httpd:2.4
COPY *.html /usr/local/apache2/htdocs/Save the html file as index.html. Save the dockerfile as Dockerfile.
docker build -t ccouzens/apache:v2 .
docker run --rm -p 8080:80 ccouzens/apache:v2
# visit http://localhost:8080/ then exit
^C
# optionally push and shareTake a look at go/web.go.
Most of you won't have go installed on your laptops.
So how do we compile it?
Answer: use docker!
# Linux host
docker run --rm -v "$PWD":/go/src/web -w /go/src/web golang:1.8 go build -v
./web # open http://localhost:8080/
# Mac host
docker run --rm -v "$PWD":/go/src/web -w /go/src/web -e GOOS=darwin golang:1.8 go build -v
./web # open http://localhost:8080/
# Windows host
docker run --rm -v "$PWD":/go/src/web -w /go/src/web -e GOOS=windows golang:1.8 go build -v
./web.exe # open http://localhost:8080/Docker containers can be short lived tools, not just servers.
This will only work if you have a docker version 17.06.0 or newer.
docker --version
git submodule update
docker build -t ccouzens/web-go-with-redis .
docker push ccouzens/web-go-with-redisRedis is a no-SQL database. We can connect our app to Redis running in a container.
docker network create --driver bridge go_app_nw
docker run -d --name my_redis --network=go_app_nw -p 6379:6379 redis # start the database
docker run -it --rm --network=go_app_nw redis redis-cli -h my_redis # connect to the database
SET who "Chris"
exit
## native
REDIS_HOST_PORT=localhost:6379 ./web # open http://localhost:8080/
## container
docker run --rm -e REDIS_HOST_PORT=my_redis:6379 -p 8080:8080 --network=go_app_nw ccouzens/web-go-with-redisWe have learnt about docker images and containers
We have seen several ways of building docker images:
- By committing a container
- With
docker build - With
docker buildmulti stage build
We have seen a couple ways of creating a running docker containers:
docker createfollowed bydocker startdocker run
We have seen that docker containers can be servers or tools.
We have seen that some docker images are useful immediately (redis) whereas others need building upon (apache).
We have seen that docker is a quick (and relatively safe) way to distribute and run software.