Monday, January 25, 2016

Working with Docker Containers


The search command allows you to search for Docker images in Docker registry, lets search images related to WordPress.

# docker search wordpress

The pull command allows you download docker images from registry. By default, it downloads from Docker public registry, also you can download images from your own registry.

# docker pull centos

List the available docker images on the system.

# docker images

You can remove downloaded images using rmi command, below command removes ubuntu image from the local system.

# docker rmi ubuntu

The following command is widely used to create a containers, uses the “centos” docker image to create a container.

# docker run -dit --name docker-centos --hostname="centos" centos /bin/bash

-d = Running a docker container in the background
-i = Running a docker container in interactive mode.
-t = Allocates tty terminal wich is required to attach to the containers.
–name = Name of a docker container
–hostname = Set a host to container

Check the running containers using ps command.

# docker ps -a
CONTAINER ID    IMAGE         COMMAND      CREATED  STATUS    PORTS      NAMES
1f99133e0387 centos:latest "/bin/bash" About a minute ago Up About a minute docker-centos

The attach command lets you to attach to running container (docker-centos), you can see the hostname is set to centos, also run some test commands like “df -h” to see the mount points details.

[root@server ~]# docker attach docker-centos

[root@centos /]# df -h
Filesystem  Size  Used Avail Use% Mounted on
/dev/mapper/docker-253:1-369-14d43fd37613411218528b599dd1c39a7b19ae2041a26a2cb170f52e8bc591e8  9.8G  254M  9.0G   3% /
tmpfs       490M     0  490M   0% /dev
shm         64M     0   64M   0% /dev/shm
/dev/mapper/fedora--server-root  50G  1.9G   45G   4% /etc/hosts
tmpfs       490M     0  490M   0% /proc/kcore

The docker run command allows you to run a command in a container. For example, let’s get an information of mount points with in a container.
–rm = removes the container when the process exits.

# docker run --rm  centos /usr/bin/df -h
Filesystem  Size  Used Avail Use% Mounted on
/dev/mapper/docker-253:1-369-8c3daee9969e4096047fa7b6802cccefe2b78ac176347d5b0feb9d4df4156c6d  9.8G  254M  9.0G   3% /
tmpfs       490M     0  490M   0% /dev
shm         64M     0   64M   0% /dev/shm
/dev/mapper/fedora--server-root   50G  1.9G   45G   4% /etc/hosts
tmpfs       490M     0  490M   0% /proc/kcore

The top command shows running process and their details.

# docker top docker-centos
UID    PID   PPID    C    STIME      TTY           TIME                CMD
root   3442  2121    0    21:44      pts/2         00:00:00           /bin/bash

The stats command does live stream of resource usage statistics, output of this command will look like normal top command.

# docker stats docker-centos

The cp command will help you to copy files/folders from containers to host system, the following command will copy “tobecopied” to /root of host machine.

# docker cp docker-centos:/tobecopied /root/

The kill command sends the SIGTERM to kill a running container.

# docker kill docker-centos

The start command lets you to start a stopped container, lets start the docker-centos.

# docker start docker-centos

The restart command helps you to restart a container.

# docker restart docker-centos

The stop command lets you to gracefully stop a container

# docker stop docker-centos

The rename command allows you to change the name of the container, following command rename 
the docker-centos to MyCentOS.

# docker rename docker-centos MyCentOS

The rm command will allow you to remove a container.

# docker rm MyCentOS

  • Create Docker container



[root@karan-ws ~]# docker create -ti --name="mona" centos bash
c7f9eb6b32eba38242b9d9ced309314f8eee720dbf29c656885aa0cbfff15aa6

  • Start your docker container

# docker start mona
  • Get IP address of your newly created docker container



[root@karan-ws ~]# docker inspect mona | grep -i ipaddress
         "IPAddress": "172.17.0.1",

  • Attach (login) to your docker container

[root@karan-ws ~]# docker attach mona

[root@c7f9eb6b32eb /]# cat /etc/redhat-release
CentOS Linux release 7.1.1503 (Core)

[root@c7f9eb6b32eb /]# df -h
Filesystem     Size   Used  Avail  Use%   Mounted on
/dev/mapper/docker-253:1-16852579-c7f9eb6b32eba38242b9d9ced309314f8eee720dbf29c656885aa0cbfff15aa6  9.8G  268M  9.0G   3% /
tmpfs             1.6G     0  1.6G   0% /dev
shm               64M     0   64M   0% /dev/shm
tmpfs             1.6G     0  1.6G   0% /run
tmpfs             1.6G     0  1.6G   0% /tmp
/dev/vda1       10G 1.6G  8.5G  16% /etc/hosts
tmpfs             1.6G     0  1.6G   0% /run/secrets
tmpfs             1.6G     0  1.6G   0% /proc/kcore



To detach from docker container use ctrl+p+q , avoid using exit command as it will stop container and exit.
  • List container

[root@karan-ws ~]# docker ps
CONTAINER ID        IMAGE        COMMAND       CREATED       STATUS       PORTS       NAMES
c7f9eb6b32eb        centos:latest     "bash"            9 minutes ago   Up 28 seconds                 mona
  • Stop and destroy container


[root@karan-ws ~]# docker stop mona ; docker kill mona
mona
mona
[root@karan-ws ~]# docker ps
CONTAINER ID  IMAGE  COMMAND  CREATED  STATUS  PORTS  NAMES



Install wordpress, mysql and apache on Docker:

$ Docker run –it –name wordpress –h wordpress –p 80:80 –e MYSQL_DB-“mycoolblog.com”  -e APACHE_SVRALIAS=www.mycoolblog.com localhost –e MYSQL_PASS=”passwrod123” –e MYSQL_DB=”mycoolblog” –e APP_USER=”wpadmin” –e APP_PASS=”password456” –e WP_KEY=”mycoolblog is cool” appcontainers/wordpress


route add 172.17.0.0 mask 255.255.0.0 192.168.0.251 –p


Installing DockerUI
Installing DockerUI is pretty easy than installing docker engine. We just need to pull the dockerui from the Docker Registry Hub and run it inside a container. To do so, we'll simply need to run the following command.
# docker run -d -p 9000:9000 --privileged -v /var/run/docker.sock:/var/run/docker.sock dockerui/dockerui
# Open your browser to http://<dockerd host ip>:9000






Bind mounting the Unix socket into the DockerUI container is much more secure than exposing your docker daemon over TCP. The --privileged flag is required for hosts using SELinux. You should still secure your DockerUI instance behind some type of auth. Directions for using Nginx auth are here.

Specify socket to connect to Docker daemon

By default DockerUI connects to the Docker daemon with/var/run/docker.sock. For this to work you need to bind mount the unix socket into the container with -v /var/run/docker.sock:/var/run/docker.sock.
You can use the -e flag to change this socket:
# Connect to a tcp socket:
$ docker run -d -p 9000:9000 --privileged dockerui/dockerui -e http://127.0.0.1:2375

Change address/port DockerUI is served on

DockerUI listens on port 9000 by default. If you run DockerUI inside a container then you can bind the container's internal port to any external address and port:
# Expose DockerUI on 10.20.30.1:80
$ docker run -d -p 10.20.30.1:80:9000 --privileged -v /var/run/docker.sock:/var/run/docker.sock dockerui/dockerui

No comments:

Post a Comment