Exercise Docker Images

Table of Contents

Introduction

This lab will take you through the Docker image creation by "Docker commit"; Let us create two Docker images:

  • One for an Apache server running on Ubuntu

  • Another is for an Nginx server running on CentOS

Exercise 1: Apache Server on Ubuntu

First, pull the Ubuntu image from Docker Hub:

docker pull ubuntu:latest

Then, run a Docker container from the Ubuntu image:

docker run -it ubuntu:latest /bin/bash

Inside the Docker container, update the system with the latest patches and install Apache:

apt-get update -y
apt-get install -y apache2

Start the Apache service and enable it to autostart:

service apache2 start
update-rc.d apache2 defaults

Then, exit the Docker container and commit the changes to a new Docker image:

exit
docker commit <container_id> my-apache-app

Finally, run a Docker container from the new image:

docker run -p 8080:80 my-apache-app

Exercise 2: Nginx Server on CentOS

First, pull the CentOS image from Docker Hub:

docker pull centos:latest

Then, run a Docker container from the CentOS image:

docker run -it centos:latest /bin/bash

Inside the Docker container, update the system, install EPEL Release, and install Nginx:

yum -y update
yum install -y epel-release
yum install -y nginx

Start the Nginx service and enable it to autostart:

systemctl start nginx
systemctl enable nginx

Then, exit the Docker container and commit the changes to a new Docker image:

exit
docker commit <container_id> my-nginx-app

Finally, run a Docker container from the new image:

docker run -p 8080:80 my-nginx-app

Note

The above exercises show how to use service and systemctl commands in Docker commit. However, it's important to note that Docker containers are designed to run one process per container. Therefore, it's not recommended to use systemctl or service to manage services within a container as it goes against this principle. Instead, it's better to run the service directly in the foreground. For Apache and Nginx, this can be done by using the -D FOREGROUND option for Apache and the -g "daemon off;" option for Nginx.

References


Please replace <container_id> with the ID of your Docker container. You can get the ID of the running containers by using the docker ps command.

Last updated