docker_script

Install

curl -sSL -O https://get.docker.com/builds/Linux/x86_64/docker-1.9.1 && chmod +x docker-1.9.1 && sudo mv docker-1.9.1 /usr/local/bin/docker

Run daemon

sudo /usr/local/bin/docker daemon

Run client

docker info

Docker Images vs Docker Containers

Images

docker images
docker search linux
docker pull ubuntu
docker images

This does not cover how to create your own images

Containers

docker run ubuntu echo Hello World
docker ps
docker ps -a
docker logs $IMAGE_NAME

Running a container with a custom name

docker run --help | less
docker run --name HelloWorld ubuntu echo Hello World
docker ps -a
docker inspect HelloWorld | less
docker rm HelloWorld

Running an interactive container

docker run --name ubuntu_shell -t -i  ubuntu bash
exit
docker start ubuntu_shell
docker exec ubuntu_shell ifconfig
docker exec -t -i ubuntu_shell bash
exit
docker ps
docker attach ubuntu_shell
exit
docker ps
docker rm ubuntu_shell

Running a web application with docker

docker run --name web -d -P training/webapp python app.py
# See https://github.com/docker-training/webapp
docker ps # Note randomly assigned port mapping
docker port web 5000
docker rm -f web
docker run --name web -d -p 5000:5000 training/webapp python app.py
docker port web 5000
curl --noproxy localhost http://localhost:5000/
docker logs web
docker top web
docker rm -f web

Network containers

Check existing docker networks

docker network ls
docker network inspect bridge

Create new network

docker network create -d bridge privatenet
docker network inspect privatenet

Start DB container in privatenet

docker run -d --net=privatenet --name db training/postgres  # See https://github.com/docker-training/postgres

Start Web container in bridged network

docker run --name web -d -p 5000:5000 training/webapp python app.py
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' web

Check IP Addresses of each container

docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' db
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' web

Check DB container can’t access web container

docker exec -it db bash
ping $WEB_IP_ADDRESS
exit

docker network connect privatenet web
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' web
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' db
docker exec -it db bash
ping $WEB_PRIVATE_NET_IP_ADDRESS
ping web
exit
docker rm -f web db

Mounting volumes in containers

Volume characteristics

  • Initialized when a container is created
  • Can be shared and reused among containers
  • Changes to a data volume are not included when an image is updated
  • Data volumes persist even if the container is deleted

Sharing data between container and host

mkdir myfiles
echo Hello > myfiles/hello
docker run -it --name ubuntu -v $PWD/myfiles:/myfiles ubuntu bash
ls -l /myfiles
exit
docker inspect ubuntu | less # search for Mounts

Sharing data between containers

TODO

docker_script

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s