Containerization with Dockers

Cahit Barkin Ozer
7 min readDec 12, 2021

--

This article is an introduction to Docker containerization. Let’s get started learning about software project deployment.

KablosuzKedi’s Turkish Docker document is the source idea of this article. You can check their Youtube channel and Github, and show your appreciation if you are interested.[1]

What is Docker?

Docker is a collection of platform-as-a-service (PaaS) tools that provide software in containers using OS-level virtualization.[2]

Why would I use Docker?

If your program works on your device but not on another, Docker is the way to go. Alternatively, you may be working alone on a project and have set up various databases and technologies for the project, which you then erased when the project was completed, but the data left behind slow down your device, forcing you to format your computer after a while. Docker places the images of the programs in containers, which you then delete and format when you’re finished. Docker simplifies deployment and reduces errors. And there are plenty of other usage scenarios…[1]

Virtual Machine vs Containers

Containerization is not a new concept. Let’s look at the differences between virtual machines and containers.

Virtual machine vs containers [3]
Docker’s advantages [4]

Advantages of Docker

Cost-effectiveness with fast deployment, mobility (ability to run anywhere), repeatability and automation, test, roll back option, flexibility, easier collaboration, modularity, and scalability.[5]

Disadvantages of Docker[6]

Containers don’t run at bare-metal speed, the container ecosystem is fragmented, permanent data storage is difficult, graphical applications don’t perform effectively, and containers aren’t suitable for all applications.[6]

Docker commands

Docker is primarily used from a command-line interface. The image of an application only works on containers, not on the applications themselves. A Docker image is a read-only template that gives instructions for building a Docker container.[1]

To download the image of the application[1]

docker pull <image-name>

To check if the image of the application exists on the container if it does not it downloads it from the remote repository[1]

docker run <image-name>

The container is up (working)[1]

docker run -it <image-name>

To Show the up containers[1]

docker ps

To Show previously run containers (ps: process, a: all)[1]

docker ps -a

To show active containers[1]

docker container ls

To exit(kills containers)[1]

exit

To name a container[1]

docker run -it --name <my-image-name> <image-name>

To run a named container(works, immediately closes, and keeps working in the background).[1]

docker start <my-image-name>

To stop a named container working in the background[1]

docker stop <my-image-name>

Note: You can also use containerID instead of the container name.

To delete container[1]

docker rm <my-image-name>

To delete all not-running containers[1]

docker container prune

You can remove multiple containers in a single command[1]

docker rm <my-image-name1> <my-image-name2> <my-image-name3>

Or you can remove all of the containers[1]

docker container rm $(docker container ls -aq)

To see all containers[1]

docker container rm $(docker container ls -aq)

To show docker images list[1]

docker images

To delete the image from the image list[1]

docker rmi <my-image-name>

Note: To delete an image, there must be no containers using that image. Therefore, you must first delete all the containers attached to this image and then run this command.[1]

Attach and Detach Mode

Because the logs from an image are printed to the terminal when it is executed, you can’t add commands to that container. This occurs when you’re in attach mode. Detach mode is the term when we make these logs run in the background.[1]

To show container ID of the running container in detach mode[1]

docker run -d <my-image-name>

To Re-attach container[1]

docker attach <imageID>

Note: It is enough to define the first few characters of the ID information[1]

Tag: An image’s version is called Tag.[1]

docker run <my-image-name>:<tag>

To show logs printed at the background when the image is in detached mode[1]

docker container logs <imageID>

To quit this mode[1]

docker stop <imageID>

Interactive Terminal

If your application needs to interact with the user/waits for the command you need to express it or else the program runs and finishes then the container shuts down.[1]

docker run -it <imageID>

Port Mapping

Containers with images operating inside them remain in your Docker host. Inside port numbers are all set to default. To access these containers from a browser by entering the URL, you must first define outside port numbers for them.[1]

docker run -p <outside-port>:<inside-port> <image-name>

For example

docker run -p 80:27017 mongo

On Docker Host, containers are stateless, which means no data is saved on them, and if you stop a container, all data saved on it is permanently wiped. We don’t want it to be like this, thus volume is used. [1]

Volume Mapping

Mapping containers location on the host location. When we boot a container, we give the address of the folder to be saved on the docker host, ensuring that the data on the container is saved. [1]

docker run -v /opt/data:/data/db <image-name>

To check container details[1]

docker inspect <image-id>

Docker link

When we run a container, we need to acquire the name of the container to connect them. [1]

Alias to be used in the code: Indicates a change in the location where the connection is made using the code. [1]

docker run -p <inside-port>:<outside-port> --link <name-of-container-to-be-connected>:<Alias-to-be-used-in-the-code> <image-name>

Docker Networks

Bridge(default): Containers have sequential IP addresses.[1]

docker run <image-id>

None: Not reachable from inside or outside (for example a logger container).[1]

docker run <image-id> — network=none

Host: Containers are reachable from Docker Host.[1]

docker run <image-id> — network=host

User-Defined:[1]

docker network create — driver bridge — subnet 182.18.0.0/16  gateway 182.18.0.1 <image-name>

To list networks[1]

docker network ls

Docker File

A Dockerfile is a text file containing all of the commands that may be used to construct an image from the command line. To do so, we create a “Dockerfile” file in our application folder.[1]

To define the OS of our terminal[1]

FROM <image-name>:<Tag>

To define commands that will be run on our terminal[1]

RUN <cmd-command>

To copy files from a specific location into a Docker image [1]

COPY <folder-directory>

To copy all files [1]

COPY ..

To define the working directory for the below commands(so you do not have to explicitly give the directory of the folder but just type folder name). [1]

WORKDIR <folder-directory>

To specify the instruction that is to be executed when a Docker container starts [1]

CMD["<command>","<folder-directory>"];

To run Dockerfile from terminal [1]

docker build .

To provide default values for your future environment variables [1]

ENV channel=kablosuzKedi

To set executables that will always run when the container is initiated [1]

ENTRYPOINT["<command>","<folder-directory>"];

To define port [1]

EXPOSE <port-number>

Note: ENTRYPOINT should be defined when using the container as an executable. [1]

Dockerignore

To not transfer specified files create a .dockerignore file,

and write “<file-name>/” inside that file. [1]

Docker Compose

When you have a lot of services, it can take a long time to run and bind them all. Docker Compose is used to automate this process.[1]

Create a file with the name “docker-compose.yml”. YAML is a data serialization language just like JSON and XML. [1]

To define version[1]

version:<version-number>

To define services[1]

services: 
<service-name>:

To define the container name of a service[1]

<service-name>:
container_name: <container-name>

To define the build of a service[1]

<service-name>:
build: .

To define port of a service[1]

<service-name>:
ports: -<outside-port>:<inside-port>

To define the image of a service[1]

<service-name>:
image: <image-name>

To define the volume of a service[1]

<service-name>:
volumes: -<folder-name>:<folder-directory>

You also need to add volume for services.[1]

volumes: <folder-name>:

Restart after any action[1]

restart: always

To give environment data of our service[1]

environment:

For example[1]

environment:
MYSQL_DATABASE: blogdb
MYSQL_USER:bloguser
MYSQL_PASSWORD:test123

Please do not forget to clap if you enjoyed my article. Thank you.

References

[1]gkandemi,(Oct 26 2021),docker:

[2]Wikipedia,(11 December 2021), Docker

[3]MaeveWorks,(JANUARY 16, 2020), A Practical Guide to Choosing between Docker COntainers and VMs:

[4]Anjali Nair,(2018), What is Docker and what are the advantages?

[5]Gregory Atkins,(Sep 11, 2020), Docker COntainers: Top 7 Benefits of Docker Containerization:

[6]Cristopher Mazzi, (May 26, 2017), Docker Downsides: COntainer COns to Consider before adopting Docker:

--

--

Cahit Barkin Ozer
Cahit Barkin Ozer

Written by Cahit Barkin Ozer

Üretken YZ başta olmak üzere teknoloji alanındaki yenilikleri öğrenip sizlerle paylaşıyorum. Youtube Kanalım: https://www.youtube.com/@cbarkinozer

No responses yet