Basic docker (P1) – image and container

Tram Ho

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.

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.

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.

  • 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:

Check that the image is used: List of images:

Delete an 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.

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

See all running and stopped containers

$ docker ps -a

Delete a container:

… (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 !!

Share the news now

Source : Viblo