primeros pasos con docker
Sección: Contenedores & VM
Creado: 13-02-21 (Actualizado: 27-02-22)
Información de la versión y el estado
docker version
docker info
sudo service docker status
Descargamos una imagen
docker pull busybox
docker images
docker pull -a busybox
Contenedores
docker run busybox echo "hola"
docker pull busybox:ubuntu-14.04
docker run -t -i busybox:ubuntu-14.04
docker run -t -i ubuntu
docker search mysql
Ctrl + P y Ctrl + Q desacopla la terminal del contenedor.
docker attach jolly_lovelace
docker diff name
docker [stop|start|restart|pause|unpause] name
docker run --name ...
docker ps -a
Limpieza
opción --rm borra el contenedor cuando acaba
docker rm 'sudo docker ps -aq --no-trunc'
docker rm container_name
docker rmi image_name
docker image rm image_name
docker image prune
docker image prune -a
Creación
docker commit container_name image_name
daemon
docker run -d ubuntu bash -c "while true;do date;sleep 5;done"
docker logs name
Build images
Dockerfile basic example
FROM busybox:latest
CMD echo hola!
docker build .
docker tag image_id name:tag
docker build -t name .
docker build -f filename
docker history __image_name_
Dockerfile build instructions
FROM <image>[:<tag>]
MAINTAINER <author's detail>
# The COPY instruction enables you to copy the files from the Docker host to the
filesystem of the new image.
COPY <src> ... <dst>
COPY html /var/www/html
# ADD is similar to COPY but it can handle tar files
ADD <src> ... <dst>
ENV <key> <value>
ENV DEBUG_LVL 3
ENV APACHE_LOG_DIR /var/log/apache
USER <UID>|<UName>
WORKDIR <dirpath>
WORKDIR /var/log
# VOLUME instruction creates a directory in the image filesystem
VOLUME ["<mountpoint>"]
VOLUME <mountpoint>
# EXPOSE instruction opens up a container network port
EXPOSE <port>[/<proto>] [<port>[/<proto>]...]
EXPOSE 7373/udp 8080
RUN <command>
RUN ["<exec>", "<arg-1>", ..., "<arg-n>"]
RUN echo "echo Welcome to Docker!" >> /root/.bashrc
# CMD The command supplied through the RUN instruction is executed
during the build time whereas the command specified through the CMD
instruction is executed when the container is launched from the newly created
image
CMD <command>
CMD ["<exec>", "<arg-1>", ..., "<arg-n>"]
CMD ["echo", "Dockerfile CMD demo"]
# docker run cmd-demo echo Override CMD demo
# the entry point
application is launched by using the ENTRYPOINT instruction, which cannot be
overridden by using the docker run subcommand arguments. However, these
docker run subcommand arguments will be passed as additional arguments to
the entry point application. Having said this, Docker provides a mechanism for
overriding the entry point application through the --entrypoint option in the
docker run subcommand. The --entrypoint option can accept only word as its
argument, and so it has limited functionality
ENTRYPOINT <command>
ENTRYPOINT ["<exec>", "<arg-1>", ..., "<arg-n>"]
ENTRYPOINT ["echo", "Dockerfile ENTRYPOINT demo"]
# The ONBUILD instruction registers a build instruction to an image and this is
triggered when another image is built by using this image as its base image
ONBUILD <INSTRUCTION>
ONBUILD ADD config /etc/appconfig
.dockerignore file
The.dockerignore is a newline-separated TEXT file, wherein you can provide the files and the directories which are to be excluded from the build process.
.git
*.tmp
Dockerfile example
###########################################
# Dockerfile to build an Apache2 image
###########################################
# Base image is Ubuntu
FROM ubuntu:14.04
# Author: Dr. Peter
MAINTAINER Dr. Peter <[email protected]>
# Set the log directory PATH
ENV APACHE_LOG_DIR /var/log/apache2
# Expose port 80
EXPOSE 80
# Install apache2 package
RUN apt-get update && \
apt-get install -y apache2 && \
apt-get clean
Services in a container
ifconfig docker0
docker inspect __containerNAME__
Publishing container ports – the -p option
docker run -p <hostPort>:<containerPort>
docker run -d -p 80:80 apache2
docker run -d -p 198.51.100.73:80:80 apache2
docker run -d -p 80 apache2
docker run -d -p 198.51.100.73::80 apache2
In dockerfile we can use expose
EXPOSE 80
and then
docker run -d -P apache2
sudo iptables -t nat -L -n
Adminer
docker run --rm -d --network sf1_default -p 8080:8080 adminer