You will learn:

  • What is the image, what is the container and what is the difference between them.
  • Find images on Docker Hub.
  • Launch the container using the docker run.

You'll find that with Docker, you can run different types of applications without the hassle of installing them. You will know how to run Docker containers (among other things) when preparing your first assignment.

Start work

Run a command prompt (eg Windows Terminal or GIT bash)

Find out which directory you are in:

pwd
ls -l

Verify that you have an accessible Docker client:

docker

Search for a busybox image

You can view the public application registry via the [Docker Hub] web interface (https://hub.docker.com/). You can also search for finished images from the command line.

There are many different other applications in the Docker Hub registry - see list of most popular applications.

Busybox is a set of basic Linux commands in one package - a command line and a popular text editor in one.

Look for an image of this program:

docker search busybox

Local image register

There is a local register of available images on your machine. If the image is in the local registry, it can be started immediately. If the required image is not in the local register, get it from the central register with the command docker pull.

docker pull busybox

This will "install" the application. The image of the container that contains everything you need is saved and is ready to run.

Warning - only install images you trust. Although they are Docker processes separate from the rest of the system, you still don't want to run unverified software.

To view the local registry, type:

docker image ls

Creating a container

In this section, we will learn how to start and end the container. We find that the container is temporary - after it is closed, we can view the created files and standard output, but we cannot restore it. The docker run command always creates a new container. The advantage is that we always have a "clean table" - the condition of the container at start-up is always the same.

Use the docker run command to try to run the application:

docker run -it busybox

If an application image is available, a new temporary container is created and executed. If the image is not available in the local registry, the system will try to retrieve it from the central registry. The -it switch opens the container console and connects it to the current standard input. The --rm switch ensures that the container is temporary and that no files remain after the container is shut down.

If you see [error message](https://stackoverflow.com/questions/48623005/docker-error-the-input-device-is-not-a-tty-if-you-are-using- mintty-try-prefi):

the input device is not a TTY. If you are using mintty, try prefixing the command with 'winpty'

add winpty before thedocker command.

winpty docker run -it busybox

You will find yourself inside a running container.

Take a look around:

ls / # What are the files here?
pwd # What directory am I in
whoami # What's my login name?

You can execute commands from the container without launching the interactive console. Instead of the default command (in the case of busybox it is the command line execution) you know as additional arguments forrun to write the commands to be executed.

docker run busybox pwd
docker run busybox ls -l /

Open the interactive console in a new container, create a text file and write a message in it:

docker run -it busybox
echo "Hello World"> sprava.txt # We will write a message
ls -l # Verify that the file exists
cat ./sprava.txt # We will check the contents of the file

To leave a running container, end the currently running process (bash):

exit

or CTRL + C.

See a list of all containers:

docker container ls -a

The -a switch causes a listing of all containers, even those that have ended.

Try running the same container again and make sure the file is there:

docker run -it busybox
cat ./sprava.txt
ls -l

Leave the running container (CTRL + D) and look at the list of containers again:

docker container ls -a

What did we find out?

  • An entry in the container database remained after each start.
  • Each launched container has its own identification string.
  • Multiple containers can be launched from one image.
  • The new start starts with the same initial state.

If we find out the identification string of the container, we can look at the error messages at the time when the container ended:

docker logs <ID>

We can even copy the file we created:

docker cp <ID>: sprava.txt ./

If we are not interested in the state of the container after the end, we enter the --rm switch at startup.

docker run --rm -it busybox

What happens if we omit the -it switch?

Running in the background

How do we return to an already launched container?

Run the container (using -d) and the commandsleep 60 (waiting 60 seconds and then ending) and see a list of all active containers:

<! - Note -d has a different behavior on Docker for Windows .. (container ends immediately). Probably related to standard input ->

docker run -d busybox sleep 60
docker container ls

You can also view a list of all Docker processes:

docker ps

Find the identification name of the running container and connect to it:

docker exec -it <Container ID>

Note that the container closes after 60 seconds, so the command may fail.

To ensure an automatic restart of the container, use the --restart always switch. If your Docker application crashes or quits, the system will automatically restart it:

docker run -d --restart always busybox sleep 60

To stop a running container with a command, the argument is the number of the running container.

docker stop <Container ID>

Clear the cache

Obtaining an image or launching a container requires hard disk space. Each acquired image or launched container takes up some space.

docker image ls
docker ps -a --size

Over time, disk space may run out. Records of closed containers can be deleted using the command:

docker container rm <container ID>

To remove an unused image, use:

docker image rm <image name>

Docker also includes "intelligent cache cleaning":

docker system prune

Please note that we can also remove useful information when cleaning.

Homework

  • Find and run the "python" image.
  • Find out what other operations can be performed with the docker command.

Other materials

  • What is the difference between container and image?

Previous Post Next Post

Week 2: Launching the container