Container’s Layer
A container’s layer is the read-write layer created when a container is launched. It’s the layer where all changes made in the container are saved. This layer is deleted with the container and therefore should not be used as persistent storage.
Launching a Container
Use the following command to launch an interactive shell in a container based on the ubuntu:18.04 image.
docker container run -ti ubuntu:18.04Installing a Package
figlet is a package that takes text input and formats it in a fun way. By default, this package is not available in the ubuntu image. Verify this with the following command:
figletThe command should give the following result:
bash: figlet: command not foundInstall the figlet package with the following command:
apt-get update -y
apt-get install figletVerify that the binary works:
figlet HolaWhich should give the following result:
_ _ _
| | | | ___ | | __ _
| |_| |/ _ \| |/ _` |
| _ | (_) | | (_| |
|_| |_|\___/|_|\__,_|Exit the container.
exitLaunching a New Container
Launch a new container based on ubuntu:18.04.
docker container run -ti ubuntu:18.04Check if the figlet package is present.
figletYou should get the following error:
bash: figlet: command not foundHow do you explain this result?
Each container launched from the ubuntu image is different from others. The second container is different from the one in which figlet was installed. Each corresponds to an instance of the ubuntu image and has its own layer, added on top of the image layers, where all changes made in the container are saved.
Exit the container:
exitRestarting the Container
List the containers (running or not) on the host machine:
docker container ls -aFrom this list, retrieve the ID of the container in which the figlet package was installed and restart it with the following command.
Note: the start command allows starting a container in the Exited state.
docker container start CONTAINER_IDLaunch an interactive shell in this container using the exec command.
docker container exec -ti CONTAINER_ID bashVerify that figlet is present in this container.
figlet HolaYou can now exit the container.
exitCleanup
List the containers (running or not) on the host machine:
docker container ls -aTo remove all containers, we can use the rm and ls -aq commands together. We add the -f option to force the removal of containers still running. Otherwise, we would need to stop the containers and then remove them.
docker container rm -f $(docker container ls -aq)All containers have been removed, verify it once again with the following command:
docker container ls -a