Table of contents
Open Table of contents
Article body
Besides finding images in public Docker registries, we can build and customize our own images with a Dockerfile.
Note: Dockerfile syntax is not very complicated, but I think that unless you specialize in operations, understanding it may be enough. Even after writing a Dockerfile and building a usable image, you may find it is not a complete Linux environment. Many parts still need configuration from scratch; otherwise, it is just a shell a few hundred megabytes in size.
What should we do? Use a ready-made setup to save time and effort. I recommend the NetEase Cloud image center, which has ready-to-use Dockerfiles for Linux distributions such as CentOS and Ubuntu. They provide a complete environment with initialization already handled.
The following example comes from a Dockerfile in the NetEase Cloud image center.
=======================================================================================
NetEase Cloud 163 image center (https://c.163yun.com/hub#/home)
Example CentOS Dockerfile
This image mainly includes:
Yum repositories replaced with the NetEase mirror,
and common software installed: openssh-server, supervisor, vim, tar, wget, curl, rsync, bzip2, iptables, tcpdump, less, telnet, net-tools, lsof, sysstat, and cron.
Ready to use out of the box.
=======================================================================================
FROM hub.c.163.com/public/centos:7.2.1511
RUN yum clean all
RUN yum install -y yum-plugin-ovl || true
RUN yum install -y vim tar wget curl rsync bzip2 iptables tcpdump less telnet net-tools lsof sysstat cronie python-setuptools
RUN yum clean all
RUN easy_install supervisor
RUN cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
EXPOSE 22
RUN mkdir -p /etc/supervisor/conf.d/
RUN /usr/bin/echo_supervisord_conf > /etc/supervisord.conf
RUN echo [include] >> /etc/supervisord.conf
RUN echo 'files = /etc/supervisor/conf.d/*.conf' >> /etc/supervisord.conf
COPY sshd.conf /etc/supervisor/conf.d/sshd.conf
CMD ["/usr/bin/supervisord"]
========================================================================================
The detailed walkthrough below uses macOS.
- Start the Docker for Mac client.
- In Preferences (Command+,) -> Docker Engine, configure a mirror to speed up image downloads in mainland China: the Alibaba Cloud mirror at https://8rwmp8tx.mirror.aliyuncs.com, or DaoCloud’s Docker Hub mirror service at https://dashboard.daocloud.io/runtime-apps. On that page, click the gray accelerator button at the top right to obtain http://f1361db2.m.daocloud.io. I found this address fast for both pulls and pushes. Here we build from a Dockerfile rather than pulling a ready-made image.
- Create a Dockerfile under the current user’s directory/dockerFiles/usingDockerFile. You can choose a different directory.
- Log in to the NetEase Cloud image center (link), find CentOS, select a version, and copy its Dockerfile.
- Run docker build -t docker-whale . to start building the image. Do not forget the final dot.
- Run docker run -it —name 163centos(container name) IMAGE_ID /bin/bash to enter the container.
- Run group root to inspect the root user’s groups.
Now configure users. 9. useradd -d /home/dhb -m dhb (-d specifies the user’s directory, -m creates it if absent, and dhb is the username). 10. groupadd -g 414 guest creates the guest group with group ID 414. 11. usermod -g guest -G root dhb (-g is the primary group, -G the supplementary group, and dhb the username) adds dhb to guest and root. 12. passwd dhb sets dhb’s password to 123456 (also the root password here). 13. rpm -qa | grep java checks whether JDK 8 is already installed. It usually is not. -qa queries all packages.
Now configure the environment.
Configure the JDK. 14. yum search jdk searches for JDK packages. 15. yum install java-1.8.0-openjdk.x86_64 installs JDK 8. 16. java -versoin checks the installation after it finishes. /usr/lib/jvm is the default installation directory. 17. vim /etc/profile opens the environment variable configuration. 18. Append the following to profile:
#set java environment
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.242.b08-0.el8_1.x86_64
JRE_HOME=$JAVA_HOME/jre
PATH=$PATH:$JAVA_HOME/bin
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME
export JRE_HOME
export PATH
export CLASSPATH
- source /etc/profile makes the changes take effect immediately.
Configure Maven. 20. yum -y install maven installs Maven. 21. vim /etc/maven/settings.xml -> :set nu (show line numbers) -> / <url (search forward for <url) -> :156 (jump to line 156, where <url appears here) -> i (insert mode) -> replace it with Alibaba’s repository at https://maven.aliyun.com/repository/public, a mirror for mainland China -> :wq -> source /etc/profile. 22. vim /etc/profile -> G (jump to the end; gg jumps to the beginning) -> append:
export M2_HOME=/usr/share/maven
(If you do not know Maven’s installation path, use whereis maven rather than which maven, which shows the executable’s path.)
export PATH=$PATH:$JAVA_HOME/bin:$M2_HOME/bin
-> :wq -> source /etc/profile (apply the file immediately). A Vim tip: $ jumps to the end of the line, and 0 to its beginning.
Run a Java program in the configured container. 23. docker cp ${projectPath}/mathml-0.0.1-SNAPSHOT.jar(the host file path; set projectPath appropriately) c84a9fdd2560(container ID):/home/dhb/project(path inside the container). Enter the container directory and try starting the JAR to check it works: java -jar ${projectPath}/mathml-0.0.1-SNAPSHOT.jar. 24. exit (leave the container) -> docker rm containerId (delete it if no longer needed). 25. docker commit -a(author) “dhb” -m(commit message) “163mavenos” containerId standardos(new image name):latest(version). 26. docker tag standardos jacksparrow414(Docker Hub account)/standardos(repository name). 27. docker push jacksparrow414/standardos pushes it to the remote registry. 28. After a successful push, find it in your personal Docker Hub repository and copy the corresponding pull command. 29. docker rmi image deletes the local image. First delete associated containers with the second command in Step 24. 30. docker pull jacksparrow414/standardos pulls the image again. 31. docker run -it —name os1(container name) —network(use a custom bridge rather than the default) dhb-net(custom bridge name) jacksparrow414/standardos:latest tests whether the downloaded image is usable. The test passes.
A later article will explain why —network is used.
That covers the basic Docker operations. Inside the container, working with the environment is much like working with Linux, so you can explore freely.
Finally, here is the image size: 1.14 GB. This is what I consider a real CentOS environment.
