This page looks best with JavaScript enabled

Playing Around with Docker

 ·  ☕ 7 min read

Building a server for website needs like installing database, backend, web tools takes a lot of time. But it would be simpler… with docker.

Overview

Docker is an open platform for developing, shipping, and running applications. If you know about virtualization, then it’s close, but not quite the same. With docker you can separate your applications from your infrastructure so you can deliver software quickly.
For example, if you’d ever been preparing your server by installing mysql database, php engine, apache2 or nginx server and then configuring them one by one. But it just for development server usage. Then you need to do the same things again for production server. Well, that’s a lot of job and time right? With docker you can make it simpler, create/pull the needed image, run the container alongside with your application! But it is not that simple as you read it, you need to understand the core component, what is image, container, network, ad volume. Here I’ll try to explain em’ the simple way 😉 (I hope it simple…)

Install Docker

  • For Mac, Windows, and Linux x86_64/amd64 you can install docker desktop, the fastest way, easy, and with GUI support. You can download the installer here and follow the installation process.
  • For Linux x86_64, especially for Linux arm64/aarch64 which is not yet supported in docker desktop, you can install docker engine which is the core of docker desktop. Most of the installation using terminal command, you can follow it here.

Explanation First, Here It Is

Official Way

For more information you can read it from docker official docs.

You can see below is docker architecture diagram
Docker architecture

  • Docker daemon, The Docker daemon (dockerd) listens for Docker API requests and manages Docker objects such as images, containers, networks, and volumes.
  • Docker client, The Docker client (docker) is the primary way that many Docker users interact with Docker. When you use commands such as docker run, the client sends these commands to dockerd.
  • Docker registries, A Docker registry stores Docker images. Docker Hub is a public registry that anyone can use, and Docker is configured to look for images on Docker Hub by default. You can even run your own private registry. When you use the docker pull or docker run commands, the required images are pulled from your configured registry.
  • Images, An image is a read-only template with instructions for creating a Docker container. Often, an image is based on another image, with some additional customization. For example, you may build an image which is based on the ubuntu image, but installs the Apache web server and your application, as well as the configuration details needed to make your application run.
  • Containers, A container is a runnable instance of an image. You can create, start, stop, move, or delete a container using the Docker API or CLI. You can connect a container to one or more networks, attach storage to it, or even create a new image based on its current state.

My Explanation

d o d d c o o k c c c e l k k r i e e e r r n n e t p r t u u w l n o l r k i m u a r a b l e g u p d e n i i s t n s u e h o s t c o n c c c t o o o a n n n i 1 2 3 n e r s n e t w o r k A

I’ll try to explain it in easy and understandable.
After you install docker you can see diagram image above. There are client and host. Host it self is the main docker which has images, container, network, etc. And to control/interact the host it self there is client that can send your commands to docker like docker pull, docker run, docker network, etc.
Next, the common objects in docker are image, container, volume, network.

  • image, you can think of this image as blueprint. This image can be pulled from docker registries. The default docker registries is docker hub. Some common and basic image are ubuntu, debian, alpine, nginx, mariadb, busybox, node, php, etc.
  • container, is a runnable instance of an image. Think it as virtual OS in VMWare, you can operate and use the ubuntu OS in VMWare as you wish. So here is the same, back to the container of docker, you can start, stop, restart, delete the container. Also you can go inside and run command in the container with console, by docker exec ....
  • volume, container it self is containerized/isolated from others and it host, as well as the volume(you can think it as storage). With docker volume you can manage the container volume/storage. Somethink like linking host directory as containers volume directory.
  • network, with docker network you can communicate between containers.

Now, with this part explanation you can get a conclusion that with docker you need to pull an image which can be run as instance called container. Within the container we have volume that you can think it as storage that container can interact with. Like storing file or writing text to txt file. To make container be communicatable with others you need to put containers in the same network.

Simple Example

assumption you use docker over terminal command. But most usage of docker is by running command, so it’s OK 👌.

Here, I’ll create web server with nginx and php-fpm.
nginx is a web server that handles the HTTP requests.
Next php version we’ll be use is the FPM version. PHP runs as a separated service when using PHP-FPM. So that the Nginx web server only handles the HTTP requests and PHP-FPM interprets the PHP code.

  • In linux host (mine is ubuntu), I create a super simple project in a directory. For example, create php file in /home/user/test/index.php. Also create nginx config file in /home/user/test/default.conf. This is just for testing our docker container purpose.

    1
    2
    3
    
    <?php
    echo phpinfo();
    ?>
    
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    
    server {
        index index.php index.html;
        server_name localhost;
        error_log  /var/log/nginx/error.log;
        access_log /var/log/nginx/access.log;
        root /var/www/html;
        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass dockphp:9000;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_path_info;
        }
    }
    
  • Next, keep focus on docker command. To pull php-fpm & nginx images, run below command:
    pull php variant 8-fpm docker pull php:8-fpm
    pull nginx docker pull nginx

  • To make our containers can communicate each other, lets create new network with docker network create mynetwork

  • Run docker container based on images we have pulled before.

    1
    
    docker run -d --name dockphp --network mynetwork --network-alias dockphp -v /home/user/test:/var/www/html php:8-fpm
    

    Command above create php-fpm container with instance name dockphp. -d option means detach, run container in background. --network option used to connect container in mynetwork and with --network-alias name dockphp. With --network-alias option you don’t need to call container url with ip (ex: 172.0.0.12) but you can access it with alias, n this case you can access it with just curl dockphp instead curl 172.0.0.12 from other container. Next -v option means volume, bind mount a volume into container. Here we bind mount /home/user/test directory in the host to /var/www/html in the container.

    1
    
    docker run -d --name docknginx --network mynetwork --network-alias docknginx -p 8086:80 -v /home/user/test:/var/www/html -v /home/user/test/default.conf:/etc/nginx/conf.d/default.conf -w /var/www/html nginx
    

    Next we create another container for nginx. Here we use the same network named mynetwork. Then with -p option we publish port 80 in container as port 8086 in public. Similar as port forwading, you can access nginx container port 80 as localhost:8086 from host. Also, we bind file /home/user/test/default.conf into /etc/nginx/conf.d/default.conf in the nginx container. This file is to setting up nginx configuration. And optional -w option to setup main workspace.

  • [optional] You can get into php container bash command with docker exec -it dockphp bash and then do want you wanna do 😉.

Conclusion

In this very simple example but I try to explain it until communication between containers in a network. We can take conclusion as below:

  • Pulling image by docker pull ...
  • Creating network by docker network create ...
  • Running container by docker run ....
  • Also optional here but nice to have, get into container bash command by docker exec -it [container] bash. If you want to sh then change bash with sh.



At last, thank you for reading this article until the last paragraph 😁. Hope you can get more knowledge after reading this. And, see you all in next……… update from me 👋

Share on

AlextraPixel
WRITTEN BY
AlextraPixel
DIY Anywhere - Share any knowledge & Experience for everyone