Docker Compact Manual (1: Basic / Basic Command)

The reason for the article

Docker, which is used as a matter of course in development However, the commands I use are limited, and there are things that should be dealt with with the mechanism of Docker. It seems to be known, but not.

Once about Docker, the learning content is summarized for each item ** Compact manual for Docker work ** I wanted to keep it as.

Post number Subtitle and access destination
1 Elementary / basic commands(* This article)
2 Mount and data persistence
3 docker-compose
4 Create a custom image

If you find any mistakes or notices, we would appreciate it if you could point them out. If you find it helpful, I would be grateful if you could ** LGTM </ font> **.

Purpose of this article

What is Docker in this article? From to basic operations are summarized.

What is Docker? What are the advantages and disadvantages?

First of all, think about what Docker is. Obviously, Docker is a typical software that realizes containers. Although there are items described in various technical books and materials, In a word, forcibly

"Providing an isolated execution environment that does not affect each other using container technology"

I think it can be said.

Benefits of Docker

--Since the environment is isolated, development is possible in each container. --Easy to copy a container and run on another server (high portability) --You can build the same environment (container) as long as you have a PC with Docker Engine installed.

Disadvantages of Docker

There are not many, but we will verify the disadvantages by comparison.

  1. Only works on Linux systems
  2. Not a complete separation. Although it has a mechanism to limit CPU usage and memory consumption so that it is not affected by other containers. It's not perfect and it's not emulated like a virtual server, so it's in Docker Engine In principle, if there is a security hole, there is a risk that the isolated part will come off.

Docker configuration

Docker is a representative software that realizes a container environment. Docker is software that runs on Linux. If you install Docker Engine on Linux, you will be able to run Docker container. On Mac and Windows, it can be executed by installing software called Docker Desktop. After installing Docker Desktop, you can install and use the Linux subsystem.

Docker container

The Docker container is the source of the container, Docker image (* It may be expressed as an image below). Obtain it from a registry such as Docker Hub or use Dockerfile Put the necessary data under the working directory and create your own customized image There are two main methods, one is to create a Docker container.


Let's summarize the operation.

Basic operation of Docker

Flow from container start to end
(Sample creation with Apache image using Docker Hub)

スクリーンショット 2020-09-22 11.30.32.png

procedure

  1. docker pull httpd (get an image from Docker Hub, which is a Docker repository) The contents of the following site.   Apache / httpd
  2. docker create (container creation)
  3. docker start (start the container)
  4. docker stop (container stop)
  5. docker rm (delete container)
  6. docker image rm (delete image)

These are a series of flows, but it is common to execute from image acquisition to operation at once with docker run. Therefore, the explanation below is based on the assumption that docker run will be used.

Docker container creation and launch options

There are some startup options that you often use when creating a docker run container.

Option name Contents
-d Detach mode. Background execution separated from the container terminal
-i Interactive mode. Container connection of standard input / output and standard error output
-t Pseudo terminal(pseudo-tty)To assign.
A terminal that supports cursor movement, character deletion, and character input such as Ctrl.
--name Give the container a name. If not specified, the container name will be random.
-p Map port numbers
(Host port number:Container port number)
-v volume:Volume refers to data that can be persisted.
Mount a volume of container data
(Host directory: Container directory)
-rm Option to destroy the container after execution is complete
It is convenient to set it as a container for test operation.
-dit, -it specification and attach / detach

--- If it is, it is connected to the Docker container (executes the program in the container) --- If it is dit, the program of Docker host is executed (the container is running behind the scenes) --Attach is a Docker container connection --Detach is a connection such as a program (shell) of the Docker host and is not connected to the Docker container. --- It can be switched to detach by pressing [CTRL] + [P], [CTRL] + [Q] in order when connecting.

If you do not add a name (it will be a random container name and difficult to manage)

スクリーンショット 2020-10-27 0.02.21.png

The behavior of the -p option

Accepts the request from the browser, Host 8080 opens, and via container 80 You'll get to Apache's public settings. スクリーンショット 2020-09-22 12.09.37.png

The behavior of the -v option

The current directory of the host is mounted on the container. (* I would like to explain the mount again in Manual 2.
This is a backup image.)
In that case, you can specify it as an environment variable with $ PWD. Example: -v" $ PWD ": / usr / local / apache2 / htdocs

スクリーンショット 2020-09-22 12.30.06.png

In other words, the command with the above explanation added is as follows.

docker run --name my-apache-app -dit -p 8080:80 -v "$PWD":/usr/local/apache2/htdocs/ httpd:2.4

Container maintenance

The following two types of commands can be used to connect to a container and perform maintenance.

command Container state State at the end of the shell Command example
docker run Stopping End of container docker run --name my-apache-app -it httpd:2.4 /bin/bash
docker exec During operation Stay in operation docker exec -it my-appache-app /bin/bash

In most cases you should use docker exec. If you execute the above command, you can put it in bash below.

docker exec -it my-apache-app bash
❯ docker exec -it my-apache-app bash
root@8c72c18e9169:/usr/local/apache2# ls -a
.  ..  bin  build  cgi-bin  conf  error  htdocs  icons	include  logs  modules

This article ends here. Thank you for reading.

Recommended Posts

Docker Compact Manual (1: Basic / Basic Command)
docker basic command
Docker Compact Manual (3: docker-compose)
Docker command
Memorandum docker command
Docker command list
Docker command memo
Docker Compact Manual (2: Mount and Data Persistence)
Docker basic commands
Docker command summary
Docker command memorandum
Docker Compact Manual (4: Create a custom image)
[Docker] Command summary
[Docker] Command list
(Git) Basic command
Docker Machine command memo
docker error delete command
docker command personal summary
Docker command cheat sheet
[Docker] Introduction of basic Docker Instruction
Decomposing the Docker run command. .. ..
Run LibreOffice Basic macros in Docker