Skip to the content.

Docker Overview

Why do you need Docker?

Diagram Description automatically generated

What can it do?

Graphical user interface, application Description automatically generated

What are containers?

Graphical user interface Description automatically generated

Operating System

Graphical user interface, application Description automatically generated

Containers vs Virtual Machines

Graphical user interface, application Description automatically generated

Graphical user interface Description automatically generated

How is it done?

Graphical user interface Description automatically generated

Container vs image

Diagram Description automatically generated

Docker commands

Command Function
docker run (-d) image_name Run an instance of image_name_app if the image it’s not present then will be download from the docker hub. (The same as before but running on the detach mode, this will run in the background, and you can use the terminal)
docker ps (-a) List of containers (all running or not containers)
docker stop id or container_name Stop a container
docker rm id or container_name Remove a container, the correct output is the name of the container or id. It’s not necessary to provide the full id, and you can remove more than one by writing the ids (a part) separated by space.
docker images List of available images
docker rmi image_name Remove images (All containers must be deleted!)
docker pull image_name Download (not run) an image
docker exec container_id command Execute a command on a specified container
docker inspect container_name Details of the container in JSON format
docker logs container_name (or id) Logs of detach running container

Containers are meant to run a specific task or process such as to host an instance of a web server, application server or a database, etc.

Once the task its complete the container exits

A container only lives as long as the process inside it is alive.

A screenshot of a computer Description automatically generated with medium confidence

This is why when you run a container from an ubuntu image it stops immediately because ubuntu is just an image of an OS, there is no process or app running by default.

docker run

A screenshot of a computer Description automatically generated with medium confidence

Graphical user interface Description automatically generated

Jenkins tutorial

Go to https://hub.docker.com/_/jenkins if you want more details.

We are going to explore the run command using de -p and -v options by following the next steps:

Graphical user interface, application Description automatically generated

Text Description automatically generated

Text Description automatically generated

Text Description automatically generated

A screenshot of a computer Description automatically generated with medium confidence

Text Description automatically generated

Background pattern Description automatically generated

Text Description automatically generated

Graphical user interface, text, application Description automatically generated

Text Description automatically generated

Graphical user interface Description automatically generated

Graphical user interface, application Description automatically generated

Graphical user interface, application Description automatically generated

Docker Images

You will create an image because you cannot find a component or a service that you need as a part of your application on docker hub, or you want to dockerize your application.

How to create an image? (Based on Python Flask)

Manually you will usually need to follow the followings steps:

  1. OS (Ubuntu)

  2. Update apt repo

  3. Install dependencies using apt

  4. Install Python Dependencies

  5. Copy source code to /opt folder

  6. Run the web server using ‘flask’ command

With Docker you create a docker file that does the job.

Dockerfile

A dockerfile its simple txt file composed of Instructions and Arguments.

A screenshot of a computer Description automatically generated with medium confidence

Layered architecture

Text Description automatically generated with medium confidence

docker history image_name

Environment Variables

We can pass an ENVIRONMENT VARIABLE to the container we use the -e command

docker run -e VAR_NAME=var_value image_name

For example, if we have an image (webapp_image) in this case we can pass the env variable APP_COLOR executing:

docker run -e APP_COLOR=green webapp_image

Text Description automatically generated

If you want to find the environment variables of a running container, you can use the inspect command.

docker inspect container_name(id)

For a more explicit output, run:

docker exec -it container_name env

Commands and Entrypoint

Remember that containers are not meant to host an OS, are meant to run task or process.

So who defines what process is run within the container?
In the dockerfile there is an instruction called CMD (command) that defines the program that will be executed within the container when it starts.

For example this is a dockerfile for the Nginx image so the image starts with nginx command.

Text, letter Description automatically generated

If we look at a dockerfile of the Ubuntu image, will see that it uses bash as the default command, that is not a process it’s a shell the listen from inputs, so that’s why a container of a plain ubuntu image stops immediately after its instantiation.

Graphical user interface, text, application Description automatically generated

We can run a process and avoid that stops executing: docker run ubuntu sleep 10 (or other instruction), but if you want to do this change permanent you can create your image as follows:

Graphical user interface, website Description automatically generated

In this example the 5 seconds are hardcoded, if we want to modify this, we can run:

docker run ubuntu-sleeper sleep 10, this will overwrite the default CMD sleep in the dockerfile.

But doesn’t make sense typing sleep command again, if we want to just pass the value as a parameter, we need to use the ENTRYPOINT instruction.

Graphical user interface, text, application, chat or text message Description automatically generated

The ENTRYPOINT instruction it’s like the CMD instruction as in you can specify the program that will run when the container starts, but whatever you specify in the command line will be appended to ENTRYPOINT.

But by this way it will be no default value for the ENTRYPOINT, so we need to combine both CMD and ENTRYPOINT instructions:

A screenshot of a computer Description automatically generated with medium confidence

Difference: In the case of the CMD instruction the command line parameters passed will get replaced entirely whereas in case of ENTRYPOINT will get appended.