Docker, how to start with it

Mateo mojica
8 min readApr 19, 2022

I’m pretty sure you have heard at least once the word docker and wondered what that might be. Well in this article I’m going to do my best to share with you all that I have learned about docker as a total beginner.

Photo by Todd Cravens on Unsplash

First, let’s talk about what is docker. Docker is a platform for developing, shipping, and running applications. Docker enables you to separate your applications from your machine and OS, so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker’s methodologies for shipping, testing, and deploying code quickly, you can significantly reduce the delay between writing code and running it in production, since you are developing your code in a production-like environment.

By now you might be asking yourself “well and why do I need to use docker?” and the answer is very straightforward, Docker streamlines the development process by allowing developers to work in standardized environments using local containers which provide your applications and services, therefore eliminating the known phrase “it runs on my machine” since all the container are the same and have only the dependencies needed. Also, containers are great for continuous integration and continuous delivery (CI/CD) workflows.

Photo by Alvin Lenin on Unsplash

Since we are talking about introductory concepts let’s take a look at the bases of docker. The foundation of docker are the images, these are read-only templates that contain all the information to create a container out of it. Containers are running instances of an image, you can create as many containers from the same image as you need, making docker a powerful tool for scaling applications and services. Think of it as OOP where the image is the class and the container is the object (an instance of a class). You can also have inheritance by basing your image on another image, getting all the properties of that image.

How do we build those images? Images are created using docker files that describe the steps to create the image. Some of the main parts of a docker file are:

  • The base image: This is the image that your application is based on. Try to choose the closest image that fits your needs, for example, if you are going to build a node application base it on the node image and not from the Linux image since it will save you a lot of configuration steps in the build.
  • Configuration commands: These are the configuration steps that you would follow in a system running that base image, with these you can install dependencies and customize configurations to your needs.
  • Folder structure: Your application will have to be copied to the container so you have to have a good folder structure and a base folder to run or compile your application from.
  • Entry point: This is the command that docker is going to run when creating the container, so for example, if you are running a rails application that entry point would be the rails server command.
Photo by Aron Yigin on Unsplash

Now that you have some basic knowledge let’s talk about some advanced techniques that will help you in your docker journey. To reduce the size of your images significantly you have to use multistage images. The concept behind it is that you use a bigger/heavier image to build your applications and then use a lighter image to run them. For example, for a .NET core application, you could use the SDK image to copy your project over and build it and then copy the build folder to an image that only has the runtime to make it smaller. The idea is to take as much overhead as you can from your images so they can go from over 1GB to a few hundred MB depending on your application.

There is also a tool that you can use in combination with docker to create multi-services applications, called clusters and it is called docker-compose. Docker-compose is a tool that comes with docker that allows you to set up and control several containers at the same time, allowing an application and all the necessary components to run in an isolated cluster. For example, if you need a database for your application, you don’t have to install the engine in your image, you just build the image for your application and then create a container from the official image (or any other image) for the database engine and connect your application to it. To use Docker Compose you have to create a .yml file where you specify all the elements in the cluster (services and images, volumes, networks, ports, commands, etc), you can also specify dependencies between services so they have a determined starting order in case you need one service to be up before another is started, for instance, your database has to be up before your application starts using it.

Photo by Jainath Ponnala on Unsplash

You have almost all there is to know to start using docker except for how to share images between developers or teams, well this is covered by a site called Dockerhub, and it is the backbone to share Docker images between developers or teams. It is a remote repository of images where you can find official images of the components that you are going to use (OS, framework, database engine, etc.) as well as customized non-official images built by someone else. When you choose your base image, docker pulls it from there unless it is already in your local repository.

Dockerhub is based on a tag system that allows the upload and organizing of the images, your username is part of the tag so when you are building the image make sure to use the correct tag to upload it. If you are a free user, they allow you to have one private image, all the other ones you upload must be public, but if you want to keep your images private you will have to pay for the subscription.

Photo by Gabriel Heinzer on Unsplash

Now that you have the basic knowledge to start using docker, I will give you some useful commands that you can run from your terminal in case you are running docker on an OS that doesn’t have the docker desktop, or you simply don’t want to deal with a GUI.

  • docker build: This command is used to build a docker image, it receives some options with their values and the last parameter is a path to the project or an URL. One of the most used parameters passed is the -t that is used to assign a tag to the image being built.
  • docker images: Lists all the available images in the machine, either created locally or pulled from docker hub. The most common parameter that can be passed is the -a which shows all the images even the intermediate created for the middle steps of the image build.
  • docker ps: Lists all the containers that are currently running in the machine, using this command is where you can get the container id to, later on, stop it.
  • docker run: This command creates a new container from an image and starts it for use. You have to pass the image name or tag for the image that you want to use, it also takes several options, the most used being -d to start the container in detached mode, meaning the terminal won’t be locked up by the container’s terminal, -it to go in the interactive terminal for the container (great for debugging or setting up the docker file), and -p to map a port from the host machine to a port on the container.
  • docker start: This allows you to start one or more stopped containers by passing on their ids.
  • docker stop: Stops one or more running containers using their ids.
  • docker rm: Removes one or more stopped containers.
  • docker save: It can be used to save your images into .tar files for easy transfer between systems without accessing Dockerhub. It has one parameter that is -o to specify the output file where you want your image to be.
  • docker export: It exports a container’s files system as a .tar file.

These are just a few of the most important commands to me, but if you want the full list of commands and options please refer to the Docker cli documentation.

Photo by Michael Held on Unsplash

As you can see docker is a powerful tool and can help you as a developer, by getting rid of the most dreadful problem that every programmer faces at some point in their career, I’m talking of course about the “it works on my machine” paradox. As you grow as a developer the amount of extensions and programs installed on your machine grows as well, this creates the problem that you may not know what is making your application work on your machine but not on the client’s server, and it is very difficult to figure out, in most cases, the culprit for it. That is where docker comes in handy because is a blank system that you have to install only the necessary dependencies so you control what goes into it, this can be replicated in the customer’s machine or even better you can give the image to the customer and they can run it in their servers without having to do any additional work. For more information on how to develop applications using docker, you can take a look at this very good video that explains how to build a NodeJS application using plain docker.

There you have it, this is all that I learned in my journey as a beginner into docker and I hope it helps you in some way or form to start yours. Thank you for reading this article and if you liked it don’t forget to leave a clap.

References

Docker Documentation: https://docs.docker.com/

DockerHub: https://hub.docker.com/

Docker CLI Documentation: https://docs.docker.com/engine/reference/commandline/docker/

Docker Videos: https://www.youtube.com/channel/UCwFl9Y49sWChrddQTD9QhRA

Developing with Docker: https://youtu.be/CsWoMpK3EtE

Multistage Images: https://youtu.be/2KzDMD5Qk2k

Docker Compose: https://youtu.be/0B2raYYH2fE

--

--