Setting Up a Host-Like Environment Using Docker Containers
This documentation provides a step-by-step guide to create a Docker container that mimics a traditional host environment. The container will be capable of running multiple processes and services using supervisord as the process manager.
Features
Nginx: Web server.
MySQL: Database server.
Supervisord: Process manager to manage multiple services.
Host-Like Environment Setup
- Create Dockerfile
Create a Dockerfile with the following content:
# Use an official Ubuntu base image
FROM ubuntu:latest
# Set environment variables to avoid user prompts during package installations
ENV DEBIAN_FRONTEND=noninteractive
# Update the package list and install necessary packages
RUN apt-get update && apt-get install -y \
nginx \
mysql-server \
supervisor \
&& rm -rf /var/lib/apt/lists/*
# Add supervisor configuration file
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
# Expose ports for services (e.g., 80 for nginx, 3306 for MySQL)
EXPOSE 80 3306
# Start supervisord to run multiple services
CMD ["/usr/bin/supervisord"]
Explanation:
Base Image: ubuntu:latest provides a minimal Ubuntu environment.
Environment Variables: DEBIAN_FRONTEND=noninteractive ensures package installations don't prompt for user input.
Package Installation: Installs nginx, mysql-server, and supervisor, followed by cleanup to reduce image size.
Configuration: Copies a custom supervisord configuration file into the container.
**Ports: **Exposes necessary ports for Nginx and MySQL.
Entrypoint: Uses supervisord to manage services within the container.
- Create supervisord Configuration
Create a supervisord.conf file with the following content:
[supervisord]
nodaemon=true
[program:nginx]
command=/usr/sbin/nginx -g "daemon off;"
autorestart=true
[program:mysql]
command=/usr/sbin/mysqld
autorestart=true
Explanation:
supervisord: Runs in the foreground (nodaemon=true).
Nginx: Configured to run in the foreground (daemon off;) and restart automatically if it fails.
MySQL: Configured to restart automatically if it fails.
- Build the Docker Image
Build the Docker image using the Dockerfile and supervisord configuration:
docker build -t my_host_like_env .
- Run the Docker Container
Run the Docker container based on the image you just built:
docker run -d --name my_container -p 80:80 -p 3306:3306 my_host_like_env
Check the container is running using:
docker ps
The output will look something similar like this:
If Nginx is running correctly, this command will fetch the default Nginx welcome page.
Access Nginx logs:
docker exec -it my_container tail -f /var/log/nginx/access.log
If prompted for a password, the default password is empty (just press Enter). Once connected, you can interact with MySQL as you would on a standard MySQL server.
Access MySQL logs:
docker exec -it my_container tail -f /var/log/mysql/error.log
Conclusion
You now have a Docker container that simulates a traditional host environment capable of running multiple processes and services. You can extend this setup by adding more services or customizing configurations as per your requirements.
SOCIAL SHARE CARD GENERATOR