Docker for Beginners

An article that will finally help you understand the key concepts of Docker

Mykola-Bohdan Vynnytskyi
6 min readOct 30, 2022
Photo by Venti Views on Unsplash

Intro

When you start learning programming, sooner or later you will hear about Docker.

This cute blue whale helps us run our projects without changing the configuration of our environment. In this article, you will finally understand the basics and start your own database with a UI interface.

Let’s start!

What is Docker and why it is used?

Suppose you create your project using Java 11 and MySQL 8.0. The project works great and you decide to share it with your friend. Your friend tries to run it, but encounters various errors and decides that the program is not working. But it works great on your local machine!

Why did this happen?
Your friend had different versions of Java and MySQL and in order for him to run your program, he needed to install the corresponding versions.

Agree, this is not very convenient and takes up extra time that he would spend on seeing your cool program.

Docker was created precisely for such cases (and not only).

Docker is a tool for packaging, delivering, and launching an application.

  • Packaging — We take our application with all the necessary dependencies and put it in a container (we’ll talk about it soon)
  • Delivering — We send our container to a friend so they can run it
  • Launching — All containers launch the same way, allowing your friend to run the app without any problems

Dockerfile vs Image vs Container

Now that you have an idea of what Docker is, let’s understand its basic concepts.

Dockerfile

A dockerfile is an instruction for creating an image (which we will talk about a little below).
It contains all the necessary environment setup commands, variables, and startup order instructions.

Consider a simple example:

# DockerfileFROM alpine
CMD ["echo", "Hello Medium!"]

This file contains instructions for creating an image that will be based on Alpine and the execution command CMD [“echo”, “Hello Medium!”]

Docker Image

An image is a file that represents your packaged program with all the dependencies needed to work.
The image is immutable and is used to create and run containers with your application.
If you are from the OOP world, you can think of an image as a class that will be used to create objects (containers)

To create an image based on the Dockerfile we described above, you need to run the following command:

docker build -t hello .

Let’s understand each word here:

  • docker build — method that allows us to build our own Docker images
  • -t — option to mention the tag to the image

Docker tags are just an alias for an image

  • hello — the alias for our image
  • . — represent the working directory, if you’re running the command in the same directory as the docker file, just use a dot, otherwise, specify the location

Docker Container

A container is an instance of an image. It is an isolated unit that represents the operation of your program.

Containers are lightweight, which allows you to create multiple instances based on one image that will work independently of each other with their data and settings.

Each container has its ID and one of seven states: сreated, restarting, running, removing, paused, exited, and dead

After our image has been successfully created, we can start our container with the following command:

docker run --rm hello
  • docker run — command that creates a container over the specified image and then runs it
  • -- rm — option to automatically remove the container when it exits
  • hello — the name of the image

To see all containers and information about them simply write this command:

docker ps -a
  • docker ps — command to display a list of containers
  • -a or --all — option to show all containers (default shows just running)

Port Mapping

For example, you have a web application that opens on port localhost:8080, if you try to run it in a container and go to the link, you will see that there is no web page.
But the container works, so what’s the problem?

As mentioned above, a docker container is an isolated environment, which means that it cannot be reached from the outside world. When you try to access your site through a browser, you are trying to knock from the outside world. Docker has a port mapping for such cases.

Port mapping is used to access the service inside the container. We open the host port to give us access to the corresponding open port inside the container.

It is important to understand that not all ports require mapping. Most containers stay with closed ports to keep our services private or only visible to containers on the same network.

In order to bind the port use this command:

docker run -p 80:8080 <image_name>
  • -p or --publish — option to publish a container’s port(s) to the host
  • 80 — external port
  • 8080 — internal port of the application

Volume

Let’s say that you run a test database using a docker container, you wrote thousands of records into it, but then something happened and your container crashed.
Since the container is isolated, all the data inside it will be lost along with it. To prevent such a scenario there is a volume.

Volume is a mechanism for persisting data generated by and used by Docker containers.
Using volumes allows data to be stored outside of the container, ensuring that it is preserved even in the event of a container failure.

Going back to our example, you can work with your test database, save the data, and when you’re done, shut down the container without fear of losing your data.

To bind mount a volume use the following command:

docker run -v <your_dir>:<conteiner_dir> <image_name>
  • -v or --volume — option to bind mount a volume

Docker-compose

At the beginning of this article, we looked at an example where you have an application written in Java and a MySQL database, now we know that for this application to work in the same way on all devices, we need to put the Java application and the database in containers.

Such projects are called multi-container. And instead of running each container separately, there is a special mechanism — docker-compose.

Docker-compose is a tool created to define and share multi-container applications.
With Compose we can create a YAML file to define the services, ports, dependencies, etc. and with a single command start or break everything.

Very convenient isn’t it?
Next, we will look at an example of such a multi-container project.

Set Up your first Database

Consider the following file:

After creating the docker-compose file, enter the following command in the terminal to start the containers:

docker compose up

Make sure the file is in the same location you run the command from

Now you can follow the link http://localhost:8080 and open your UI,
enter login and password and work with your database from the browser.

Login: root
Password: rootpassword (or another if you change it in the file)

It will take a bit of time to get your base up and running with the interface, but eventually, you’ll be able to use your containers without any problems.

To stop containers, just type this command:

docker compose down

Conclusion

In this article, we looked at several examples of using Docker, got acquainted with the basic concepts, and started our first database!

Hope now you can definitely say that you know what Docker is.

I advise you to also read my other article, which perfectly shows how to work with Docker.

I will be glad for your comments and subscriptions.
See you!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Mykola-Bohdan Vynnytskyi
Mykola-Bohdan Vynnytskyi

Written by Mykola-Bohdan Vynnytskyi

Data Engineer by day. YouTuber, author, and creator of courses by night. Passionate about Big Data and self-development.

Responses (2)

Write a response

Never heard of Docker before! Thanks!

congratulations.....
I'm your 100th follower...