Usage: docker container COMMAND Manage containers
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 Execute 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 (3 prim digitos del ID + ETC)
run Create and run a new container from an image
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
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Aliases:
docker image ls, docker image list, docker images
Options:
-a, --all Show all images (default hides intermediate images)
--digests Show digests
-f, --filter filter Filter output based on conditions provided
--format string Format output using a custom template:
'table': Print output in table format with column headers (default)
'table TEMPLATE': Print output in table format using the given Go template
'json': Print in JSON format
'TEMPLATE': Print output using the given
Go template.
Refer to https://docs.docker.com/go/formatting/ for more information about formatting
output with templates
--no-trunc Don't truncate output
-q, --quiet Only show image IDs
C:\Users\Familia Paez>docker image --help
Usage: docker image COMMAND
Manage images
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 Download an image from a registry
push Upload an image 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
Run 'docker image COMMAND --help' for more information on a command.
docker compose –help
definir y gestionar aplicaciones Docker multi-contenedor…define servicios,
dependencias, orquestación de contenedores, configure la reutilización,
administración de redes y volúmenes.
Usage: docker compose [OPTIONS] COMMAND
Define and run multi-container applications with Docker.
Options:
--ansi string Control when to print ANSI control
characters ("never"|"always"|"auto")
(default "auto")
--compatibility Run compose in backward compatibility mode
--dry-run Execute command in dry run mode
--env-file stringArray Specify an alternate environment file.
-f, --file stringArray Compose configuration files
--parallel int Control max parallelism, -1 for
unlimited (default -1)
--profile stringArray Specify a profile to enable
--progress string Set type of progress output (auto,
tty, plain, quiet) (default "auto")
--project-directory string Specify an alternate working directory
(default: the path of the, first
specified, Compose file)
-p, --project-name string Project name
Commands:
build Build or rebuild services
config Parse, resolve and render compose file in canonical format
cp Copy files/folders between a service container and the local filesystem
create Creates containers for a service.
down Stop and remove containers, networks
events Receive real time events from containers.
exec Execute a command in a running container.
images List images used by the created containers
kill Force stop service containers.
logs View output from containers
ls List running compose projects
pause Pause services
port Print the public port for a port binding.
ps List containers
pull Pull service images
push Push service images
restart Restart service containers
rm Removes stopped service containers
run Run a one-off command on a service.
start Start services
stop Stop services
top Display the running processes
unpause Unpause services
up Create and start containers
version Show the Docker Compose version information
---------------- DOCKER postgres y PGADMIN------------------
-POSTGRES “-dp mapeo el Puerto 5432 de mi pc con el puerto 5432 del contenedor”
docker run --name some-postgres -dp 5432:5432 -e POSTGRES_PASSWORD=mypass postgres
-POSTGRES
docker container run^
-d^
--name postgres-db^
-e POSTGRES_PASSWORD=123456^
-v postgres-db:/var/lib/postgresql/data^
postgres:15.1
-PG ADMIN
docker container run ^
--name pgAdmin ^
-e PGADMIN_DEFAULT_PASSWORD=123456 ^
-e [email protected] ^
-dp 8080:80 ^
dpage/pgadmin4:6.17
-Creat Network
docker network ALGO PARA CREAR postgres-net
-UNION
docker network connect postgres-net ID del contenedor 1
docker network connect postgres-net ID del contenedor 2
-MARIADB (creo una DB y una volume para persistencia de datos)
A)docker pull mariadb:jammy (es la version jammy)
B)docker volume create world-db
C)
docker container run^
-dp 3307:3306^
--name world-db^
-e MARIADB_USER=example-user^
-e MARIADB_PASSWORD=user-password^
-e MARIADB_ROOT_PASSWORD=root-secret-password^
-e MARIADB_DATABASE=world-db^
--volume world-db:/var/lib/mysql^
--network world-app^
mariadb:jammy
-PHPMYAPACHE
docker container run --name phpmyadmin -d -e PMA_ARBITRARY=1 -p 8080:80 --network
world-app phpmyadmin:5.2.0-apache
-BIND VOLUME
crea un enlace entre una ubicación en el sistema de archivos del host y una ubicación en el
sistema de archivos del contenedor, lo que permite que los datos se compartan y sincronicen
entre el host y el contenedor.
docker container run ^
--name nest-app ^
-w /app ^
-p 80:3000 ^
-v "$(pwd)":/app ^
node:16-alpine3.16 ^
sh -c "yarn install && yarn start:dev"
BUILT Image (creo imagen usando dockerfile)
docker build –t klerith/cron-ticker:tigre
CONSTRUCCION multiples PLATAFORMAS
docker build --platform Linux/amd64,Linux/arm64,Linux/arm/v7 ^
-t klerith/cron-ticker:latest –push .
SUBIR IMAGEN a Dockerhub
docker image tag klerith/cron-ticket (por defecto es el latest)
docker image tag klerith/cron-ticket: pantera
BAJAR IMAGEN a Dockerhub
docker pull klerith/cron-ticket (por defecto es el latest)
docker pull klerith/cron-ticket: pantera
Plataforma de construccion (Arquitectura)
FROM --platform=Linux/amd64 node:18-alpine
FROM --platform=linux/amd64 node:19.2-alpine3.16
FROM --platform=$BUILDPLATFORM node:19.2-alpine3.16 (utilize el buildx que cree)
Multi-Stage Build
Las creaciones (dependencias/test/build/run), divide el dockerfile en leyer/images-> “STAGES”.
Si cambia algo en alguno de los bloques. Ahí se ejecuta nuevamente ese bloque
COMO SE VE SIN Multi-Stage Build
#CREACION IMAGEN
# docker build -t klerith/cron-ticker:mapache
# /app/usr/lib tiene una carpeta donde se guarda las info
# FROM node:19.2-alpine3.16
# FROM --platform=linux/amd64 node:19.2-alpine3.16
FROM --platform=$BUILDPLATFORM node:19.2-alpine3.16 (con buildx no se usa esta linea)
# cd app "me muevo de directorio a app"
WORKDIR /app
#copio de mi proyecto los archivos app y mis package.json
#COPY -archivos a copiar- | -ubicacion-
COPY app.js package.json ./
#instalo las DEP, puedo agregar mas -> && npm install
RUN npm install
# compio todos los archivos y lo pega en el WorkingDirctory
COPY . .
#Realizar el testing
RUN npm run test
#Eliminar archivos no necesarios en produccion
RUN rm -rf tests && rm -rf node_modules
#Instalo unicamente las DEP de produccion --
RUN npm install --prod
# Comado run de la imagen -npm run/start-
CMD [ "node","app.js" ]
#EN TERMINAL creo la imagen (docker build --tag cron-ticker .)
# . por que se ejecuta en la ubicacion donde esta el 'Dockerfile'
COMO SE VE con Multi-Stage Build
Creo mi BUILDX y luego indico que lo voy a usar
docker buildx créate –name mybuilder –driver docker-container –bootstrap
docker buildx use mybuilder
docker build ls
#STAGE dependencias de desarollo "me muevo de directorio a app"
FROM node:19.2-alpine3.16 as deps
WORKDIR /app
COPY app.js package.json ./
RUN npm install
#STAGE builder o test (al final puede ir npm build)
FROM node:19.2-alpine3.16 as builder
WORKDIR /app
#puedo hacer referencia a una etapa anterior "copio dependencias"
# RUN npm install -> cambio por...(que modulo|ubicacion de mi Stage|destino)
COPY --from=deps /app/node_modules ./node_modules
COPY . .
#Realizar el testing
RUN npm run test
#STAGE Dependencias de produccion
FROM node:19.2-alpine3.16 as prod-deps
WORKDIR /app
COPY package.json ./
RUN npm install --prod
# STAGE Ejecutar la APP
FROM node:19.2-alpine3.16 as runner
WORKDIR /app
COPY --from=prod-deps /app/node_modules ./node_modules
COPY app.js ./
# copio de tasks | pego en ./tasks
COPY tasks/ ./tasks
CMD [ "node","app.js" ]
DOCKER COMPOSE
Creo archivo “docker-compose.yml”
version: '3'
services:
#Config de la DB
db:
container_name: ${MONGO_DB_NAME}
image: mongo:6.0
volumes:
- poke-vol:/data/db
# ports:
# - 27017:27017
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: ${MONGO_USERNAME}
MONGO_INITDB_ROOT_PASSWORD: ${MONGO_PASSWORD}
command: ['--auth']
#Forma de accedes a la DB
mongo-express:
depends_on:
- db
image: mongo-express:1.0.0-alpha.4
environment:
- ME_CONFIG_MONGODB_ADMINUSERNAME=${MONGO_USERNAME}
- ME_CONFIG_MONGODB_ADMINPASSWORD=${MONGO_PASSWORD}
- ME_CONFIG_MONGODB_SERVER=${MONGO_DB_NAME}
ports:
- 8080:8081
restart: always
#Montando nuestra aplicacion que esta basada en una imagen/version especifica
#de nuestra aplicacion(si el Back necesite variar de version se modif la imagen)
poke-app:
depends_on:
- db
- mongo-express
image: klerith/pokemon-nest-app:1.0.0
ports:
- 3000:3000
environment:
MONGODB: mongodb://${MONGO_USERNAME}:${MONGO_PASSWORD}@$
{MONGO_USERNAME}:27017
DB_NAME: ${MONGO_USERNAME}
#1 MONGO_USERNAME es el nombre del contenedor tanto como del servidor
#que es distinto a un nombre de un servicio
restart: always
volumes:
poke-vol:
# true: busca en el hambiente externo (un volume que yo cree)
# false: crea automaticamente
external: false
#CONEXION URL en TablePlus
#URL:mongodb://strider:123456789@localhost:27017
.env
MONGO_USERNAME=strider
MONGO_PASSWORD= 123456789
MONGO_DB_NAME=pokemonDB
docker compose up –d (crea la red|contenedor|volumen)
docker compose down (da de baja la red|contenedor|volumen)
docker compose build (build de produccion)
Otro Ejemplo
Docker-compose.yml
version: '3'
services:
app:
build:
context: .
target: dev
dockerfile: Dockerfile
#se puede utilizar esta comando aqui A
command: yarn start:dev
volumes:
#la primera es mi bind volume
- .:/app/
- /app/node_modules
#la segunda es por si no existe/hay problema con la primera
container_name: nest-app
ports:
- ${PORT}:${PORT}
environment:
APP_VERSION: ${APP_VERSION}
STAGE: ${STAGE}
DB_PASSWORD: ${DB_PASSWORD}
DB_NAME: ${DB_NAME}
DB_HOST: ${DB_HOST}
DB_PORT: ${DB_PORT}
DB_USERNAME: ${DB_USERNAME}
PORT: ${PORT}
HOST_API: ${HOST_API}
JWT_SECRET: ${JWT_SECRET}
db:
image: postgres:14.3
restart: always
ports:
- "5432:5432"
environment:
POSTGRES_PASSWORD: ${DB_PASSWORD}
POSTGRES_DB: ${DB_NAME}
container_name: ${DB_NAME}
volumes:
- postgres-db:/var/lib/postgresql/data
volumes:
postgres-db:
external: false
docker-compose-prod.yml
version: '3'
#Elimino lo que no necesito para levantar en produccion
services:
app:
build:
context: .
target: ${STAGE} # STAGE cambio en .env | dev -> prod |
dockerfile: Dockerfile
#image da nombre a los layers/tags de las image
image: klerith/teslo-shop-backend
container_name: nest-app
ports:
- ${PORT}:${PORT}
environment:
APP_VERSION: ${APP_VERSION}
STAGE: ${STAGE}
DB_PASSWORD: ${DB_PASSWORD}
DB_NAME: ${DB_NAME}
DB_HOST: ${DB_HOST}
DB_PORT: ${DB_PORT}
DB_USERNAME: ${DB_USERNAME}
PORT: ${PORT}
HOST_API: ${HOST_API}
JWT_SECRET: ${JWT_SECRET}
db:
image: postgres:14.3
restart: always
ports:
- "5432:5432"
environment:
POSTGRES_PASSWORD: ${DB_PASSWORD}
POSTGRES_DB: ${DB_NAME}
container_name: ${DB_NAME}
volumes:
- postgres-db:/var/lib/postgresql/data
volumes:
postgres-db:
external: false
VARIABLES DE ENTORNO (.env)
APP_VERSION=1.0.1
STAGE=dev
DB_PASSWORD=MySecr3tPassWord@as2
DB_NAME=TesloDB
DB_HOST=TesloDB
DB_PORT=5432
DB_USERNAME=postgres
PORT=3000
HOST_API=http://localhost:3000/api
JWT_SECRET=Est3EsMISE3Dsecreto32s