Crear contenedores con Docker

docker

 

Primero hay que instalar Docker, el cual se puede realizar de varias maneras:

Descargandolo desde el sitio oficial de docker.com

Lo primero que haremos será instalar curl, apt-get install curl

Después descargamos el script de instalación de Docker, curl -sSL https://get.docker.com/ | sh

En este caso se estara realizando la instalacion desde un sistema operativo de Linux Debian, para ello se utiliza aptitude

# aptitude install docker-engine

Una vez instalado podemos comprobar su funcionamiento haciendo uso de los comandos propios:

# docker

Usage: docker COMMAND

A self-sufficient runtime for containers

Options:
–config string Location of client config files (default «/root/.docker»)
-D, –debug Enable debug mode
–help Print usage
-H, –host list Daemon socket(s) to connect to
-l, –log-level string Set the logging level («debug»|»info»|»warn»|»error»|»fatal») (default «info»)
–tls Use TLS; implied by –tlsverify
–tlscacert string Trust certs signed only by this CA (default «/root/.docker/ca.pem»)
–tlscert string Path to TLS certificate file (default «/root/.docker/cert.pem»)
–tlskey string Path to TLS key file (default «/root/.docker/key.pem»)
–tlsverify Use TLS and verify the remote
-v, –version Print version information and quit

Management Commands:
container Manage containers
image Manage images
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage Swarm
system Manage Docker
volume Manage volumes

Commands:
attach Attach local standard input, output, and error streams to a running container
build Build an image from a Dockerfile
commit Create a new image from a container’s changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container’s filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container’s filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes

Run ‘docker COMMAND –help’ for more information on a command.

Una vez instalado Docker, se puede hacer busqueda para instalar una aplicacion o un sistema operativo

# docker search debian
 NAME DESCRIPTION STARS OFFICIAL AUTOMATED
 ubuntu Ubuntu is a Debian-based Linux operating s... 7014 [OK]
 debian Debian is a Linux distribution that's comp... 2377 [OK]
 google/debian 52 [OK]
 armhf/debian Debian is a Linux distribution that's comp... 29
 arm32v7/debian Debian is a Linux distribution that's comp... 23
 itscaro/debian-ssh debian:jessie 20 [OK]
 samueldebruyn/debian-git a minimal docker container with debian and... 16 [OK]
 resin/armv7hf-debian Debian is a Linux distro composed entirely... 15
 arm64v8/debian Debian is a Linux distribution that's comp... 8
 eboraas/debian Debian base images, for all currently-avai... 8 [OK]
 i386/debian Debian is a Linux distribution that's comp... 7
 vergissberlin/debian-development Docker debian image to use for development... 4 [OK]
 rockyluke/debian Docker images of Debian. 4
 vicamo/debian Debian docker images for all versions/arch... 3
 frekele/debian docker run --rm --name debian frekele/debian 3 [OK]
 s390x/debian Debian is a Linux distribution that's comp... 2
 ppc64le/debian Debian is a Linux distribution that's comp... 2
 dockershelf/debian Repository for docker images of Debian. Te... 1 [OK]
 vpgrp/debian Docker images of Debian. 1
 smartentry/debian debian with smartentry 1 [OK]
 igneoussystems/base-debian-client Base image for debian clients 0
 konstruktoid/debian Debian base image 0 [OK]
 jdub/debian-sources-resource Concourse CI resource to check for updated... 0 [OK]
 trollin/debian 0
 casept/debian-amd64 A debian image built from scratch. Mostly ... 0

Una vez identificado la imagen del repositorio que se desea instalar, se procede a realizar la instalación con el comando:

# docker pull debian
Using default tag: latest
latest: Pulling from library/debian
723254a2c089: Pull complete 
Digest: sha256:0a5fcee6f52d5170f557ee2447d7a10a5bdcf715dd7f0250be0b678c556a501b
Status: Downloaded newer image for debian:latest

Para ejecutar  el sistema operativo o aplicacion:

#docker run -i -t --name apache2 -h apache2 -p 80:80 debian /bin/bash
argumentos:
-i -> interactive
 -t -> consola tty
 --name -> nombre de la imagen, es éste caso apache2
 -h -> nombre del host, en éste caso apache2
 -p 80:80 -> Puertos de interconexión entre el container (puerto interior) y la maquina anfitrion (puerto exterior). 
             Le dice a Docker que mapee los puertos que el contenedor este exponiendo, en éste caso 80, hacia el S.O huesped en el puerto 80 
 ubuntu:15.10 -> nombre de la imagen y versión (tag), en éste caso ubuntu versión 15.10
 /bin/bash -> comando a ejecutar cuando inicie la imagen, en éste caso la consola bash

 Conectarse a un contenedor iniciado

Verificar cuales son las instancias que se estan ejecutando:

#docker ps -a

Luego se selecciona por el nombre:

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                           PORTS                            NAMES
2acd9b71fbbe        debian              «/bin/bash»         14 minutes ago      Up 13 minutes                    0.0.0.0:8083->8083/tcp           srv05
e169ffddaf18        httpd               «/bin/bash»         18 minutes ago      Up 18 minutes                    80/tcp, 0.0.0.0:8082->8082/tcp   SRV01

En este caso se selecciona la primera instancia:

docker exec -i -t 665b4a1e17b6 /bin/bash