|

Contenedores Docker

El repositorio de contenedores para descargar esta en https://hub.docker.com

Cada repositorio tiene diferentes contenedores.Cada contenedor se etiqueta(tag) para poder diferenciarlos.

Para descargar Ubuntu 14.04

$ sudo docker pull ubuntu:trusty
trusty: Pulling from library/ubuntu
324d088ce065: Pull complete
2ab951b6c615: Pull complete
9b01635313e2: Pull complete
04510b914a6c: Pull complete
83ab617df7b4: Pull complete
Digest: sha256:b8855dc848e2622653ab557d1ce2f4c34218a9380cceaa51ced85c5f3c8eb201
Status: Downloaded newer image for ubuntu:trusty

Si no ponemos etiqueta entonces por defecto se descarga el contenedor con etiqueta «latest».
Vamos a ver las imagenes

$ sudo docker images
[sudo] password for jon:
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              trusty              8cef1fa16c77        2 weeks ago         223MB
hello-world         latest              e38bc07ac18e        5 weeks ago         1.85kB
busybox             latest              8ac48589692a        6 weeks ago         1.15MB

Para borrar imagenes

$ sudo docker rmi 8ac48589692a
Error response from daemon: conflict: unable to delete 8ac48589692a (must be forced) - image is being used by stopped container a9ad22be49f3
$

No deja borrarlo porque tiene asociado un contenedor.

Para ver los contenedores que tenemos creados

$ sudo docker ps -a
185d8d93b1cd        ubuntu              "/bin/bash"              2 hours ago         Exited (0) 2 hours ago                        naughty_lichterman
a9ad22be49f3        busybox             "sh"                     21 hours ago        Exited (0) 21 hours ago                       reverent_lichterman
7487aec705bb        hello-world         "/hello"                 21 hours ago        Exited (0) 21 hours ago                       clever_tereshkova

Para borrar contenedores

$ sudo docker rm a9ad22be49f3
a9ad22be49f3
$ sudo docker rmi 8ac48589692a
Untagged: busybox:latest
Untagged: busybox@sha256:58ac43b2cc92c687a32c8be6278e50a063579655fe3090125dcb2af0ff9e1a64
Deleted: sha256:8ac48589692a53a9b8c2d1ceaa6b402665aa7fe667ba51ccc03002300856d8c7
Deleted: sha256:0314be9edf00a925d59f9b88c9d8ccb34447ab677078874d8c14e7a6816e21e1
$
Vamos a crear nuestro primer contenedor interactivo :
$ sudo docker run -it ubuntu

Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
a48c500ed24e: Pull complete
1e1de00ff7e1: Pull complete
0330ca45a200: Pull complete
471db38bcfbf: Pull complete
0b4aba487617: Pull complete
Digest: sha256:c8c275751219dadad8fa56b3ac41ca6cb22219ff117ca98fe82b42f24e1ba64e
Status: Downloaded newer image for ubuntu:latest
root@185d8d93b1cd:/#

Nos cambia el prompt y lo identifica con el ID del contenedor.

$ sudo docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                    PORTS               NAMES
185d8d93b1cd        ubuntu              "/bin/bash"         2 minutes ago       Up 2 minutes                                  naughty_lichterman

7487aec705bb        hello-world         "/hello"            19 hours ago        Exited (0) 19 hours ago                       clever_tereshkova

Probamos a instalar curl.

root@185d8d93b1cd:/# apt-get update
root@185d8d93b1cd:/# apt-get install curl
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
ca-certificates krb5-locales libasn1-8-heimdal libcurl4 libgssapi-krb5-2 libgssapi3-heimdal libhcrypto4-heimdal libheimbase1-heimdal libheimntlm0-heimdal libhx509-5-heimdal libk5crypto3
libkeyutils1 libkrb5-26-heimdal libkrb5-3 libkrb5support0 libldap-2.4-2 libldap-common libnghttp2-14 libpsl5 libroken18-heimdal librtmp1 libsasl2-2 libsasl2-modules libsasl2-modules-db
libsqlite3-0 libssl1.1 libwind0-heimdal openssl publicsuffix
Suggested packages:
krb5-doc krb5-user libsasl2-modules-gssapi-mit | libsasl2-modules-gssapi-heimdal libsasl2-modules-ldap libsasl2-modules-otp libsasl2-modules-sql
The following NEW packages will be installed:
ca-certificates curl krb5-locales libasn1-8-heimdal libcurl4 libgssapi-krb5-2 libgssapi3-heimdal libhcrypto4-heimdal libheimbase1-heimdal libheimntlm0-heimdal libhx509-5-heimdal
libk5crypto3 libkeyutils1 libkrb5-26-heimdal libkrb5-3 libkrb5support0 libldap-2.4-2 libldap-common libnghttp2-14 libpsl5 libroken18-heimdal librtmp1 libsasl2-2 libsasl2-modules
libsasl2-modules-db libsqlite3-0 libssl1.1 libwind0-heimdal openssl publicsuffix
0 upgraded, 30 newly installed, 0 to remove and 0 not upgraded.
Need to get 4580 kB of archives.
After this operation, 14.1 MB of additional disk space will be used.

Do you want to continue? [Y/n]

Salimos del contenedor.

root@185d8d93b1cd:/# exit
exit
$

Para parar un contenedor :

$ sudo docker stop 185d8d93b1cd
 
Para volver a entrar en el contenedor.
$ sudo docker start -i 185d8d93b1cds
root@185d8d93b1cd:/#

Para ejecutar contenedores en background.
-d, –detach

$ sudo docker run -d nginx

Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
f2aa67a397c4: Pull complete
3c091c23e29d: Pull complete
4a99993b8636: Pull complete
Digest: sha256:0fb320e2a1b1620b4905facb3447e3d84ad36da0b2c8aa8fe3a5a81d1187b884
Status: Downloaded newer image for nginx:latest
962392723bf524cfe0d17ecc6b9f5d3ca72683e500b5406d6292e6e466c93f69
$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
962392723bf5        nginx               "nginx -g 'daemon of…"   2 minutes ago       Up 2 minutes        80/tcp              brave_ramanujan

Creamos un contenedor con un nombre especifico.

$ sudo docker run -it --name contenedor_ubuntu ubuntu
root@29d208c2f162:/#
$ sudo docker ps
29d208c2f162        ubuntu              "bash"                   8 minutes ago       Up 8 minutes                            contenedor_ubuntu

Para poder ejecutar comandos en el contenedor.

$ sudo docker exec -it contenedor_ubuntu date
Fri May 18 14:40:14 UTC 2018

Para aglutinar todos los comandos relacionados con imagenes tenemos el comando «docker image».

$ sudo docker image --help

Usage:  docker image COMMAND

Manage images

Options:

Commands:
  build       Build an image from a Dockerfile
  history     Show the history of an image
  import      Import the contents from a tarball to create a filesystem image
  inspect     Display detailed information on one or more images
  load        Load an image from a tar archive or STDIN
  ls          List images
  prune       Remove unused images
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rm          Remove one or more images
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

Para aglutinar todos los comandos relacionados con contenedores tenemos el comando docker container.

$ sudo docker container --help

Usage:  docker container COMMAND

Manage containers

Options:

Commands:
  attach      Attach local standard input, output, and error streams to a running container
  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
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive
  inspect     Display detailed information on one or more containers
  kill        Kill one or more running containers
  logs        Fetch the logs of a container
  ls          List containers
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  prune       Remove all stopped containers
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  run         Run a command in a new container
  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
  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
  wait        Block until one or more containers stop, then print their exit codes

Para ver lo que esta mostrando un contenedor en background.

$ sudo docker logs contenedor_ubuntu --tail 10 

Para matar un contenedor en background.

$ sudo docker kill contenedor_ubuntu

Para ver los recursos que esta consumiendo un contendor.

$ sudo docker top contenedor_ubuntu

o tambien podemos ultilizar

$ sudo docker stats contenedor_ubuntu

Para ver las propiedades y características de contenedores e imágenes en formato JSON.

$ sudo docker inspect contenedor_ubuntu

Ejecutar un bash dentro de un contenedor de ubnutu1.

$ docker exec -it ubuntu1 bash

Publicaciones Similares