Build a CentOS Linux 8 environment with Docker and start Apache HTTP Server

Overview

--Build a CentOS Linux 8 environment with Docker on macOS --Start Apache HTTP Server and access from inside and outside the container

This environment

$ docker --version
Docker version 19.03.8, build afacb8b

Get CentOS Linux 8 image from Docker Hub

Get the image with the docker pull command.

$ docker pull centos:centos8
centos8: Pulling from library/centos
8a29a15cefae: Pull complete 
Digest: sha256:fe8d824220415eed5477b63addf40fb06c3b049404242b31982106ac204f6700
Status: Downloaded newer image for centos:centos8
docker.io/library/centos:centos8

You can find an image of CentOS Linux that you can install from centos Tags -Docker Hub.

Create and launch a container

Assign the name foobar to the container and run it in the background. Here, port 80 of the container is assigned to port 8080 of the host.

$ docker run --detach --name foobar --privileged --publish=8080:80 centos:centos8 /sbin/init

run — Docker \ -docs \ -ja 17 \ .06 \ .Beta documentation

The docker run command first creates a writable container layer on the specified image. Then start using the specified command. This docker run is the same as running / containers / (id) / start after the API / containers / create.

Note that if you do not specify --privileged and / sbin / init in the docker run command, you will get the following error message when using systemd and you will not be able to use systemd.

System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down

Log in to CentOS Linux 8

Use the docker exec command to run the bash shell inside the launched container. You can now get inside CentOS Linux 8.

$ docker exec -it foobar bash

Uses CentOS Linux 8

Check the OS version etc. in the shell.

[[email protected] /]# cat /etc/os-release 
NAME="CentOS Linux"
VERSION="8 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="8"
PLATFORM_ID="platform:el8"
PRETTY_NAME="CentOS Linux 8 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:8"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-8"
CENTOS_MANTISBT_PROJECT_VERSION="8"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="8"
[[email protected] /]# uname -a
Linux 29d737551a55 4.19.76-linuxkit #1 SMP Thu Oct 17 19:31:58 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

You don't have the software you think you use often, such as vim.

[[email protected] /]# vim
bash: /usr/bin/vim: No such file or directory

Install vim.

[[email protected] /]# dnf install vim
[[email protected] /]# vim --version
VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Nov 11 2019 19:08:24)
Included patches: 1-1763

Use the exit command to exit the shell.

[[email protected] /]# exit
exit

Stop container

You can stop the running container with the docker stop command.

$ docker stop foobar

Check container information

You can display container information with the docker ps command.

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                        PORTS               NAMES
29d737551a55        centos:centos8      "/sbin/init"        46 minutes ago      Exited (137) 19 seconds ago                       foobar

Start container

You can start a stopped container with the docker start command.

$ docker start foobar

Install Apache HTTP Server

Launch a shell inside the container and log in.

$ docker exec -it foobar bash

Install the httpd package with the dnf command.

[[email protected] /]# dnf install httpd

You have now installed Apache HTTP Server. Check the version.

[[email protected] /]# httpd -v
Server version: Apache/2.4.37 (centos)
Server built:   Dec 23 2019 20:45:34

Set to manage with systemd.

[[email protected] /]# systemctl enable httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.

Start Apache HTTP Server with the systemctl start command.

[[email protected] /]# systemctl start httpd

By default, the document root is / var / www / html /, so create an HTML file in /var/www/html/index.html.

[[email protected] /]# vi /var/www/html/index.html

This time, I described the following contents.

<html>
<body>
Hello, World!
</body>
</html>

Apache HTTP Server outputs an HTML file when accessed with the curl command.

[[email protected] /]# curl -i http://localhost/
HTTP/1.1 200 OK
Date: Sat, 21 Mar 2020 06:00:04 GMT
Server: Apache/2.4.37 (centos)
Last-Modified: Sat, 21 Mar 2020 05:59:52 GMT
ETag: "2d-5a1571c48ab9e"
Accept-Ranges: bytes
Content-Length: 45
Content-Type: text/html; charset=UTF-8

<html>
<body>
Hello, World!
</body>
</html>

This time, port 80 of the container is assigned to port 8080 on the Docker host side. If you access port 8080 on the host side from outside the container, you will be connected to port 80 of the container.

$ curl -i http://localhost:8080/
HTTP/1.1 200 OK
Date: Sat, 21 Mar 2020 06:01:21 GMT
Server: Apache/2.4.37 (centos)
Last-Modified: Sat, 21 Mar 2020 05:59:52 GMT
ETag: "2d-5a1571c48ab9e"
Accept-Ranges: bytes
Content-Length: 45
Content-Type: text/html; charset=UTF-8

<html>
<body>
Hello, World!
</body>
</html>

Reference material

Recommended Posts

Build a CentOS Linux 8 environment with Docker and start Apache HTTP Server
Build Docker environment (Linux 8) and start Apache HTTP Server container
[Linux] Build a jenkins environment with Docker
[Linux] Build a Docker environment with Amazon Linux 2
Build Apache HTTP Server and Wildfly on Oracle Linux 8
[Linux] Create a self-signed certificate with Docker and apache
Build a LAMP environment with Vagrant (Linux + Apache + MySQL + PHP)
[DynamoDB] [Docker] Build a development environment for DynamoDB and Django with docker-compose
Build a python environment with ansible on centos6
Start a simple Python web server with Docker
[Python] Build a Django development environment with Docker
Build a virtual environment with pyenv and venv
Build PyPy and Python execution environment with Docker
Source compile Apache2.4 + PHP7.4 with Raspberry Pi and build a Web server --2 PHP introduction
Build a Python + bottle + MySQL environment with Docker on RaspberryPi3! [Trial and error]
Source compile Apache2.4 + PHP7.4 with Raspberry Pi and build a Web server ―― 1. Apache introduction
Source compile Apache2.4 (httpd 2.4.43) + PHP7.4 on Linux and build a Web server ―― 1. Apache introduction
Source compile Apache2.4 (httpd 2.4.43) + PHP7.4 on Linux and build a Web server --2 PHP introduction
Build a python virtual environment with virtualenv and virtualenvwrapper
Build a python virtual environment with virtualenv and virtualenvwrapper
Build a development environment with Poetry Django Docker Pycharm
Build a numerical calculation environment with pyenv and miniconda3
Build a LAMP environment [CentOS 7]
Source compile Apache2.4 + PHP7.4 with Raspberry Pi and build a web server --3. Use MySQL
Install LAMP on Amazon Linux 2 and build a WordPress environment.
Build a Django development environment with Docker! (Docker-compose / Django / postgreSQL / nginx)
Build a Docker environment that can use PyTorch and JupyterLab
Build Azure Pipelies with Azure DevOps in a Linux self-hosted environment
Build a machine learning scikit-learn environment with VirtualBox and Ubuntu
[Memo] Build a development environment for Django + Nuxt.js with Docker
Set up a web server with CentOS7 + Anaconda + Django + Apache
[Django] Build a Django container (Docker) development environment quickly with PyCharm
Build a Python environment and transfer data to the server
Build GPU environment with GCP and kaggle official image (docker)
Create a Todo app with Django ① Build an environment with Docker
Build a python environment on CentOS 7.7 for your home server
Build a go environment using Docker
Creating a Flask server with Docker
Build a deb file with Docker
Build Mysql + Python environment with docker
Build PyPy execution environment with Docker
Build a python3 environment on CentOS7
Build a 64-bit Python 2.7 environment with TDM-GCC and MinGW-w64 on Windows 7
Create a C ++ and Python execution environment with WSL2 + Docker + VSCode
Create a simple Python development environment with VS Code and Docker
Easily build a development environment with Laragon
Build a Tensorflow environment with Raspberry Pi [2020]
A memo with Python2.7 and Python3 on CentOS
Build a Fast API environment with docker-compose
Get a local DynamoDB environment with Docker
CentOS 6.4 with Python 2.7.3 with Apache with mod_wsgi and Django
Build a Samba server on Arch Linux
Build a python virtual environment with pyenv
Build a modern Python environment with Neovim
Build NGINX + NGINX Unit + MySQL environment with Docker
Build a Python + bottle + MySQL environment with Docker on RaspberryPi3! [Easy construction]
I tried Linux (CentOS 7) life and death monitoring (Ping) with monitoring server Zabbix
Set up reverse proxy to https server with CentOS Linux 8 + Apache mod_ssl
Start a web server using Bottle and Flask (I also tried using Apache)
Build a drone simulator environment and try a simple flight with Mission Planner
Build Linux on a Windows environment. Steps to install Laradock and migrate