====== Ejercicios prácticos (Docker avanzado) ======
Contenido perteneciente al curso [[informatica:sistemas_operativos:cursos:docker_avanzado|Docker avanzado]].
1. Check that docker is correctly running and that you have permission to use the engine
docker info
2. (**pull**) Pull an image from the official registry, eg: debian:latest (you can browse https://store.docker.com if you want to find other images).
docker pull debian:latest
debian is the repository name, and '':latest'' is a tag that identifies an image in the repository (for the case of the debian repository it is the latest stable release).
You could also write '':jessie'' or '':stretch'' to use a specific version of the distribution.
You can check that your image is present in the docker engine:
docker images
3. (**run**) Run a container from this image.
docker run debian:latest
(you may also write docker run debian which is equivalent: '':latest'' is the defaut tag if none are provided)
Nothing happens? actually the container has already terminated, you can display it with ''docker ps'', but add ''-a/%%--%%all'' because non-running container are not displayed by default.
docker ps -a
The default command of the debian image is ''/bin/bash'' and by default docker containers are run without stdin (it is redirected from ''/dev/null'' ). Thus bash exits immediately.
4. (**run a command**) You may override the default command by providing extra arguments after the image name. Then this command will be executed (instead of bash).
docker run debian ls /bin
docker run debian cat /etc/motd
5. (**stdin**) Let's go back to bash, this time we want interact with the shell. To keep stdin open, we launch the container with ''-i/%%--%%interactive''.
docker run -i debian
The container runs, but displays nothing. Actually bash is running in batch mode. You can try to execute commands (eg: ''ls'', ''id'', ''hostname'' ...) and you will see the result.
Bash is in batch mode because it is not running on a terminal (its stdout is a pipe, not a tty).
6. (**tty**) To have a real interactive shell inside our container, we need to allocate a tty with ''-t/%%--%%tty'':
docker run -t -i debian
7. (**start**) You can exit your container and display the list of all containers:
docker ps -a
It is possible to start them again with ''docker start'' . Like the ''run'' command, you may use ''-i'' to have stdin open. Note that start expects you to tell which container you want to start. Containers may be identified either by their id (first column of ''docker ps'') or by their name (last column). You may provide only the first digits of the id (as long as there is no ambiguity). Examples:
docker start -i 85bcdca6c38f
docker start -i 85bcd
docker start -i 85
docker start -i 85bcdca6c38f07e3f8140cbf8b4ad37fd80d731b87c6945012479439a450a443
docker start -i pensive_hodgkin
8. (**commit**) You can modify files inside a container. If you restart the same container you can note that these changes are still present. However they will not be present in the other container (even if they are running the same image) because docker uses a copy-on-write filesystem.
Use the command ''docker diff'' to show the difference of a container from its image.
Remember that all changes inside a container are thrown away when the container is removed. If we want save a container filesytem for later use, we have to commit the conainer (i.e take a snapshot).
docker commit CONTAINER
This operation creates a new image (visible in docker images ). This image in turn can be used to start a new container.
Note: ''docker commit'' does not affect the state of the container. If it is running, then it just keeps running. You may take as many snapshots as you like.
9. (**rm**) You now have too many dead containers in your engine. You should use ''docker rm'' to remove them. Alternatively you can run ''docker container prune'' which removes all dead container.
10. (**extras**) If you still have extra time, you can experiment o the other docker run options we introduced so far:
* ''%%--%%rm'' to remove the container automatically when it terminates
* ''-d/%%--%%detach'' to run a container in the background
* ''-u/%%--%%user'' to run the container as a different user
* ''-w/%%--%%workdir'' to start the container in a different directory
* ''-e/%%--%%env'' to set an environment variable
* ''-h/%%--%%hostname'' to set a different hostname (the host name inside the container)
* ''%%--%%name'' to set a different name (the name of the container in the docker engine)
* also you may type ''docker run %%--%%help'' to display all configuration keys
Other docker commands (note: some of these commands require the container to be running, just launch docker ''run -d -t -i debian'' to have one that keeps running in the background):
* ''docker inspect'' to display the metadata of a container (json format)
* ''docker cp'' to transfer files from/into the container
* ''docker exec'' to have launch a separate command (very useful for providing a debugging shell -> ''docker exec -t -i CONTAINER bash'')
* ''docker top'' to display the processes running inside the container
* ''docker stats'' to display usage statistics
* ''docker logs'' to display the container output
* ''docker attach'' to reattach to the console of a detached container