All you need to know about Docker, Docker-Compose and Dockerfile.

A detailed explanation of dockerization

Rihab Feki
4 min readAug 29, 2021
Photo by Ian Taylor on Unsplash

Dockerizing is the process of packing, deploying, and running applications using Docker containers.

Docker provides the ability to package and run an application in an isolated environment called a container. Containers are lightweight and contain everything needed to run the application, so you do not need to rely on what is currently installed on the host. You can easily share containers, and be sure that everyone you share with gets the same container that works in the same way.

In this article I will cover the following:

  • What is Docker
  • What is Docker-Compose and what it is used for
  • What is a Dockerfile and what it consists of
  • How to install Docker
  • Best practices for building Docker images

What is Docker?

Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and deploy it as one package.

Docker is what enables you to run, create and manage containers on a single operating system.

What is Docker-compose and what is it used for?

Docker compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration. Docker compose lets you run all your services at once (also in the right order) and manage them via a unified interface.

Docker compose in general will contain:
Services: Services are the list of individual docker containers that will be run by the compose tool. We will specify the ports and other configurations needed to run the docker containers here.
Networks: Network provides a way by which different services can interact with each other. Each container can attach itself to a network and all containers within the same network can communicate with each other.
Volumes: Docker containers by default do not contain any kind of persistence storage. If a docker container is killed then all the data in its memory gets lost. So in order to save some persistent data you need volumes.

Check this link on Docker-Compose cheat sheet

What is a Dockerfile and what does it consists of?

A Dockerfile is a file that contains instructions on what and how to build a docker image. This is an automated process that operate by building the images based on layer or steps hence order is very important in creating a Dockerfile. The Dockerfile is a file without any extension.

The basic set of instructions for any Dockerfile include:

FROM <image>: this is from where you want to build the base image or operating system.

COPY <source> <destination> :For copying files from one place to the image.

WORKDIR <directory>: this shows the working directory we will be setting up within our image

RUN <command>: this command tells the docker daemon to run a set of commands or programs either via

  • shell form: RUN pip install -r requirements.txt
  • exec form: RUN [“pip”,”install”,”-r”,”requirements.txt”]

EXPOSE <port>: This informs Docker that the container is exposed to listen on the specified port at runtime.

ENTRYPOINT [commands] : Allows for configuring the container as an executable.The entrypoint is similar to the CMD in that they serve as a point for what command to run in the container not the image, but rather when you create a container from an image.

CMD <commands>: this is used to provide defaults for an executing container. You can use the shell form or the exec form . When you use both together it is recommended to use it in the exec form that follows the JSON array format.

How to install Docker?

  • To install Docker on Windows follow the instructions here
  • To install Docker on Mac follow the instructions here
  • To install Docker on Linux follow the instructions here

Best practices for building Docker images

  • Use a solid base image: When you build containers on top of an unmaintained or untrusted image, you may inherit all the vulnerabilities of that image. So as always check the
  • Include a.dockerignore: In the root directory of the context, having a .dockerignore will prevent undesirable files and directories to be contained in the image when running the docker build command. to find out more about the syntax of such file check this link.
  • Avoid installing unnecessary dependencies: It might be tempting to install more than you need but to decrease the image’s size and complexity, just install what is actually required.
  • Reduce the numbers of layers in your image: Each instruction in the Dockerfile adds a layer to the image.

Docker Hub is a service provided by Docker for finding and sharing container images with your team. It is the world’s largest repository of container images with an array of content sources including container community developers, open source projects and independent software vendors (ISV) building and distributing their code in containers.

Docker dashboard

The Docker Dashboard provides a simple interface that enables you to manage your containers, applications, and images directly from your machine without having to use the CLI to perform core actions. The Containers/Apps view provides a runtime view of all your containers and applications.

Image by the author

I hope this article was useful and stay tuned for the next article ;)

Let me know if you have questions or suggestions in the comments.

By Rihab Feki

--

--