Installing Docker on CentOS 7 with JDK and Tomcat Setup
Docker Installation on CentOS 7
Registry Mirror Configuration
To improve image download speed, configure Alibaba's registry mirror by editing /etc/docker/daemon.json:
{
"registry-mirrors": ["https://91cntlkt.mirror.aliyuncs.com"]
}
Restart the Docker service after making changes:
systemctl restart docker
Installling Docker
Install Docker using yum:
yum install -y docker
Start and enable Docker at boot:
systemctl start docker
systemctl enable docker
Basic Docker Commands
Check Docker status:
docker --help
docker info
List images and containers:
docker images
docker ps -a
Pulling Base Images
Download CentOS and Tomcat images:
docker pull centos
docker pull tomcat
Running Containers
Run Tomcat container with port mapping:
# Detached mode
docker run -d -p 8081:8080 tomcat:latest
Container Management
Restart a running container:
docker restart container_name
Remove a image:
docker rmi -f image_id
File Transfer Between Host and Container
Copy files from container to host:
docker cp container_name:/path/to/file /host/path/
Copy files from host to container:
docker cp /host/path/file container_name:/path/to/destination/
Kernel Requirements
Verify kernel version:
uname -r
For CentOS 6.5 systems, upgrade kernel:
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
rpm -ivh http://www.elrepo.org/elrepo-release-6-5.el6.elrepo.noarch.rpm
yum -y --enablerepo=elrepo-kernel install kernel-lt
Edit /etc/grub.conf to set default kernel:
# Change default=1 to default=0
Reboot system to apply changes.
Installing Software in Container
Run interactive container:
docker run -it centos:latest /bin/bash
Inside container, install JDK and Tomcat:
# Extract JDK
mkdir -p /opt/java
tar -zxf jdk-7u55-linux-i586.tar.gz -C /opt/java
# Rename JDK directory
mv /opt/java/jdk1.7.0_67 /opt/java/jdk
# Extract Tomcat
wget http://archive.apache.org/dist/tomcat/tomcat-7/v7.0.52/bin/apache-tomcat-7.0.52.tar.gz
tar -zxf apache-tomcat-7.0.52.tar.gz -C /opt
mv /opt/apache-tomcat-7.0.55 /opt/tomcat
Set environment variables:
# Add to ~/.bashrc
export JAVA_HOME=/opt/java/jdk
export PATH=$PATH:$JAVA_HOME/bin
Apply environment settings:
source ~/.bashrc
Install missing dependencies if needed:
yum install -y glibc.i686
Creating Startup Script
Create script to launch Tomcat:
#!/bin/bash
source ~/.bashrc
sh /opt/tomcat/bin/catalina.sh run
Make executable:
chmod +x /root/run.sh
Building Custom Image
Commit container changes to new image:
docker commit container_id username/image_name:tag
Running Java Web Application
Launch container with custom image:
docker run -d -p 58080:8080 --name java-web username/image_name:tag /root/run.sh
Docker Concepts
Images vs Containers
Docker images are read-only templates used to create containers. Containers are running instances of images with a writable layer added on top.
Layered Architecture
Image are built in layers where each layer represents a set of changes. The base layer is the kernel, followed by base OS, then application layers.
Container Lifecycle
Containers can be in running or stopped states. Changes made during runtime are saved to the container's writable layer, not the base image.