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) |
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.
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.
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.
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.
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 build Declare 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 |
At what point is RUN, CMD, ENTRYPOINT running? ?? I made a mistake too. I think this should be remembered in the figure.
--RUN is the stage of image creation --CMD, ENTRYPOINT is the stage of container startup
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.
Custom samples in Rails are also available. You can see that it is different from the instruction in the official image of Docker Hub.
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"]
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"
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.
WEB
-[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