Después de completar elInstalación de Dockerdebe tener una nueva ventana que lo guiará a través de los primeros pasos para crearimagenesycontenedorescon Docker:
Esta es una forma interesante de ponerte al día con la descarga de tu primera imagen y ejecutarla como contenedor.
Puede ejecutar los comandos en el terminal de la derecha incorporada en esta aplicación, pero prefiero ejecutarlo en mi propio shell.
Abro la Terminal de macOS, ejecutocd dev
para entrar a mi casadev
carpeta, y creo unadocker
subdirectorio, donde alojaré todos los experimentos de Docker. Corrocd docker
para entrar, luego corro
git clone https://github.com/docker/getting-started
Este comando creó un nuevogetting-started
carpeta con el contenido del repositoriohttps://github.com/docker/getting-started:
Ahora desde esta carpeta, ejecute el comandodocker build
De este modo:
docker build -t docker101tutorial .
Esto creará la imagen a partir del contenido de la carpeta actual en la que se encuentra, con el nombre de la etiquetadocker101tutorial
.
Este es el Dockerfile
*# Install the base requirements for the app.*
*# This stage is to support development.*
FROM python:alpine AS base
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
# Run tests to validate app
FROM node:12-alpine AS app-base
WORKDIR /app
COPY app/package.json app/yarn.lock ./
RUN yarn install
COPY app/spec ./spec
COPY app/src ./src
RUN yarn test
# Clear out the node_modules and create the zip
FROM app-base AS app-zip-creator
RUN rm -rf node_modules &&
apk add zip &&
zip -r /app.zip /app
# Dev-ready container - actual files will be mounted in
FROM base AS dev
CMD [“mkdocs”, “serve”, “-a”, “0.0.0.0:8000”]
# Do the actual build of the mkdocs site
FROM base AS build
COPY . .
RUN mkdocs build
# Extract the static content from the build
# and use a nginx image to serve the content
FROM nginx:alpine
COPY --from=app-zip-creator /app.zip /usr/share/nginx/html/assets/app.zip
COPY --from=build /app/site /usr/share/nginx/html
Como puede ver, crea nuestra imagen no solo a partir de una, sino de 3 imágenes base:python:alpine
,node:12-alpine
ynginx:alpine
.
Cuando corresdocker build -t docker101tutorial .
, comenzará descargando la primera imagen base:
Luego ejecutará todos los comandos que definimos en el Dockerfile.
Continúa hasta que llegamos al final:
Ahora tenemos la imagendocker101tutorial
y podemos ejecutar un contenedor basado en esta imagen.
Ejecuta el comandodocker run
con esos atributos:
docker run -d -p 80:80 --name docker-tutorial docker101tutorialWe’re using the option -d
to run the container in background and print the container ID. If you miss this flag, you will not immediately get back to the shell until the container exits (but if it’s long-lived, for example it runs a service like a Node app or something, it will not exit automatically).
The -p
option is used to map port 80 of the container to the host machine port 80. The container exposes a Web server on port 80, and we can map ports on our computer to ports exposed by the container.
--name
assigns a name to the container, and finally we have the image name (docker101tutorial
) we should use to create the container.
If you have any doubt about a command option, run docker <command> --help
, in this case docker run --help
and you’ll get a very detailed explanation:

This command is very fast and you’ll get the container ID back:

More docker tutorials:
- Introduction to Docker
- Introduction to Docker Images
- Introduction to Docker Containers
- Dockerfiles
- Installing Docker on macOS
- First steps with Docker after the installation
- Using Docker Desktop to manage a Container
- Create a simple Node.js Hello World Docker Container from scratch
- What to do if a Docker container immediately exits
- Working with Docker Containers from the command line
- Working with Docker Images from the command line
- Sharing Docker Images on Docker Hub
- How to access files outside a Docker container
- How to commit changes to a Docker image
- Updating a deployed container based on a Docker image