On Github npalm / docker-introduction
Created by Niek Palm (2016)
//: //#### Docker architecture
BUILD an Image from a Dockerfile
RUN (create+start) a container
COMMIT (persist) a container to a new imageRUN a new container from an image
STOP or KILL a running container
START a stopped container
RM (delete) a stopped container
RMI a container image (delete image)
docker pull mongo:latest # pull the mongo image from the registry docker inspect mongo:latest # list information of the container docker run -p 27017:27017 \ --name my-mongo -d \ mongo:latest # create and start a mongo container docker inspect my-mongo # inspect the running container info docker logs -f my-mongo # tail the log of the container docker stop my-mongo # stop the container docker rm -v my-mongo # remove the container docker rmi mongo:latest # remove the image from the local repo
Each Dockerfile is a script, composed of various commands and arguments listed successively to automatically perform actions on a base image in order to create a new one. Such script are used for organizing things and greatly help with deployments by simplifying the process start-to-finish.
FROM dockerfile/java:oracle-java8 MAINTAINER Niek Palm <dev.npalm@gmail.com> RUN apt-get install git -y ADD service.jar EXPOSE 8080 CMD ["java","-jar","/service.jar"]
Tmux lets you switch easily between several programs in one terminal, detach them (they keep running in the background) and reattach them to a different terminal.
Ctrl-b c : creates a new window Ctrl-b n : go to next window Ctrl-b p : go to previous window Ctrl-b " : split window top/bottom Ctrl-b % : split window left/right Ctrl-b Alt-1 : rearrange windows in columns Ctrl-b Alt-2 : rearrange windows in rows Ctrl-b arrows : navigate to other windows Ctrl-b d : detach session tmux attach : reattach to session
chmod 400 <path>/HANDSON.pem
ssh -o ServerAliveInterval=120 -i <path>/HANDSON.pem ubuntu@<ip-aws-instance>
mkdir docker-introduction cd docker-introduction
vagrant init npalm/ubuntu-1404-dev-desktop
vagrant up --provider virtualbox
vagrant up # Starts the VM vagrant halt # Stops the VM vagrant destory # Removes the VM vagrant box list # Shows all the local vagrant boxes vagrant box remove <id> # Removes a box
docker help # Partial output Commands: images List images logs Fetch the logs of a container ps List containers pull Pull an image or a repository from a Docker registry rm Remove one or more containers rmi Remove one or more images run Run a command in a new container start Start a stopped container stop Stop a running container Run 'docker help' for all commands. Run 'docker COMMAND --help' for more information on a command.
docker pull ubuntu
docker images
docker run ubuntu echo "hello world"
docker ps docker ps -a
docker rm <id or name>
docker run -d --name mycontainer ubuntu /bin/sh -c \ "while true; do echo Hello world; sleep 1; done"
docker logs -f mycontainer
docker ps docker stop mycontainer docker ps docker ps -a
# Start our container docker start mycontainer # Exec the bash shell, this command gives access to our container docker exec -i -t mycontainer /bin/bash ## You should see now something like: > root@<id>:/# _
apt-get update && apt-get install cowsay -y && \ ln /usr/games/cowsay /usr/bin/cowsay && rm -rf /var/lib/apt/lists/*
cowsay "Hello world"
exit
docker exec -i -t mycontainer cowsay "Hello <name>"
docker commit mycontainer <yourname>/ubuntu
docker diff mycontainer # shows the added files docker history ubuntu # shows the image history docker history <yourname>/ubuntu # shows the image history
docker stop mycontainer \ | xargs docker rm # remove the container
docker run --rm <yourname>/ubuntu cowsay "Hello world"
docker run --rm ubuntu cowsay "Hello world"
docker help # Partial output Commands: build Build an image from a Dockerfile commit Create a new image from a container's changes info Display system-wide information inspect Return low-level information on a container or image login Register or log in to a Docker registry server logout Log out from a Docker registry server port Lookup the public-facing port that is NAT-ed to PRIVATE_PORT push Push an image or a repository to a Docker registry server tag Tag an image into a repository top Lookup the running processes of a container
mkdir lab2-web cd lab2-web
<!DOCTYPE html> <html><head> <meta charset="UTF-8"> <title>Hello world</title></head> <body>Hello world</body> </html>
With a dockerfile you specify how an image is build, which files are added, and which command should executed when the container is started.
Create a file named Dockerfile and add the following content
FROM nginx MAINTAINER <your name> <your.mail@domain.ext> COPY index.html /usr/share/nginx/html/
docker build --tag lab2/webapp .
docker images
docker run -d --name myapp -p 8888:80 lab2/webapp
docker stop myapp | xargs docker rm
docker port $(docker run -d --name myapp -p 80 lab2/webapp) | \ cut -d\> -f2 | \ xargs curl
docker stop myapp | xargs docker rm
The next step is to automate the build.
# Ensure you are in the directory lab2-web echo '# lab2-web' >> README.md git init git add --all git commit -m "Some comment" git remote add origin <your git url> git push -u origin master
docker run -d --name myapp -p 8888:80 \ <docker-hub-account>/lab2-web
docker run -d -p 8888:80 --name myapp -v \ <dir-to-webapp>:/usr/share/nginx/html nginx
# Run a container with no network docker run --rm --net none busybox:latest ifconfig # Run a container in a bridged network docker run --rm --net bridge busybox:latest ifconfig # or (bridge is the default) docker run --rm busybox:latest ifconfig # joined docker run --name joined1 -d --net none busybox:latest \ nc -l 127.0.0.1:3333 docker run --rm -it --net container:joined1 busybox:latest netstat -al # host docker run --rm --net host busybox:latest ifconfig
# EXAMPLE ONLY docker run -d --name postgres <image> <command>
# EXAMPLE ONLY docker run -d --link postgres:db --name web <image> <command
Next we build a simple cluster containing.
Clone the following git repo.
cd git clone https://github.com/npalm/simple-docker-cluster.git cd simple-docker-cluster
# Search for the offical redis image in the docker registry docker search redis # download the image docker pull redis # inspect the image and look for the volumes listed docker inspect redis
# start a redis container docker run -d --name redis redis # find the volume name and list the volumes. docker inspect --format='{{range .Mounts}}{{.Name}}{{end}}' redis docker volume ls # remove the redis contaienr, -v will remove the volume as well. docker rm -v -f redis
# start the data store mkdir .data \ docker run -d --name redis -v $(pwd)/.data:/data redis
# Build the image docker build -t lab3/web web # Start the container, not exposting the port is optional docker run -d -p 8080:8080 --link redis:redis --name web1 lab3/web # Test curl http://localhost:8080 # Inspect logging docker logs -f web1 # Add two more nodes docker run -d --link redis:redis --name web2 lab3/web docker run -d --link redis:redis --name web3 lab3/web
# Build the image docker build -t lab3/proxy proxy # Start the container. docker run -d -p 80:80 --name proxy \ --link web1:web1 --link web2:web2 --link web3:web3 lab3/proxy # Test your cluster and inspect the logging # Open a new terminal docker logs -f proxy # Back to terminal one and fire some requests (or use the browser) for i in {0..99}; do curl http://localhost; echo ""; done
# -v removes inplicit mounts volumes # -f force to remove running containers # -q shows id only # -a show all (also stpped ones) docker rm -v -f $(docker ps -q -a)
# have a look at the compose file cat docker-compose.yml # build the conatiners docker-compose build # start the containers docker-compose up # execute in a new terminal window for i in {0..99}; do curl http://localhost; echo ""; done
Docker Machine makes it really easy to create Docker hosts on your computer, on cloud providers, and inside your data center. It creates servers, installs Docker on them, then configures the Docker client to talk to them.
Compose is a tool for defining and running complex applications with Docker. With Compose, you define a multi-container application in a single file, and then you spin your application up in a single command which does everything that needs to be done to get it running.
gitlabdb: image: postgres environment: - POSTGRES_USER=gitlab - POSTGRES_PASSWORD=password gitlab: image: sameersbn/gitlab links: - gitlabdb:postgresql environment: - DB_USER=gitlab - DB_PASS=password ports: - "10080:80" volumes: - ./data/gitlab/data:/home/git/data
Docker Swarm is native clustering for Docker. It turns a pool of Docker hosts into a single, virtual host.
docker run -p 80:80 npalm/docker-introduction