Monday, 30 May 2016

Docker command tips

This post is to allow me remember how to use docker :)
since i am getting older i tend to forget new syntax more than before !!

0- Docker pull:
Docker pull will fetch a docker image from the default docker registry.
eg:
docker pull redis:latest
 
You can browse the official docker repository at: https://hub.docker.com/explore/
Another way to look for docker images is to use docker search as below:
[root@fingolfin stock_apache_docker]# docker search mysql
INDEX       NAME                                 DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/mysql                      MySQL is a widely used, open-source relati...   2409      [OK]     
docker.io   docker.io/mysql/mysql-server         Optimized MySQL Server Docker images. Crea...   149                  [OK]
docker.io   docker.io/centurylink/mysql          Image containing mysql. Optimized to be li...   45                   [OK]
docker.io   docker.io/sameersbn/mysql                                                            35                   [OK]
docker.io   docker.io/google/mysql               MySQL server for Google Compute Engine          16                   [OK]
docker.io   docker.io/appcontainers/mysql        Centos/Debian/Ubuntu Based Customizable My...   7                    [OK]
docker.io   docker.io/marvambass/mysql           MySQL Server based on Ubuntu 14.04              6                    [OK]
docker.io   docker.io/alterway/mysql             Docker Mysql                                    2                    [OK]
docker.io   docker.io/azukiapp/mysql             Docker image to run MySQL by Azuki - http:...   2                    [OK]
docker.io   docker.io/drupaldocker/mysql         MySQL for Drupal                                2                    [OK]
docker.io   docker.io/yfix/mysql                 Yfix docker built mysql                         2                    [OK]
docker.io   docker.io/bahmni/mysql               Mysql container for bahmni.  Contains the ...   1                    [OK]
docker.io   docker.io/frodenas/mysql             A Docker Image for MySQL                        1                    [OK]
docker.io   docker.io/ivories/mysql              mysql                                           1                    [OK]
docker.io   docker.io/phpmentors/mysql           MySQL server image                              1                    [OK]
docker.io   docker.io/sin30/mysql                MySQL images with my own config files.          1                    [OK]
docker.io   docker.io/akilli/mysql               akilli/base based MySQL image                   0                    [OK]
docker.io   docker.io/cloudposse/mysql           Improved `mysql` service with support for ...   0                    [OK]
docker.io   docker.io/dockerizedrupal/mysql      docker-mysql                                    0                    [OK]
docker.io   docker.io/lancehudson/docker-mysql   MySQL is a widely used, open-source relati...   0                    [OK]
docker.io   docker.io/livingobjects/mysql        MySQL                                           0                    [OK]
docker.io   docker.io/nanobox/mysql              MySQL service for nanobox.io                    0                    [OK]
docker.io   docker.io/projectomakase/mysql       Docker image for MySQL                          0                    [OK]
docker.io   docker.io/tozd/mysql                 MySQL (MariaDB fork) Docker image.              0                    [OK]
docker.io   docker.io/vukor/mysql                Build for MySQL. Project available on http...   0                    [OK]
[root@fingolfin stock_apache_docker]#
Also there is a useful link to how to setup a local registry: https://docs.docker.com/registry/deploying/
 
1- Docker rm:
docker rm is used to remove the docker containers, for this to work we need to list all the containers that are created in our docker system:
to do this use:

docker ps -a |cut -d" " -f1|tail -n +2

then to remove all the containers run this one line script:

docker rm `docker ps -a |cut -d" " -f1|tail -n +2`

This will remove all the containers :)

2- Docker rmi:
This command is used to remove docker images, i am using this to get red of the temp and unused ones to save disk space:

[root@fingolfin stock_apache_docker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
                            508a8098b89d        17 minutes ago      282.3 MB
                            68cb22da5789        41 minutes ago      282.3 MB
                            fa8600453d4f        8 hours ago         282.3 MB
                            1324b4fad9fc        8 hours ago         282.3 MB
                            eb9d45dd81cd        8 hours ago         282.3 MB
                            cbf901a1a88e        8 hours ago         282.3 MB
                            b16a988c6668        8 hours ago         282.3 MB
                            e2ac7f0940f9        8 hours ago         282.3 MB
                            e74dccbf3e7d        24 hours ago        310.8 MB
                            b8cae09ad3bf        25 hours ago        196.7 MB
docker.io/ubuntu    latest              8444cb1ed763        2 days ago          122 MB
docker.io/centos    centos6             d487f1b804de        12 days ago         194.5 MB
docker.io/centos    latest              8c59c0a396b7        12 days ago         196.7 MB
[root@fingolfin stock_apache_docker]#


then:

[root@fingolfin stock_apache_docker]# docker rmi 68cb22da5789 fa8600453d4f 1324b4fad9fc eb9d45dd81cd cbf901a1a88e b16a988c6668 e2ac7f0940f9 e74dccbf3e7d b8cae09ad3bf
Deleted: 68cb22da57893d493c9a0983cc4892b33241766f2fcbbdfd752d191352bdd751
Deleted: c0fc7de087b97f1c75bc189785b9861999fafa7aef05aeb5ed90b23a5bd387d2
Deleted: fa8600453d4f95758d739cf017cec8f6ee4f50b27d4c3430f1859f414aed04fc
 . . . 
 . . . 


3- Docker run:
This is a sample command to run an apache container and map container port 80 to the host port 9090.
Also we use volumes feature of docker to mount host volumes on the container for the apache htdocs /var/www/http and logs at /var/log/httpd:

command:

docker run -p 9090:80 -v /root/docker_stage/stock_apache_docker/html:/var/www/html  -v /root/docker_stage/stock_apache_docker/logs:/var/log/httpd 68cb22da5789

you can put any content on the host folder and docker apache will pick it up. also you can collect the apache logs from the host folder with a simple shell script !

Another form of docker run is to create a shell and actually execute commands on the docker container affecting the image directly, this needs to allocate a terminal -t and be interactive -i as below:

[root@fingolfin logs]# docker run -t -i centos:centos6 /bin/bash
Usage of loopback devices is strongly discouraged for production use. Either use `--storage-opt dm.thinpooldev` or use `--storage-opt dm.no_warn_on_loop_devices=true` to suppress this warning.
[root@204005540b83 /]# hostname
204005540b83
[root@204005540b83 /]# cat /etc/redhat-release
CentOS release 6.7 (Final)
[root@204005540b83 /]# exit
[root@fingolfin logs]#

4- Docker build:
Docker build will process a prefined docker file and execute the steps in it one after the other using containers resulting from set n-1 to do step n.
This makes it easy to create custom containers as needed.
below is a simple dockerfile for apache:

[root@fingolfin stock_apache_docker]# cat dockerfile
FROM centos:centos6

RUN yum -y --nogpgcheck install httpd

VOLUME /var/www/html
VOLUME /var/log/httpd


EXPOSE 80

ENTRYPOINT /usr/sbin/httpd -D FOREGROUND
[root@fingolfin stock_apache_docker]#

 
Note that in the dockerfile we need to suppress all user interaction and also make sure that the entrypoint and or CMD commands are running in foreground, if they run as a service something else would need to block the container from exiting, say using a tail -f on a none rotating log . .

Also note the difference between RUN and ENTRYPOINT/CMD, run will execute its command in build time and its action will take effect at build time, in this case its a yum install, it could also be an mkdir or running any setup script that affects the image.

ENTRYPOINT/CMD will not run at build time, instead, it will run when a container is initialized from  the image, say when we use a docker run command. ENTRYPOINT should only appear once and thus most of the time holds the purpose of the image, in our case running apache.
CMD can appear multiple times to run commands and provide arguments to the entry point command.

It’s always better to use 1 container per piece of software, makes things much easier.


No comments:

Post a Comment