Skip to content

Instantly share code, notes, and snippets.

@jaso514
Created January 9, 2019 02:32
Show Gist options
  • Save jaso514/375cd28620c1fe9920ef6d623267cb2d to your computer and use it in GitHub Desktop.
Save jaso514/375cd28620c1fe9920ef6d623267cb2d to your computer and use it in GitHub Desktop.
Annotations Docker
https://docs.docker.com/get-started/
Instalación:
https://docs.docker.com/toolbox/toolbox_install_windows/#step-2-install-docker-toolbox
Entrar a docker quickstart
Para listar los containers:
docker image ls --all
Para correr un container
docker run <container>
Para correr un container y entrar al bash
docker run -it ubuntu bash
Ver informaición de docker (cantidad de containers creados, activos, apagados, etc)
Se pueden crear archivos Dockerfile con especificaciones de que lenguajes y otras configuraciones se quieren tener.
De este modo se pueden tener N container con diferentes configuraciones y poder reutilizar y distribuir contenedores y apps.
Para esto se crea el archivo Dockerfile.
Luego se construye el contenedor: docker build -t friendlyhello .
Se corre la app: docker run -p 4000:80 friendlyhello
Para ingresar a una app web se puede mapear el puerto del host con el del contenedor con el -p <puerto_host>:<puerto_contenedor>
Se puede ingresar por localhost:4000, si no funciona buscar la ip del contenedor con docker-machine ip y luego acceder con esa IP y puerto.
Para ingresar a la consola del contenedor ejecutar:
docker exec -it <id_contenedor> bash
Si se quiere iniciar con una carpeta compartida se corre el siguiente comando:
docker run -v /host/directory:/container/directory -other -options image_name command_to_run
Ej:
docker run -v /c/Users/Jhonny/Documents/vm_share/mongoDB/shared:/data/code -t -i -p 27019:27017 ubuntu_pymongo
Para conectarse al contenedor se debe acceder a la ip:
192.168.99.100
El puerto de conexión es el primer puerto indicado (27019)
Hay que asegurarse de que el contenedor ejecute el bash, esto se puede hacer agregando:
ENTRYPOINT ["/bin/bash"]
Compartir imagenes
Se debe tener cuenta en docker cloud (cierra el 20 de mayo), o alguna otra (ver:https://docs.docker.com/docker-cloud/migration/).
Iniciar sesion desde el terminal:
docker login
Crear imagen nueva:
docker tag <image> <username>/<repository>:<tag>
Subir imagen:
docker push <username>/<repository>:<tag>
En caso de tener muchos contenedores siendo usados y estos ocupan mucho espacio de docker, se deben eliminar con container rm
Resumen de comandos parte 2:
docker build -t friendlyhello . # Create image using this directory's Dockerfile
docker run -p 4000:80 friendlyhello # Run "friendlyname" mapping port 4000 to 80
docker run -d -p 4000:80 friendlyhello # Same thing, but in detached mode
docker container ls # List all running containers
docker container ls -a # List all containers, even those not running
docker container stop <hash> # Gracefully stop the specified container
docker container kill <hash> # Force shutdown of the specified container
docker container rm <hash> # Remove specified container from this machine
docker container rm $(docker container ls -a -q) # Remove all containers
docker image ls -a # List all images on this machine
docker image rm <image id> # Remove specified image from this machine
docker image rm $(docker image ls -a -q) # Remove all images from this machine
docker rmi -f $(docker images -q) # force remove all imageshsbc
docker login # Log in this CLI session using your Docker credentials
docker tag <image> username/repository:tag # Tag <image> for upload to registry
docker push username/repository:tag # Upload tagged image to registry
docker run username/repository:tag # Run image from a registry
docker stack ls # List stacks or apps
docker stack deploy -c <composefile> <appname> # Run the specified Compose file
docker service ls # List running services associated with an app
docker service ps <service> # List tasks associated with an app
docker inspect <task or container> # Inspect task or container
docker container ls -q # List container IDs
docker stack rm <appname> # Tear down an application
docker swarm leave --force # Take down a single node swarm from the manager
Docker Compose
https://docs.docker.com/compose/reference/build/
docker-compose build # crea la imagen
docker-compose.exe build --force-rm # crea la imagen
docker-compose up --build # crea la imagen si no existe, luego levanta el contenedor
docker-compose up # levanta el contenedor
docker-compose restart # reinicia el contenedor
docker-compose exec web_dev bash
IF
$ docker.exe run -it --rm ubuntu:14.04 /bin/bash
the input device is not a TTY. If you are using mintty, try prefixing the command with 'winpty'
THEN
$ winpty docker.exe run -it --rm ubuntu:14.04 /bin/bash
swarm
Docker Swarm es la aplicación nativa de Docker para gestionar clusters.
Se pueden crear varias maquinas virtuales para que alverguen a los distintos host, los cuales se conectaran entre si.
Docker tiene ya la logica para monitorear los cluster y reiniciar los que se caigan.
Con el comando:
docker-machine create --driver virtualbox myvmX
se crea una maquina virtual en windows 10 home, la primera que se cree será el manager. El resto son workers.
El manager ejecuta los comandos y autentica los workers.
docker-machine create --driver virtualbox myvm1 # Create a VM (Mac, Win7, Linux)
docker-machine create -d hyperv --hyperv-virtual-switch "myswitch" myvm1 # Win10
docker-machine env myvm1 # View basic information about your node
docker-machine ssh myvm1 "docker node ls" # List the nodes in your swarm
docker-machine ssh myvm1 "docker node inspect <node ID>" # Inspect a node
docker-machine ssh myvm1 "docker swarm join-token -q worker" # View join token
docker-machine ssh myvm1 # Open an SSH session with the VM; type "exit" to end
docker node ls # View nodes in swarm (while logged on to manager)
docker-machine ssh myvm2 "docker swarm leave" # Make the worker leave the swarm
docker-machine ssh myvm1 "docker swarm leave -f" # Make master leave, kill swarm
docker-machine ls # list VMs, asterisk shows which VM this shell is talking to
docker-machine start myvm1 # Start a VM that is currently not running
docker-machine env myvm1 # show environment variables and command for myvm1
eval $(docker-machine env myvm1) # Mac command to connect shell to myvm1
& "C:\Program Files\Docker\Docker\Resources\bin\docker-machine.exe" env myvm1 | Invoke-Expression # Windows command to connect shell to myvm1
docker stack deploy -c <file> <app> # Deploy an app; command shell must be set to talk to manager (myvm1), uses local Compose file
docker-machine scp docker-compose.yml myvm1:~ # Copy file to node's home dir (only required if you use ssh to connect to manager and deploy the app)
docker-machine ssh myvm1 "docker stack deploy -c <file> <app>" # Deploy an app using ssh (you must have first copied the Compose file to myvm1)
eval $(docker-machine env -u) # Disconnect shell from VMs, use native docker
docker-machine stop $(docker-machine ls -q) # Stop all running VMs
docker-machine rm $(docker-machine ls -q) # Delete all VMs and their disk images
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment