Skip to content

Instantly share code, notes, and snippets.

@fikriauliya
Last active September 23, 2015 06:41
Show Gist options
  • Save fikriauliya/46b3f7a8d12e53485afe to your computer and use it in GitHub Desktop.
Save fikriauliya/46b3f7a8d12e53485afe to your computer and use it in GitHub Desktop.
Docker
$ sudo docker run ubuntu:14.04 /bin/echo 'Hello world'
# The -t flag assigns a pseudo-tty or terminal inside our new container and the -i flag allows us to make an interactive connection by grabbing the standard in (STDIN) of the container.
$ sudo docker run -t -i ubuntu:14.04 /bin/bash
# A Daemonized Hello world
$ sudo docker run -d ubuntu:14.04 /bin/sh -c "while true; do echo hello world; sleep 1; done"
1e5535038e285177d5214659a068137486f96ee5c2e85a4ac52dc83f2ebe4147
$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1e5535038e28 ubuntu:14.04 /bin/sh -c 'while tr' 2 minutes ago Up 1 minute insane_babbage
$ sudo docker logs insane_babbage
hello world
hello world
hello world
. . .
# stop
$ sudo docker stop insane_babbage
insane_babbage
# Running a Web Application in Docker
$ sudo docker run -d -P training/webapp python app.py
$ sudo docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bc533791f3f5 training/webapp:latest python app.py 5 seconds ago Up 2 seconds 0.0.0.0:49155->5000/tcp nostalgic_morse
# bind to specific port
$ sudo docker run -d -p 5000:5000 training/webapp python app.py
$ sudo docker port nostalgic_morse 5000
0.0.0.0:49155
$ sudo docker logs -f nostalgic_morse
* Running on http://0.0.0.0:5000/
10.0.2.2 - - [23/May/2014 20:16:31] "GET / HTTP/1.1" 200 -
10.0.2.2 - - [23/May/2014 20:16:31] "GET /favicon.ico HTTP/1.1" 404 -
$ sudo docker top nostalgic_morse
PID USER COMMAND
854 root python app.py
$ sudo docker inspect nostalgic_morse
[{
"ID": "bc533791f3f500b280a9626688bc79e342e3ea0d528efe3a86a51ecb28ea20",
"Created": "2014-05-26T05:52:40.808952951Z",
"Path": "python",
"Args": [
"app.py"
],
"Config": {
"Hostname": "bc533791f3f5",
"Domainname": "",
"User": "",
. . .
$ sudo docker inspect -f '{{ .NetworkSettings.IPAddress }}' nostalgic_morse
172.17.0.5
$ sudo docker stop nostalgic_morse
nostalgic_morse
# restart container
$ sudo docker start nostalgic_morse
nostalgic_morse
# remove container
$ sudo docker stop nostalgic_morse
nostalgic_morse
$ sudo docker rm nostalgic_morse
nostalgic_morse
# Listing images on the host
$ sudo docker images
# Getting a new image
$ sudo docker pull centos
$ sudo docker run -t -i centos /bin/bash
bash-4.1#
# Search for image
$ sudo docker search sinatra
# Creating our own images
# 1) We can update a container created from an image and commit the results to an image.
$ sudo docker run -t -i training/sinatra /bin/bash
$ root@0b2616b0e5a8:/# gem install json
$ sudo docker commit -m="Added json gem" -a="Kate Smith" 0b2616b0e5a8 ouruser/sinatra:v2
# try the new image
$ sudo docker run -t -i ouruser/sinatra:v2 /bin/bash
# 2) We can use a Dockerfile to specify instructions to create an image.
$ mkdir sinatra
$ cd sinatra
$ touch Dockerfile
$ cat Dockerfile
# This is a comment
FROM ubuntu:14.04
MAINTAINER Kate Smith <[email protected]>
RUN apt-get update && apt-get install -y ruby ruby-dev
RUN gem install sinatra
$ sudo docker build -t="ouruser/sinatra:v2" .
# Create a container from the new image
$ sudo docker run -t -i ouruser/sinatra:v2 /bin/bash
root@8196968dac35:/#
# Setting tags on an image
$ sudo docker tag 5db5f8471261 ouruser/sinatra:devel
# Push an image to Docker Hub
$ sudo docker push ouruser/sinatra
# Remove an image from the host
$ sudo docker rmi training/sinatra
# To bind port 5000 of the container to a dynamic port but only on the localhost, you could use:
$ sudo docker run -d -p 127.0.0.1::5000 training/webapp python app.py
# You can also bind UDP ports by adding a trailing /udp. For example:
$ sudo docker run -d -p 127.0.0.1:5000:5000/udp training/webapp python app.py
$ sudo docker port nostalgic_morse 5000
127.0.0.1:49155
# Linking Containers Together
# Container naming
$ sudo docker run -d -P --name web training/webapp python app.py
$ sudo docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
aed84ee21bde training/webapp:latest python app.py 12 hours ago Up 2 seconds 0.0.0.0:49154->5000/tcp web
$ sudo docker inspect -f "{{ .Name }}" aed84ee21bde
/web
# Container Linking
$ sudo docker run -d --name db training/postgres
$ sudo docker run -d -P --name web --link db:db training/webapp python app.py
$ sudo docker inspect -f "{{ .HostConfig.Links }}" web
[/db:/web/db]
# Docker exposes connectivity information for the source container to the recipient container in two ways:
# 1) Environment variables,
$ sudo docker run --rm --name web2 --link db:db training/webapp env
. . .
DB_NAME=/web2/db
DB_PORT=tcp://172.17.0.5:5432
DB_PORT_5432_TCP=tcp://172.17.0.5:5432
DB_PORT_5432_TCP_PROTO=tcp
DB_PORT_5432_TCP_PORT=5432
DB_PORT_5432_TCP_ADDR=172.17.0.5
. . .
# 2) Updating the /etc/hosts file.
$ sudo docker run -t -i --rm --link db:db training/webapp /bin/bash
root@aed84ee21bde:/opt/webapp# cat /etc/hosts
172.17.0.7 aed84ee21bde
. . .
172.17.0.5 db
root@aed84ee21bde:/opt/webapp# apt-get install -yqq inetutils-ping
root@aed84ee21bde:/opt/webapp# ping db
PING db (172.17.0.5): 48 data bytes
56 bytes from 172.17.0.5: icmp_seq=0 ttl=64 time=0.267 ms
56 bytes from 172.17.0.5: icmp_seq=1 ttl=64 time=0.250 ms
56 bytes from 172.17.0.5: icmp_seq=2 ttl=64 time=0.256 ms
# Managing Data in Containers
# 1) Data volumes
# A data volume is a specially-designated directory within one or more containers that bypasses
# the Union File System to provide several useful features for persistent or shared data:
$ sudo docker run -d -P --name web -v /webapp training/webapp python app.py
# Mount a Host Directory as a Data Volume
$ sudo docker run -d -P --name web -v /src/webapp:/opt/webapp training/webapp python app.py
# Mount a Host File as a Data Volume
$ sudo docker run --rm -it -v ~/.bash_history:/.bash_history ubuntu /bin/bash
# 2) Data Volume Container
# If you have some persistent data that you want to share between containers, or want to use from non-persistent containers,
# it's best to create a named Data Volume Container, and then to mount the data from it.
$ sudo docker run -d -v /dbdata --name dbdata training/postgres echo Data-only container for postgres
$ sudo docker run -d --volumes-from dbdata --name db1 training/postgres
$ sudo docker run -d --volumes-from dbdata --name db2 training/postgres
$ sudo docker run -d --name db3 --volumes-from db1 training/postgres
# Backup, restore, or migrate data volumes
$ sudo docker run --volumes-from dbdata -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar /dbdata
$ sudo docker run -v /dbdata --name dbdata2 ubuntu /bin/bash
$ sudo docker run --volumes-from dbdata2 -v $(pwd):/backup busybox tar xvf /backup/backup.tar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment