loading...

What is Docker, and how to use it? We tell in detail

tecyguy profile image TECHY360 Originally published at techy360.com on ・10 min read

Let’s take a look at the bottom line, because Docker is a powerful tool, and a huge amount of information on working with it is unlikely to fit into a brochure.

What is a docker?

This is open source software, the principle of which is easiest to compare with transport containers. Just think, because once transport companies faced similar problems:

  1. How to transport different (incompatible) types of goods together (for example, food with chemicals or glass with brick)?
  2. How to process packages of different sizes with the same vehicle?

With the introduction of containers, it became possible to transport bricks and glass together, chemicals and food, and much more. Cargo of different sizes can be distributed across standardized containers that are loaded / unloaded by the same vehicle.

But back to the containers. When you are developing an application, you need to provide the code along with all its components, such as libraries, server, databases, etc. You may find yourself in a situation where the application is running on your computer, but refuses to turn on another user’s device.

This problem is solved by creating software independence from the system.

What is the difference from virtualization?

Initially, virtualization was designed to eliminate such problems, but it has significant drawbacks:

  • slow loading;
  • possible payment for the provision of additional space;
  • not all virtual machines (VMs) support compatible use;
  • Supporting VMs often require complex configuration
  • the image may be too large, since the “additional OS” adds a gigabyte of space to the project on top of the operating system, and in most cases several VMs are put on the server, which take up even more space.

Docker simply splits the kernel of the OS into all containers (Docker container) that work as separate processes. This is not the only such platform, but, undoubtedly, one of the most popular and in demand.

What are the obvious pluses?

Its benefits include:

  1. Accelerated development process. There is no need to install auxiliary tools like PostgreSQL, Redis, Elasticsearch: they can be run in containers.
  2. Convenient application encapsulation.
  3. Clear monitoring.
  4. Simple scaling.

Supported Platforms

Docker works not only on its native OS, Linux, but is also supported by Windows and macOS. The only difference from interacting with Linux is that on macOS and Windows, the platform is encapsulated in a tiny virtual machine. Currently, Docker for macOS and Windows has achieved a significant level of usability.

In addition, there are many additional applications, such as Kitematic or Docker Machine , that help you install and use Docker on platforms other than Linux.

Installation

Here you can see detailed installation instructions. If you are working with Docker on Linux, you need to perform a few simple steps and re-enter the system:

sudo usermod -aG docker $(whoami)

Terminology

  1. A container is an executable instance that encapsulates the required software. It consists of images. It can be easily removed and re-created in a short amount of time.

  2. Image – the basic element of each container. Depending on the image, it may take some time to create it.

  3. Port – This is the TCP / UDP port in its original value. To keep things simple, suppose ports can be opened in the outside world or connected to containers (accessible only from these containers and invisible to the outside world).

  1. Volume – described as a shared folder. Volumes are initialized when the container is created and are designed to store data, regardless of the container’s life cycle.

  2. A registry is a server on which images are stored. Compare it with GitHub: you can pull an image from the registry to deploy it locally, and you can also add created images to the registry locally.

  3. Docker Hub is a public interface repository provided by Docker Inc. It stores many images. The resource is a source of “official” images made by the Docker team or created in collaboration with a software developer. For official images, their potential vulnerabilities are listed. This information is open to any registered user. Both free and paid accounts are available.

Example 1: Hello World

It’s time to launch our first container:

docker run ubuntu /bin/echo 'Hello world'

Console output:

Unable to find image 'ubuntu:latest' locally latest: Pulling from library/ubuntu d54efb8db41d: Pull complete f8b845f45a87: Pull complete e8db7bf7c39f: Pull complete 9654c40e9079: Pull complete 6d9ef359eaaa: Pull complete Digest: sha256:dd7808d8792c9841d0b460122f1acf0a2dd1f56404f8d1e56298048885e45535 Status: Downloaded newer image for ubuntu:latest Hello world
  • docker run is a container launch command.
  • ubuntu is the image that you are running (for example, an image of the Ubuntu operating system). When you specify it, Docker first analyzes the element in the context of the host.
  • / bin / echo ‘Hello world’ – the command that will be run inside the new container. This container simply displays “Hello world” and stops execution.

Now let’s try to create an interactive shell inside the container:

docker run -i -t --rm ubuntu /bin/bash
  • -t assigns a pseudo-tty or terminal inside a new container.
  • -i allows you to create an interactive connection by capturing the standard input (STDIN) of the container.
  • –rm is required to automatically remove the container when exiting the process. By default, containers are not deleted.

If you want the container to work after the session ends, you need to “demonize” it:

docker run --name daemon -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done"
  • –name daemon assigns a name to the new container. If you do not specify it, a name will be generated and assigned automatically.
  • -d starts the container in the background (“demonizes” it).

Let’s see what containers we have at the moment:

docker ps -a

Console output:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1fc8cee64ec2 ubuntu "/bin/sh -c 'while..." 32 seconds ago Up 30 seconds daemon c006f1a02edf ubuntu "/bin/echo 'Hello ..." About a minute ago Exited (0) About a minute ago gifted\_nobel
  • docker ps – a command to list containers.
  • -a shows all containers (without -a ps it shows only running containers).

ps shows us that we have two containers:

  • gifted_nobel (the name for this container was generated automatically) – the first container we created with “Hello world” typed.
  • daemon is the third container that we created and “demonized.”

Note: the second container (with an interactive shell) is missing, because we set the -rm parameter, as a result of which this container is automatically deleted after execution.

Let’s check the logs and see what the daemon container is doing right now:

docker logs -f daemon

Console output:

...hello world hello world hello world
  • docker logs get container logs.
  • -f monitors the output of the log.

Now let’s stop the daemon container:

docker stop daemon

Check its stop:

docker ps -a

Console output:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1fc8cee64ec2 ubuntu "/bin/sh -c 'while..." 5 minutes ago Exited (137) 5 seconds ago daemon c006f1a02edf ubuntu "/bin/echo 'Hello ..." 6 minutes ago Exited (0) 6 minutes ago gifted\_nobel

The container is stopped. Let’s run it again:

docker start daemon

Make sure it is running:

docker ps -a  

Console output:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1fc8cee64ec2 ubuntu "/bin/sh -c 'while..." 5 minutes ago Up 3 seconds daemon c006f1a02edf ubuntu "/bin/echo 'Hello ..." 6 minutes ago Exited (0) 7 minutes ago gifted\_nobel

Now stop it and delete all the containers manually:

docker stop daemon docker rm <your first container name> docker rm daemon

To remove all containers, we can use the following command:

docker rm -f $(docker ps -aq)
  • docker rm – command to delete the container.
  • -f (for rm) should stop the container if it works (force delete).
  • -q (for ps) is the output of container identifiers only.

Example 2: Nginx

Starting with this example, you will need additional files that you can find in the GitHub repository.

It’s time to create and run a more important container, such as Nginx.

Change the directory to examples / nginx:

docker run -d --name test-nginx -p 80:80 -v $(pwd):/usr/share/nginx/html:ro nginx:latest

Console output:

Unable to find image 'nginx:latest' locally latest: Pulling from library/nginx 693502eb7dfb: Pull complete 6decb850d2bc: Pull complete c3e19f087ed6: Pull complete Digest: sha256:52a189e49c0c797cfc5cbfe578c68c225d160fb13a42954144b29af3fe4fe335 Status: Downloaded newer image for nginx:latest 436a602273b0ca687c61cc843ab28163c720a1810b09005a36ea06f005b0c971
  • -p – display ports HOST PORT: CONTAINER PORT.
  • -v is responsible for the HOST DIRECTORY: CONTAINER DIRECTORY.

Now check this url in your web browser.

We can also try changing /example/nginx/index.html (which is added to the / usr / share / Nginx / HTML directory inside the container) and refresh the page.

Get the information about the test-nginx container:

docker inspect test-nginx

This command displays system information about installing Docker. It includes the kernel version, the number of containers and images, open ports, etc.

Example 3: Dockerfile Record

To create an image, you first need to create a Dockerfile: this is a text file with instructions and arguments. A brief description of the instructions that we are going to use in the example:

  • FROM – set the base image
  • RUN – execute a command in a container
  • ENV – set the environment variable
  • WORKDIR – set the working directory
  • VOLUME – create a mount point for the volume
  • CMD – install the executable file for the container

More information here .

Let’s create an image that will receive the contents of the site and save it in a text file. We need to pass the URL through the SITE_URL variable. The resulting file will be placed in the directory installed as the volume:

FROM ubuntu:latest RUN apt-get update RUN apt-get install --no-install-recommends --no-install-suggests -y curl ENV SITE\_URL https://google.com/ WORKDIR /data VOLUME /data CMD sh -c "curl -L $SITE\_URL > /data/results"

Dockerfile is ready, it’s time to create an image.

Image creation

Go to examples / curl and run the following command:

docker build . -t test-curl  

Console output:

Sending build context to Docker daemon 3.584 kB Step 1/7 : FROM ubuntu:latest ---> 0ef2e08ed3faStep 2/7 : RUN apt-get update ---> Running in 4aa839bb46ecGet:1 http://archive.ubuntu.com/ubuntu xenial InRelease [247 kB] Get:2 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [102 kB] ...Fetched 24.9 MB in 4s (5208 kB/s) Reading package lists... ---> 35ac5017c794Removing intermediate container 4aa839bb46ec Step 3/7 : RUN apt-get install --no-install-recommends --no-install-suggests -y curl ---> Running in 3ca9384ecf8dReading package lists... Building dependency tree... Reading state information... The following additional packages will be installed... ---> f3c6d26b95e6Removing intermediate container 3ca9384ecf8d Step 4/7 : ENV SITE\_URL https://google.com/ ---> Running in 21b0022b260f ---> 9a733ee39a46Removing intermediate container 21b0022b260f Step 5/7 : WORKDIR /data ---> c024301ddfb8Removing intermediate container 3bc973e5584c Step 6/7 : VOLUME /data ---> Running in a9594a8958fe ---> 6802707a7114Removing intermediate container a9594a8958fe Step 7/7 : CMD sh -c "curl -L $SITE\_URL > /data/results" ---> Running in 37503bc4e386 ---> 5ebb2a65d771Removing intermediate container 37503bc4e386 Successfully built 5ebb2a65d771
  • docker build creates a new image locally.
  • -t sets the name label in the image.

Now we have a new image, and we can see it in the list of existing ones:

docker images  

Console output:

REPOSITORY TAG IMAGE ID CREATED SIZE test-curl latest 5ebb2a65d771 37 minutes ago 180 MB nginx latest 6b914bbcb89e 7 days ago 182 MB ubuntu latest 0ef2e08ed3fa 8 days ago 130 MB

We can create and run a container from an image. Let’s try to do this with the default settings:

docker run --rm -v $(pwd)/vol:/data/:rw test-curl

To view the results saved in a file:

cat ./vol/results  

Let’s try with facebook.com:

docker run --rm -e SITE\_URL=https://facebook.com/ -v $(pwd)/vol:/data/:rw test-curl

To view the results saved in a file:

cat ./vol/results

Image guidelines

  • Avoid installing unnecessary packages – they will consume additional disk space.
  • Use cache.
  • Be careful with volumes. You must remember what data is in the volumes. Since volumes are persistent and do not “die” with containers, the next container will use the data from the volumes that were created by the previous container.
  • Use environment variables (in RUN, EXPOSE, VOLUME). This will make your dockerfile more flexible.

Connection between containers

Docker compose is the only right way to connect containers to each other.

Example 4: Python + Redis

In this example, we will connect Python and Redis containers.

version: '2' services: app: build: context: ./app depends\_on: - redis environment: - REDIS\_HOST=redis ports: - "5000:5000" redis: image: redis:3.2-alpine volumes: - redis\_data:/datavolumes: redis\_data:

Let’s move on to examples / compose and execute the command:

docker-compose --project-name app-test -f docker-compose.yml up

The current example will increase the view count in Redis. Open the link and see for yourself.

Using docker-compose is a topic for the whole tutorial. To get started, you can play around with some of the images from the Docker Hub, and if you want to create your own, follow the recommendations listed above. The only thing you can add in terms of using docker-compose is to always give explicit names to your volumes. This simple rule will save you trouble in the future.

version: '2' services: ... redis: image: redis:3.2-alpine volumes: - redis\_data:/datavolumes: redis\_data:

In this case, redis_data will be the name inside the docker-compose.yml file.

We look at the execution of the volume:

docker volume ls  

Console output:

DRIVER VOLUME NAME local apptest\_redis\_data

Without an explicit volume name, there will be a UUID. And here is an example:

DRIVER VOLUME NAME local ec1a5ac0a2106963c2129151b27cb032ea5bb7c4bd6fe94d9dd22d3e72b2a41b local f3a664ce353ba24dd43d8f104871594de6024ed847054422bbdd362c5033fc4c local f81a397776458e62022610f38a1bfe50dd388628e2badc3d3a2553bb08a5467f local f84228acbf9c5c06da7be2197db37f2e3da34b7e8277942b10900f77f78c9e64 local f9958475a011982b4dc8d8d8209899474ea4ec2c27f68d1a430c94bcc1eb0227 local ff14e0e20d70aa57e62db0b813db08577703ff1405b2a90ec88f48eb4cdc7c19 local polls\_pg\_data local polls\_public\_files local polls\_redis\_data local projectdev\_pg\_data local projectdev\_redis\_data

Finally

Docker has become one of the most important tools of a modern developer. Yes, it has some limitations and requirements depending on the architecture of your system.

The post What is Docker, and how to use it? We tell in detail appeared first on TECHY360.

Discussion

markdown guide