Herramientas de usuario

Herramientas del sitio


informatica:sistemas_operativos:cursos:docker_avanzado:ejercicios_practicos

Diferencias

Muestra las diferencias entre dos versiones de la página.

Enlace a la vista de comparación

Próxima revisión
Revisión previa
informatica:sistemas_operativos:cursos:docker_avanzado:ejercicios_practicos [2023/10/10 18:49] – creado tempwininformatica:sistemas_operativos:cursos:docker_avanzado:ejercicios_practicos [2023/10/10 18:58] (actual) tempwin
Línea 1: Línea 1:
 ====== Ejercicios prácticos (Docker avanzado) ====== ====== 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
  
-1. Check that docker is correctly running and that you have permission to use the +<code>
-engine+
 docker info docker info
-2. (pull) Pull an image from the official registry, eg: debian:latest (you can +</code> 
-browse https://store.docker.com if you want to find other images).+ 
 +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). 
 + 
 +<code>
 docker pull debian:latest docker pull debian:latest
-debian is the repository name, and :latest is a tag that identifies an image in +</code> 
-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 +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). 
-distribution.+ 
 +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: You can check that your image is present in the docker engine:
 +
 +<code>
 docker images docker images
-3. (run) Run a container from this image.+</code> 
 + 
 +3. (**run**) Run a container from this image. 
 + 
 +<code>
 docker run debian:latest docker run debian:latest
-(you may also write docker run debian which is equivalent: :latest is the +</code> 
-defaut tag if none are provided) + 
-Nothing happens? actually the container has already terminated, you can display +(you may also write docker run debian which is equivalent: '':latest'' is the defaut tag if none are provided) 
-it with docker ps , but add -a/--all because non-running container are not + 
-displayed by default.+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. 
 + 
 +<code>
 docker ps -a docker ps -a
-The default command of the debian image is /bin/bash and by default docker +</code> 
-containers are run without stdin (it is redirected from /dev/null ). Thus bash + 
-exits immediately. +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 +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). 
-of bash).+ 
 +<code>
 docker run debian ls /bin docker run debian ls /bin
 docker run debian cat /etc/motd docker run debian cat /etc/motd
-5. (stdin) Let's go back to bash, this time we want interact with the shell. To keep +</code> 
-stdin open, we launch the container with -i/--interactive .+ 
 +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''. 
 + 
 +<code>
 docker run -i debian docker run -i debian
-The container runs, but displays nothing. Actually bash is running in batch +</code> 
-mode. You can try to execute commands (eg: ls , id , hostname ...) and you will + 
-see the result. +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). +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+6. (**tty**) To have a real interactive shell inside our container, we need to allocate a tty with ''-t/%%--%%tty'': 
 + 
 +<code>
 docker run -t -i debian docker run -t -i debian
-7. (start) You can exit your container and display the list of all containers:+</code> 
 + 
 +7. (**start**) You can exit your container and display the list of all containers: 
 + 
 +<code>
 docker ps -a docker ps -a
-It is possible to start them again with docker start . Like the run command, +</code> 
-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 +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: 
-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:+<code>
 docker start -i 85bcdca6c38f docker start -i 85bcdca6c38f
 docker start -i 85bcd docker start -i 85bcd
 docker start -i 85 docker start -i 85
-docker start -i +docker start -i 85bcdca6c38f07e3f8140cbf8b4ad37fd80d731b87c6945012479439a450a443
-85bcdca6c38f07e3f8140cbf8b4ad37fd80d731b87c6945012479439a450a443+
 docker start -i pensive_hodgkin docker start -i pensive_hodgkin
-8. (commit) You can modify files inside a container. If you restart the same +</code> 
-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) +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.  
-because docker uses a copy-on-write filesystem. Use the command docker + 
-diff to show the difference of a container from its image. +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 +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). 
-have to commit the conainer (i.e take a snapshot).+ 
 +<code>
 docker commit CONTAINER docker commit CONTAINER
-This operation creates a new image (visible in docker images ). This image in +</code> 
-turn can be used to start a new container. + 
-Note: docker commit does not affect the state of the container. If it is running, +This operation creates a new image (visible in docker images ). This image in turn can be used to start a new container.  
-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 +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. 
-docker rm to remove them. Alternatively you can run docker container + 
-prune which removes all dead container. +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: +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 +  * ''%%--%%rm'' to remove the container automatically when it terminates 
-▪ -u/--user to run the container as a different user +  * ''-d/%%--%%detach'' to run a container in the background 
-▪ -w/--workdir to start the container in a different directory +  * ''-u/%%--%%user'' to run the container as a different user 
-▪ -e/--env to set an environment variable +  * ''-w/%%--%%workdir'' to start the container in a different directory 
-▪ -h/--hostname to set a different hostname (the host name inside +  * ''-e/%%--%%env'' to set an environment variable 
-the container) +  * ''-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 +  * ''%%--%%name'' to set a different name (the name of the container in the docker engine) 
-docker engine) +  also you may type ''docker run %%--%%help'' to display all configuration keys    
-▪ 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)
-o other docker commands (note: some of these commands require the + 
-container to be running, just launch docker run -d -t -i debian to +  * ''docker inspect'' to display the metadata of a container (json format) 
-have one that keeps running in the background) +  * ''docker cp'' to transfer files from/into the container 
-▪ docker inspect to display the metadata of a container (json +  * ''docker exec'' to have launch a separate command (very useful for providing a debugging shell -> ''docker exec -t -i CONTAINER bash''
-format) +  * ''docker top'' to display the processes running inside the container 
-▪ docker cp to transfer files from/into the container +  * ''docker stats'' to display usage statistics 
-▪ docker exec to have launch a separate command (very useful +  * ''docker logs'' to display the container output 
-for providing a debugging shell -> docker exec -t -i +  * ''docker attach'' to reattach to the console of a detached container
-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+
  
informatica/sistemas_operativos/cursos/docker_avanzado/ejercicios_practicos.1696956590.txt.gz · Última modificación: por tempwin