Docker tutorial series – part 2

In this article, I will write about “data mount (volume), Docker Network, Docker Compose”. Now, let’s go Docker tutorial series – part 2.

Data management in Docker

Dynamic data to be handled in the started container can be placed in the top layer (container layer) that can be read and written.

  • When the container is deleted, data in the container disappears.
  • Can not share data among containers.
  • Since writing to the container layer uses a union file system different from the normal file system, the writing speed is slow.

Disadvantage

Docker manages data on the host machine and mounts it on the container is used.

There are three main methods, which I will explain below.

volume

A method of mounting the specified directory (/var/lib/docker/volumes) which is automatically generated on the host machine to the container

On the host machine

$ docker volume create [volume name]

By doing so, When you create a volume (/var/lib/docker/volumes directory) and start up the container.

For example
$ docker run -itd -name mount-test --mount source=volume1,target=/app nginx

You can mount the specified volume by attaching the –mount option (it can also be the -v option) as shown in.

The mounted volumes directory should not be directly manipulated from the directory on the host from which it is mounted.

Even different containers that are set up on the same host can share files by mounting the same volume respectively.

Furthermore, if you want to share volume with multiple containers, you can also set edit permission for each container.

$ docker run -itd -name mount-c4 --mount source=copy-vol,destination=/etc/nginx,readonly nginx

volume management command

Check volume list

$ docker volume ls

Check volume details

$ docker volume inspect [volume name]

Delete volume

$ docker volume rm [volume name]

Note that deleting the container will keep the volume, so you need to delete it with the $docker volume rm [volume name].

bind mount

Bind mount is a mount method different from the volume, in that you can mount arbitrary directories on the host machine and directly manipulate directories on the host side.

It is not necessary to set it in advance like volume, but mount it by specifying it as an option at container startup with the following command.

For example:
$ docker run -itd -name bind-mount-test --mount type=bind,source="$ (pwd)"/mount,target=/app nginx

If the source (mount source) directory does not exist, an error will occur, so let’s make it in advance. (It is automatically created when using the -v option)

if you mount an empty directory on the host. on the container, data on the container will disappear and the container may not work properly, so be careful.

tmpfs (tempfs)

A method of mounting the memory area of the host machine on the container

Whether the host machine is terminated or when the container is closed, the held data is released.

For example:
$ docker run --itd --name tmpfs-test --mount type=tmpfs,destination=/app nginx

You can mount by specifying tmpfs as the type of the -mount option at the time of container startup.

Also, in order to eliminate the possibility of unlimited use of memory on the host,

$ docker run --itd --name tmpfs-test --mount type=tmpfs,destination=/app,tmpfs-size=100000000,tmpfs-mode=800 nginx

You can also limit the memory size that can be used as an option.

Docker network

we will explain here the method of communicating between multiple containers launched.

For example, when operating a web page, if you start up a WordPress container and a MySQL container, the WordPress container needs to communicate with the MySQL container.

As a method of inter-container communication here, it is the Docker network that connects and communicates with multiple containers.

The Docker network has three networks that exist by default and networks that you define yourself.

Bridge network

In the existing network by default, the created container is connected by default to the bridge network.

In the Bridge network, you can communicate with a container that exists in the same network by specifying an IP address.
However, in the Bridge network, since DNS is not defined, the container name can no communicate with other containers.

In other words, it is possible to communicate by specifying an IP address among containers launched without specifying Network, but it is impossible to communicate by specifying a container name.

Although it is a network that is not exposed to the outside in the default state, it can be accessed from the outside by releasing the port specified by the -p option.

Host network

The host network is the network that exists by default with the host driver.

The connected container will have the same network settings as the docker host.

Therefore, it is possible to connect to the container by connecting to the Docker host’s IP number 80 only by starting the container without setting the -p option at the container startup and opening the port to the outside like the bridge network I will.

None Network

It is the default existing network, and the connected container has no network interface. When connecting to a network, it is necessary to be disconnected from all other networks.

Docker create

By creating your own network, you can communicate between containers by container name.

$ docker network create [network name]

Create a new network with (Default driver is a bridge)

$ docker network connect [Network name to connect to] [Container name]

Docker Daemon’s built-in DNS works on the user-defined unique network resolve names by container name and associates with IP, so you can connect to other containers by container name.

Network commands

Check the network list

$ docker network ls

Check network details

$ docker network inspect [network name]

Create a new network

$ docker network create [network name]

The default driver is bridge

Connect a container to a network

$ docker network connect[ Network name to connect to] [Container name]

Disconnect container from a network

$ docker network disconnect Network name container name

Docker Compose

Docker Compose is a tool for predefining and running multiple container Docker applications. So, Docker Compose is able to automate them.

When building the execution environment of a Web service with Docker, by writing the definitions of the Web server, DB server, Cache server, etc. in one docker-compose.yml file, based on that, the container required for execution You can start and set all at once.

The procedure is as follows.

  • Prepare a Dockerfile or prepare an image to use for Docker Hub etc.
  • Define docker-compose.yml
  • Execute $ docker-compose up in the directory containing the yml file

docker-compose command list

Display a list of containers started with docker-compose

$ docker-compose ps

Container activation from the setting described in docker-compose.yml

$ docker-compose up

It restarts even if the container is already running

Delete containers and networks created with docker-compose

$ docker-compose down
# $ docker-compose down -v

The volume is also deleted together with the -v option

Execute command within specified service container

$ docker-compose run [container name] [command]

A series of container stops

$ docker-compose stop

A series of container activation

$ docker-compose start

Docker tutorial series – part 1

About HuuPV

My name is Huu. I love technology, especially Devops Skill such as Docker, vagrant, git, and so forth. I like open-sources, so I created DevopsRoles.com to share the knowledge I have acquired. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

1 thought on “Docker tutorial series – part 2

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.