Wednesday, June 3, 2015

Useful docker commands

Check docker version:

> docker version

Search an image named tutorial:

> docker search tutorial

Download an image:

> docker pull learn/tutorial

Install ping on your image:

> docker run learn/tutorial apt-get install -y ping

Show running processes:

> docker ps -l

Grab the ID above and commit the change with a name. Docker will return a new ID for the new image. (Only the first three characters of the ID is enought)

> docker commit 698 learn/ping

See list of running container:

> docker ps

Grab the container ID above and you can inspect the information of the container by running

> docker inspect

Inspect one element of the container specs

> docker inspect -f '{{ .NetworkSettings.IPAddress }}' nostalgic_morse

Push to a docker repository:

> docker push learn/ping

Build an image from a DockerFile:

docker build -t   .

Remove all containers:

> docker stop $(docker ps -qa)

Run an app with interactive mode

> docker run -it --rm -p 3000:8080

See mapped ports.

> docker port

If you are using boot2docker, check the ip by:

> boot2docker ip

You should get something like 192.168.59.103

Stop the container:

> docker stop

Remove the container:

> docker rm

Shows standard output of a container:

> docker logs

See the end of the standard output of a container (if you are running a web app, you can see the outputs):

> docker logs -f

See applications running insider the container

> docker top

list all local images:

> docker images

list all containers, including exited

> docker ps -a

Remove all exited containers:

> docker ps -a | grep Exit | cut -d ' ' -f 1 | xargs docker rm 

Commit a change to an image with id 0b2616b0e5a8

> docker commit -m "Added json gem" -a "Kate Smith" \ 0b2616b0e5a8 ouruser/sinatra:v2

Use bash

> docker run -t -i training/sinatra /bin/bash

Port mapping from host to container.

> docker run -d -p 5000:5000 training/webapp python app.py

Port mapping from only localhost port to container.

> docker run -d -p 127.0.0.1:5000:5000 training/webapp python app.py

Port mapping from localhost dynamic port to container.

> docker run -d -p 127.0.0.1::5000 training/webapp python app.py

Port mapping to UDP.

> docker run -d -p 127.0.0.1:5000:5000/udp training/webapp python app.py

Check where on the host the container is mapped to.

> docker port 5000

Change name to web and run the web app.

> docker run -d -P --name web training/webapp python app.py

Inspect name of the container.

> docker inspect -f "{{ .Name }}"

Remove a running container

> docker rm -f

Create a web container and link to a db container (--link :alias)

> docker run -d --name db training/postgres
> docker run -d -P --name web --link db:db training/webapp python app.py

Inspect the link information:

> docker inspect -f "{{ .HostConfig.Links }}" web

Output: [/db:/web/db]

When containers are linked, docker automatically creates environment variables and a /etc/host file

> sudo docker run --rm --name web2 --link db:db training/webapp env

Output:

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

It is recommended to use /etc/host file to set locations.

> docker run -t -i --rm --link db:webdb training/webapp /bin/bash
> cat /etc/hosts

Output:

172.17.0.7  aed84ee21bde
. . .
172.17.0.5  webdb 6e5cdeb2d300 db

Ping the address:

> apt-get install -yqq inetutils-ping
> ping webdb

Restart db: (Note that the host /etc/hosts will auto update itself)

> docker restart db

Adding a data volume with -v:

> docker run -d -P --name web -v /webapp training/webapp python app.py

Note that docker volumes are persistent. Even if the container is removed, it will still be there.

Mount host directory to container's with read-write permissions

> docker run -d -P --name web -v /src/webapp:/opt/webapp training/webapp python app.py

Mount host directory to container's with read-only permission.

> docker run -d -P --name web -v /src/webapp:/opt/webapp:ro training/webapp python app.py

Mount a single file

> docker run --rm -it -v ~/.bash_history:/.bash_history ubuntu /bin/bash

Example run:


> docker run -it -d --name flask -p 3000:8080 flask/image:latest

No comments:

Post a Comment