Hands-on Lab 2.0
Inside the container:
code
libraries
package manager
app
data
Outside the container:
logging
remote access
networking
monitoring
security
FROM ubuntu
RUN apt-get update && apt-get install -y apache2 && apt-get clean
ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
EXPOSE 80
CMD ["/usr/sbin/apache2", "-D", "FOREGROUND"]
Docker supports multiple rootfs
ps | List containers |
kill | Kill a running container |
restart | Restart a running container |
rm | Remove a container |
run | Run a command in a new container |
start | Start a stopped container |
stop | Stop a running container |
wait | Block until a container stops, then print its exit code |
info | Display system-wide information |
inspect | Return low-level information on a container |
logs | Fetch the logs of a container |
port | Lookup the public-facing port which is NAT-ed to PRIVATE_PORT |
attach | Attach to a running containe |
insert | Insert a file in an image |
diff | Inspect changes on a container's filesystem |
commit | Create a new image from a container's changes |
build | Build a container from a Dockerfile |
import | Create a new filesystem image from the contents of a tarball |
export | Stream the contents of a container as a tar archive |
images | List images |
rmi | Remove an image |
history | Show the history of an image |
login | Register or Login to the docker registry server |
pull | Pull an image or a repository from the docker registry server |
push | Push an image or a repository to the docker registry server |
search | Search for an image in the docker index |
tag | Tag an image into a repository |
$ docker pull ubuntu
$ docker run ubuntu /bin/echo "hello world"
Running containers in the background
# run -d means detached detach
$ DOCKER_ID=$(docker run -d ubuntu \
bash -c 'while true ; \
do sleep 1; \
echo hello world at $(date); \
done' )
$ echo $DOCKER_ID # shows id of container
$ docker attach $DOCKER_ID # attach to stdout of the container
$ docker ps # shows all running containers
$ docker stop $DOCKER_ID # stops specified container
$ docker ps -a # shows stopped and running containers
$ docker rm $DOCKER_ID # removes the container
# Look at an empty filesystem
$ docker run ubuntu /bin/ls /tmp
# Modify the filesystem
$ DOCKER_ID=$(docker run -d ubuntu \
bash -c 'while true ; do \
date > /tmp/$(date +%Y%m%d%H%M); \
sleep 60;\
done')
# See the changes on the filesystem
$ docker diff $DOCKER_ID
# Stop the instance
$ docker stop $DOCKER_ID; docker rm $DOCKER_ID
# Changes are gone!
$ docker run ubuntu /bin/ls /tmp
creating a new image
# Modify the filesystem
$ DOCKER_ID=$(docker run -d ubuntu \
bash -c 'while true ; do \
date > /tmp/$(date +%Y%m%d%H%M); \
sleep 60; \
done')
# See the changes on the filesystem and commit
$ docker diff $DOCKER_ID
$ docker commit $DOCKER_ID $USER/mydemo # name of image $USER/mydemo
# Stop and remove the instance
$ docker stop $DOCKER_ID ; docker rm $DOCKER_ID
# Changes are persisted!
$ docker run $USER/mydemo /bin/ls /tmp
Dockerfile
From
Run
CMD
ENTRYPOINT
EXPOSE
Lab Exercise
create a tomcat images
FROM tomcat
Build a new image
$ cd tomcat7
$ docker build -t tkssharma/tomcat7 .
push to the registry
$ docker login
$ sudo docker tag tkssharma/tomcat7 tkssharma/tomcat7:v0.1
$ docker push tkssharma/tomcat7:v0.1
running tomcat
# Start a tomcat container
$ DOCKER_ID=$(docker run -P -d tkssharma/tomcat7:v0.1)
# docker inspect show details about the container
$ docker inspect $DOCKER_ID
# Obtain mapped port of port 8080 of the container
$ PORT=$(docker port $DOCKER_ID 8080)
# access tomcat via mapped port
$ curl http://localhost:$PORT
# Obtain ip address of container
$ IPADDRESS=$(docker inspect -f '{{.NetworkSettings.IPAddress}}' $DOCKER_ID)
# http request on image IP address
$ curl http://$IPADDRESS:8080
creating a farm of tomcat
count=0
TOMCAT_IPS=""
while [ $count -lt 5 ] ; do
DOCKER_ID=$(docker run -P –d <docker-hub-name>/tomcat7:v0.1)
IPADDRESS=$(docker inspect -f '{{.NetworkSettings.IPAddress}}' $DOCKER_ID)
TOMCAT_IPS="$TOMCAT_IPS $IPADDRESS"
count=$(($count + 1))
done
echo all tomcats : $TOMCAT_IPS
Lab Exercise
Dockerfile
FROM nodesource/node:4.0
ADD package.json package.json
RUN npm install
ADD . .
CMD ["node","app.js"]
Dockerfile
$ docker build .
Step 0 : FROM nodesource/node:4
---> 813c5874eb90
Step 1 : ADD package.json package.json
---> 45726a0a7fb3
Removing intermediate container 78cd990108c5
Step 2 : RUN npm install
---> Running in 14a14e26e19f
retry@0.7.0 node_modules/retry
pg@4.4.1 node_modules/pg
├── packet-reader@0.2.0
├── pg-connection-string@0.1.3
├── buffer-writer@1.0.0
├── generic-pool@2.1.1
├── semver@4.3.6
├── pgpass@0.0.3 (split@0.3.3)
└── pg-types@1.10.0 (postgres-bytea@1.0.0, ap@0.2.0, postgres-date@1.0.0, postgres-array@1.0.0, postgres-interval@1.0.0)
---> 4b6ede2c7fd7
Removing intermediate container 14a14e26e19f
Step 3 : ADD . .
---> 0c5891f99c6c
Removing intermediate container 5bdc6717ea4c
Step 4 : CMD node app.js
---> Running in 5c75cb1759a7
---> fec7c6f133a9
Removing intermediate container 5c75cb1759a7
Successfully built fec7c6f133a9
DockerHub
$ docker login
Username: your_user_name
Password:
Email: your_email@foo.bar
Login Successful!
$ docker push “your_user_name/myapp”