Docker Compact Manual (4: Create a custom image)

At first

This article "Docker Compact Manual" consists of 4 items. Other articles are here.

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

Purpose of this article

If you want to create your own app, you need a customized container. It included not only the image from Docker Hub but also the data I wanted to include You have to create a custom Docker image. This time we will focus on how to create a custom Docker image.

Dockerfile best practices

Things to check before creating a custom image Docker officially publishes Dockerfile best practices. If you don't follow this best practice to some extent It may be difficult to understand, difficult to use, unmaintainable, or buggy. It is recommended to create it based on this content.

Best practices for writing Dockerfiles

Those who thought it was English: raised_hand: There is a site translated by the Japanese localization project. : relaxed: Please access from the following site.

Dockerfile best practices

One thing to note is that you should check the update history. If the update history is old, you should also check the official website.

スクリーンショット 2020-10-26 17.01.10.png

Create a custom image from a Dockerfile

There is also a method of creating a container image from a container (docker commit), It is common to store the Dockerfile and the data to be included in the image in the same working directory.

スクリーンショット 2020-10-26 19.34.51.png

Instructions that can be specified in the Dockerfile

Note In the Dockerfile, the instructions are executed line by line in order from the beginning. In other words, if the description order is changed, an error will occur or an unintended container will be created.

order Execution content
FROM Specify base image
ADD Add files and folders to the image.
If you specify a compressed file, it will be automatically expanded, which is convenient.
You can also specify a remote file outside the directory where the Dockerfile is located.
COPY Add files and folders to your image
The compressed file is not decompressed and is copied as it is
Only files in the directory where the Dockerfile is placed can be specified.
RUN Command execution when building an image
CMD Default command to execute when the container is started
(When the command to be executed by docker create or docker run is omitted)
To specify
ENTRYPOINT At the time of image execution (when docker create or docker run)
Force command execution
ONBUILD Execute arbitrary instruction when build is completed
EXPOSE Communicate the assumed communication port to the image user
VOLUME Communicate persistent data storage location to image users
ENV Environment variable definition
WORKDIR RUN,CMD,ENTRYPOINT,ADD,Specify the working directory for COPY
SHELL Shell specification at build time
LABEL Name / version number / manufacturer information setting
USER RUN,CMD,The user who executes the command specified by ENTRYPOINT
Set the group (no USER specified is root)
ARG docker buildDeclare the arguments that can be specified when
STOPSIGNAL For the program running in the container when docker stop
Change the signal to send
(The rule is SIGTERM)
HEALTHCHECK Customize the health check method to check the life and death of the container

Easy to get hooked on part 1

At what point is RUN, CMD, ENTRYPOINT running? ?? I made a mistake too. I think this should be remembered in the figure. スクリーンショット 2020-10-26 20.58.54.png

--RUN is the stage of image creation --CMD, ENTRYPOINT is the stage of container startup

Easy to get hooked on part 2

Proper use of CMD and ENTRY POINT How should I use them properly? ??

** Conclusion ** Use CMD in most cases. When using CMD, if the user explicitly writes a command, that will take precedence. For example, if a bug occurs in a custom container, if it is specified by ENTRYPOINT It cannot be changed because the forced command is executed. That bug will continue to occur. On the other hand, CMD allows the user to choose to leave it to the default command or execute an arbitrary command.

Sample example

Custom samples in Rails are also available. You can see that it is different from the instruction in the official image of Docker Hub.

Docker Hub Official Rails

FROM ruby:2.3

RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        postgresql-client \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /usr/src/app
COPY Gemfile* ./
RUN bundle install
COPY . .

EXPOSE 3000
CMD ["rails", "server", "-b", "0.0.0.0"]

Quickstart: Compose and Rails

FROM ruby:2.5
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp

# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000

# Start the main process.
CMD ["rails", "server", "-b", "0.0.0.0"]

Even so, the Ruby version of Docker Hub is 2.3, and Quickstart is also 2.5. Too old ❗️❗️ I would like it to be version 2.6 or later.

So, to build a modern Rails container environment, customize the contents of Quickstart further. I think you should do it.

** Promotion ** Those who wondered if it would be easier to build a modern development environment with Rails Please visit this site I made.

Manual for building a stable development environment for "Rails6" with "Docker-compose"

Finally

I posted Docker in 4 copies. Thank you very much for those who have been with us until the end. I would appreciate it if you could use it as a reference ** LGTM </ font> **.

If you have any mistakes, please let us know. We also received advice from many people. I would like to thank you here.

Reference materials / reference source site

WEB

Books

-[Docker / Kubernetes Practical Container Development Introduction (Technical Review)](https://www.amazon.co.jp/Docker-Kubernetes-%E5%AE%9F%E8%B7%B5%E3%82%B3 % E3% 83% B3% E3% 83% 86% E3% 83% 8A% E9% 96% 8B% E7% 99% BA% E5% 85% A5% E9% 96% 80-% E5% B1% B1% E7% 94% B0-% E6% 98% 8E% E6% 86% B2 / dp / 4297100339 / ref = sr_1_1? __mk_ja_JP =% E3% 82% AB% E3% 82% BF% E3% 82% AB% E3% 83% 8A & dchild = 1 & keywords = Docker% 2FKubernetes +% E5% AE% 9F% E8% B7% B5% E3% 82% B3% E3% 83% B3% E3% 83% 86% E3% 83% 8A% E9% 96% 8B% E7% 99% BA% E5% 85% A5% E9% 96% 80 & qid = 1603714747 & sr = 8-1) -[Touch and learn cloud infrastructure docker Container construction from the basics (Nikkei BP)](https://www.amazon.co.jp/%E3%81%95%E3%82%8F%E3%81%A3% E3% 81% A6% E5% AD% A6% E3% 81% B6% E3% 82% AF% E3% 83% A9% E3% 82% A6% E3% 83% 89% E3% 82% A4% E3% 83% B3% E3% 83% 95% E3% 83% A9-docker% E5% 9F% BA% E7% A4% 8E% E3% 81% 8B% E3% 82% 89% E3% 81% AE% E3% 82% B3% E3% 83% B3% E3% 83% 86% E3% 83% 8A% E6% A7% 8B% E7% AF% 89-% E5% A4% A7% E6% BE% A4-% E6% 96% 87% E5% AD% 9D-ebook / dp / B089VZXX63 / ref = sr_1_1? __mk_ja_JP =% E3% 82% AB% E3% 82% BF% E3% 82% AB% E3% 83% 8A & dchild = 1 & keywords =% E3% 81% 95% E3% 82% 8F% E3% 81% A3% E3% 81% A6% E5% AD% A6% E3% 81% B6% E3% 82% AF% E3% 83% A9% E3% 82% A6% E3% 83% 89% E3% 82% A4% E3% 83% B3% E3% 83% 95% E3% 83% A9 + docker +% E5% 9F% BA% E7% A4% 8E% E3% 81% 8B% E3% 82% 89% E3% 81% AE% E3% 82% B3% E3% 83% B3% E3% 83% 86% E3% 83% 8A% E6% A7% 8B% E7% AF% 89 & qid = 1603714817 & sr = 8-1)

Recommended Posts

Docker Compact Manual (4: Create a custom image)
Create a lightweight STNS Docker image
Docker Compact Manual (3: docker-compose)
Create a docker image that runs a simple Java app
Docker Compact Manual (1: Basic / Basic Command)
Create a Docker image with the Oracle JDK installed (yum
Create a Vue3 environment with Docker!
Run lambda with custom docker image
2. Create Docker image and register Registry
[Kotlin / Android] Create a custom view
Use Jenkins to build inside Docker and then create a Docker image.
Create a Docker Image for redoc-cli and register it on Docker Hub
Create a web environment quickly using Docker
Create a MySQL environment with Docker from 0-> 1
How to create a small docker image of openjdk 11 (ea) application (1GB → 85MB)
[Memo] Create a CentOS 8 environment easily with Docker
Create a Privoxy + Tor environment instantly using Docker
Create a Lambda Container Image based on Java 15
Let's create a custom tab view in SwiftUI 2.0
[Introduction to Docker] Create a Docker image for machine learning and use Jupyter notebook
Create a docker environment for Oracle 11g XE
Create a Spring Boot development environment with docker
I tried using Wercker to create and publish a Docker image that launches GlassFish 5.
I made a Docker image of SDAPS for Japanese
How to deploy to Heroku from a local docker image
Create a Docker container to convert EPS to PGF source
[Note] Create a java environment from scratch with docker
Docker Compact Manual (3: docker-compose)
Docker Compact Manual (1: Basic / Basic Command)
Docker Compact Manual (4: Create a custom image)
CI/CD pipeline and Docker
Docker installation and initialization
Docker terms and commands
[Java] Create a filter
Delete unused docker image
Maybe it works! Create an image with Docker and share it!
How to create a lightweight container image for Java apps
[Personal notes] How to push a Docker image to GitHub Packages
Create a Kibana container image for ARM64 (Raspberry Pi/Mac M1)
I tried to create a padrino development environment with Docker