Hello everyone, today I would like to return to an article about the development environment when developing apps. And I would like to share a little knowledge about Docker – a great tool for developing or scaling (expanding) applications. With a little bit of my knowledge, I hope to help you to understand a little of the basics and start using Docker. Let’s go to the article together!
1. Introduction
With normal application development, we often run directly on the real machine (host) to build the application. And an application we not only have one service but also need many services or packages of different languages to be able to build completely. For each service, package we have to meticulously install to ensure that each version of the package or language is used in order to be compatible and work smoothly.
Whenever handing over to customers or other developers for application maintenance, they will have to reinstall each of those packages, but cannot avoid installing the wrong version or confict, and it takes too much time to set. -up environment. The birth of Docker has solved that problem, this tool gives us the convenience when all the essentials of the application are encapsulated (images) and containerization (in the way of implementing docker containers) to application maintain and build are faster than ever.
Architecture of docker:
Docker works on client-server architectures.
- Client is where users interact to send requests to Docker Daemon (DOCKER_HOST) through CLI (Command line interface) to perform operations such as build, pull, run images.
- Server: The Docker daemon listens for requests from clients and performs those operations, managing images, containers, networks, and volumes. Docker daemon.
- In the picture we have a Registry: this is a place to store the built-in images and currently has a public storage for everyone, which is the docker hub.
2. Installation.
- First to use it, we need to install docker. The home page also has installation instructions, so I do not detail each step, please see this link: https://docs.docker.com/engine/install/ubuntu/
3. Basic concepts (image and container).
3.1. Images
- Images is a read-only file that cannot be changed, it contains the libraries, tools, services or packages, configurations to run and necessary to build the application. We also cannot start or run images like containers and can create images for ourselves or take public images on the registry to download and customize into an image containing necessary tools for themselves.
To create an image, we need to create a Docker File. Docker relies on this file to use and build images. The docker file contains all of the instructions for the docker to build an image. Now we will demo to create an image containing ubuntu.
1 2 3 4 5 6 7 8 | FROM ubuntu:18.04 LABEL maintainer=" <a class="__cf_email__" href="/cdn-cgi/l/email-protection">[email protected]</a> " RUN apt-get update && apt-get -y install apache2 EXPOSE 80 |
Above I have a simple Dockerfile file to build an image of ubuntu and install apache2 on. In this file we see the following instructions:
- FROM : This is the place to declare the image base, I get the ubuntu version: 18.04 to use.
- LABEL : a place that gives metadata or image information, it can be version, maintainer …
- RUN : This will execute the command on the top layer of images. ie once we create a new image, now we wish we are on a new layer. In short it will execute the command when building images.
- EXPOSE : normally apache2 runs on gong 80. So we will use port 80 on ubuntu can run apache2. And when we want to use it, we need to append the -p tag and assign the port to them when creating the container from this image
Also in Dockerfile we also have
- CMD : CMD is used to execute command when we are using images or start container.
1 2 3 4 | CMD ["executable","param1","param2"] CMD ["param1","param2"] (sử dụng cho việc truyền tham số cho ENTRYPOINT) CMD command param1 param2 |
- ENTRYPOINT : ENTRYPOINT is often used to execute many statements during the start of the container. Usually in ENTRYPOINT will choose the shell script file to get the command to be executed.
- WORKDIR: used to define the working directory for the container. Any commands while executing RUN, CMD, … will be executed in this directory.
- In addition, we also have other instructions such as ADD, COPY, … I have linked the homepage in more detail here: https://docs.docker.com/engine/reference/builder/
After configuring this Dockerfile file, we run the following command to build the image:
1 2 | $ docker build -t docker-apache2 . |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | Sending build context to Docker daemon 2.56kB Step 1/4 : FROM ubuntu:18.04 18.04: Pulling from library/ubuntu 92dc2a97ff99: Pull complete be13a9d27eb8: Pull complete c8299583700a: Pull complete Digest: sha256:4bc3ae6596938cb0d9e5ac51a1152ec9dcac2a1c50829c74abd9c4361e321b26 Status: Downloaded newer image for ubuntu:18.04 ---> 329ed837d508 Step 2/4 : LABEL maintainer=" <a class="__cf_email__" href="/cdn-cgi/l/email-protection">[email protected]</a> " ---> Running in 64bef33f08eb Removing intermediate container 64bef33f08eb ---> 93afda6024d0 Step 3/4 : RUN apt-get update && apt-get -y install apache2 ... Removing intermediate container 000eeb497905 ---> 2efcc7312083 Step 4/4 : EXPOSE 80 ---> Running in 9798427c060c Removing intermediate container 9798427c060c ---> 9289f9d24416 Successfully built 9289f9d24416 Successfully tagged docker-apache2:latest |
Check that the image is used: List of images:
1 2 | $ docker images -a |
1 2 3 4 5 | REPOSITORY TAG IMAGE ID CREATED SIZE docker-apache2 latest 9289f9d24416 3 hours ago 195MB <none> <none> 2efcc7312083 3 hours ago 195MB |
Delete an image:
1 2 | $ docker rmi <id_image | name_image> |
1 2 | $ docker image rm <id_image | name_image> |
So we have successfully built an image. So what does this image do, we will continue with the container section.
3.2. Container
To use the Docker utility, the most important part that is indispensable with Images is the Container. We have the image repackaging the necessary tools. So to use images we need to run the container to use that image.
1 2 | $ docker run -it --name "apachelinux" -p 8080:80 docker-apache2 |
With “apachelinux” being the name of the container we set the option.
- -p represents the port we configure post on the real machine and assign it to the container. My container uses the image docker-apache2 just created above with port 80. I mapped port 8080 on the real machine (host) to port 80 on docker.
- -it: to run the container and use it with the terminal.
Then we will get into the container using the ubuntu image and try to run service apache2 start
and get the results:
Note: If you type exit in terminal, the container will stop. So if you want to exit the terminal but the container is still running, everyone press Ctrl + p, Ctrl + q.
To see the running containers we use:
$ docker ps
1 2 3 | CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES def3ba1d8a87 docker-apache2 "/bin/bash" 12 minutes ago Up 12 minutes 0.0.0.0:8080->80/tcp apachelinux |
See all running and stopped containers
$ docker ps -a
1 2 3 4 | CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES def3ba1d8a87 docker-apache2 "/bin/bash" 12 minutes ago Up 12 minutes 0.0.0.0:8080->80/tcp apachelinux 20c8aa9108b1 ubuntu-nginx "/build/start.sh /bi…" 9 days ago Exited (0) 9 days ago intelligent_wozniak |
Delete a container:
1 2 | $ docker rm <id_container | name_container> |
1 2 | $ docker image rm <id_image | name_image> |
… (there’s more)
4. Summary.
With this article, I introduced some basic and important knowledge in docker as well as some commonly used commands with docker. Hopefully with this knowledge people can continue to develop their skills with docker to easily work on large projects that need to use it. See you in my further winch posts on the topic of docker. Thanks for reading !!