Skip to content

Instantly share code, notes, and snippets.

@boredstiff
Created August 31, 2015 23:52
Show Gist options
  • Save boredstiff/f56b11f29ee8bb8d250d to your computer and use it in GitHub Desktop.
Save boredstiff/f56b11f29ee8bb8d250d to your computer and use it in GitHub Desktop.
Notes of this:
https://training.docker.com/introduction-to-docker
https://training.docker.com/docker-fundamentals
https://training.docker.com/docker-operations
Docker:
Engine - program that enables containers to be built, shipped and run.
Engine uses Linux Kernel namespaces and control groups.
Namespaces give us the isolated workspace referred to as a 'container'
Namespaces:
PID: Process Isolation
NET: Manages network stack
IPC:
MNT: Mount points
UTS:
Hub: Public registry that contains a large number of images. App store..
Machine: Tool that provisions Docker hosts and installs the Docker Engine on them.
Swarm: Tool that clusters many Engines and schedules containers.
Compose: Tool to create and manage multi-container applications.
Kitematic: GUI for docker.
Images vs. Containers:
Images:
Read-only template used to create containers.
Built by you or other Docker users.
Stored in Docker Hub or your local registry.
Containers:
Isolated application platform.
Contains everything needed to run application.
Based on one or more images.
Images are created from containers.
Container Processes:
A container only runs as long as the process from your specified docker run command is running.
Your command's process is always PID 1 inside the container.
Container ID:
Containers can be specified using their ID or name
Containers have a long ID and a short ID.
Short ID and name can be obtained by using docker ps to list containers.
Long ID can be obtained by inspecting a container.
Registry:
Repositories.
Images are stored inside of registries.
Publice Registry: Docker Hub: hub.docker.com
Docker Orchestration:
Organization and management of the computer systems, middleware and services.
Running in detached mode
Runs in the background or as a daemon.
-d flag
Observing output:
docker logs [container id]
Create a centos container and run ping 50 times on itself.
docker run -d centos:7 ping 127.0.0.1 -c 50
Commands:
Getting docker on Linux
wget -q0- https://get.docker.com/ | sh
Windows:
After installing on Windows, run
docker-machine create --driver virtualbox my-default
This will take a minute.
List machines:
docker-machine ls
Get environmment commands for new VM:
docker-machine env --shell cmd my-default
Then run them in the shell to connect envvars.
Connect your shell to the my-default machine:
eval "$(docker-machine env my-default)"
run hello-world container to verify setup.
docker run hello-world
download and install or run if it's installed locally, Ubuntu 14.04:
docker run ubuntu:14.04
Container with Terminal:
docker run -i -t ubuntu:latest /bin/bash
-i tells Docker to connect to stdin on the container
-t specifies to get a pseudo-terminal
List running containers
docker ps
docker ps -a
-a flag will list all containers. Includes ones that have stopped.
Create a container using tomcat, run in detached, map the tomcat ports to the host ports.
docker run -d -P tomcat:7
With windows, there's an issue with getting the TLS working:
http://stackoverflow.com/questions/32047660/docker-for-windows-not-working
https://forums.docker.com/t/are-you-trying-to-connect-to-a-tls-enabled-daemon-without-tls/1947
For me, the key is to run this: eval "$(docker-machine env machineName)"
Every time.
And answer at the bottom says to disable Hyper-V.
Building Images:
Docker Commit:
Syntax:
docker commit [options] [container ID] [repository:tag]
docker commit (shortIDHere) alex/myapplication:1.0
You can use the name instead of the short ID.
So once you have an image running (I ran ubuntu), you can install a package
apt-get install curl
Then you can
exit
docker ps -a
Find the short ID, copy it.
Then run
docker commit 2381a907b88e alex/appname1.0
Dockerfile:
Configuration file that contains instructions for building a Docker image.
Provides a more effective way to build images compared to docker commit.
Easily fits into continuous integration and deployment process.
Made up of instructions:
Instructions specify what to do when building the image
FROM instruction specifies what the base image should be.
RUN instruction specifies a command to execute.
# Example of a comment
FROM ubuntu:14.04
RUN apt-get install vim
RUN apt-get install curl
.
Or you can combine them together:
RUN apt-get update && apt-get install -y\
curl \
vim \
openjdk-7-jdk
Each instruction builds a commit to the output file.
You'll see it commit as many times as there are instructions.
If you aggregate the instructions together, it will condense the commits.
Docker build:
docker build [options] [path]
docker build -t [repository:tag] [path]
Build an image using the current folder as the context path.
docker build -t alex/myimage:1.0 .
Same, but use the myproject folder as the context path.
docker build -t alex/myimage:1.0 myproject
You can make a dockerfile using vim in the shell.
CMD defined a default command to execute when a container is created.
CMD performs no action during the image build.
Shell format and EXEC format.
Can only be specified once in a Dockerfile.
Can be overridden at run time.
Shell format:
CMD ping 127.0.0.1 -c 30
Exec format:
CMD ["ping", "127.0.0.1", "-c", "30"]
ENTRYPOINT instruction.
Defines the command that will run when a container is executed.
Run time arguments and CMD instruction are passed as parameters to the ENTRYPOINT instruction.
Shell and Exec form.
Exec form preferred as shell form cannot accept arguments at run time.
Container essentially runs as an executable.
ENTRYPOINT ["ping"]
Managing Images and Containers
Starting and Stopping:
Find your containers first with docker ps and note the ID or name.
docker start
docker stop
If you're running a container in the background or as part of a daemon:
List all Containers:
docker ps -a
Start a container using the container ID:
docker start a938e94244bc
Stop a container using the container ID:
docker stop a938e94244bc
You can use the name as well.
--
docker run -d nginx
docker ps
Getting Terminal Access:
Use docker exec command to start another process within a container.
Execute /bin/bash to get a bash shell.
docker exec -i -t [container ID] /bin/bash
Exiting from the terminal will not terminate the container.
exit
Deleting containers:
Can only containers that have been stopped
command:
docker rm
Specify the container ID or name
Deleting local images:
docker rmi [image ID]
or
docker rmi [repo:tag]
If an image is tagged multiple times, remove each tag.
Then look at the remaining images:
docker images
Pushing images to Docker Hub:
docker push [repo:tag]
docker push alex/ubuntu:1.2
Tagging Images:
Used to rename a local image repository before pushing to Docker Hub.
docker tag [image ID] [repo:tag]
OR
docker tag [local repo:tag] [Docker Hub repo:tag]
--
Tag Image with ID
docker tag b87263dd2b23 alex/ubuntu:1.2
Tag Image with local repository tag
docker tag alex/ubuntu:1.2 onlineFolder/directory
Then you can push to the hub if you'd like.
docker push alex/ubuntu:1.2
It will push the images layer by layer
Volumes
A volume is a designated directory in a container, which is designed to persist data, independent of the container's life cycle.
Volume changes are excluded when updating an image.
Persist when a container is deleted.
Can be mapped to a host folder.
Can be shared between containers.
Mount A Volume:
Volumes are mounted when creating or executing a container.
Can be mapped to a host directory.
Volume paths specified must be absolute.
Execute a new container and mount the folder /myvolume into it's file system.
docker run -d -P -v /myvolume nginx:1.7
Execute a new container and map the /data/src folder from the host into the test/src folder in the container.
docker run -i -t -v /data/src:/test/src/nginx:1.7
Volumes in Dockerfile:
Volume instruction creates a mount point
Can specify arguments as a JSON array or a string
Cannot map volumes to host directory
Volumes are initialized when the container is executed
String:
VOLUME /myvol
String with multiple volumes:
VOLUME /www/website1.com /www/website2.com
JSON:
VOLUME ["myvol", "myvol2"]
Uses of volumes:
Decouple the data that is stored from the container which created the data
Good for sharing data between containers
Can set up a data container which has a volume you mount in other containers
Mounting folders from the host is good for testing purposes but not recommended for production use.
Usage(assuming you still have nginx installed from earlier):
docker run -d -P -v /www/website nginx
Grab the long ID
docker exec -it dba1d8ded402ac958cb29be3505c229a520896470eae76872893d887e2ec8f36 bash
ls
You should now see the www volume.
cd www/website/
# This puts hello into a file
echo "hello" >> test
ls
cat test
exit
docker stop dba1d8ded402ac958cb29be3505c229a520896470eae76872893d887e2ec8f36
docker commit dba1d8ded402ac958cb29be3505c229a520896470eae76872893d887e2ec8f36 test:1.0
docker run -it test:1.0 bash
If you look back in the www volume, you won't see the file because the volume changes are excluded when updating an image.
Container Networking Basics:
Mapping Ports:
Containers have their own network and IP address.
Map exposed container ports to ports on the host machine
Ports can be manually mapped or auto mapped.
Use the -p and -P parameters in docker run
Maps port 80 on the container to 8080 on the host.
docker run -d -p 8080:80 nginx:1.7
Automapping ports:
Use the -P option
Automatically maps them, will tell you what it is.
EXPOSE instruction:
Configures which ports a container will listen on at runtime
Ports still need to be mapped when container is executed
FROM ubuntu 14.04
RUN apt-get update
RUN apt-get install -y nginx
EXPOSE 80 443
CMD ["nginx", "-g", "daemon off;"]
Linking containers
Linking doesn't expose network ports.
Linking is a communication method between containers which allows them to securely transfer data from one to another.
Source and recipient containers.
Recipient containers have access to data on source containers.
Links are established based on container names.
Creating a Link:
Create the source container first
Then create the recipient container and use the --link option
Give containers meaningful names
Create the source container using the postgres
docker run -d --name database postgres
Create the recipient container and link it.
docker run -d -P --name website --link database:db nginx
Usage:
docker run -d --name dbms postgres
docker ps
docker run -it --name website --link dbms:db ubuntu:14.04 bash
cat /etc/hosts
=========================
Docker Operations:
Troubleshoot containers
Overview of security practices
private registry
Intro to Docker Machine
Intro to Docker Swarm
Intro to Docker Compose
Building micro service applications with Docker
If a container goes offline, it's probably because the application it was running crashed.
docker logs <container name>
View and follow the output:
docker logs -f <container name>
fda
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment