--Build a CentOS Linux 8 environment with Docker on macOS --Start Apache HTTP Server and access from inside and outside the container
$ docker --version
Docker version 19.03.8, build afacb8b
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.
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
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
Check the OS version etc. in the shell.
[root@29d737551a55 /]# 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"
[root@29d737551a55 /]# 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.
[root@29d737551a55 /]# vim
bash: /usr/bin/vim: No such file or directory
Install vim.
[root@29d737551a55 /]# dnf install vim
[root@29d737551a55 /]# 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.
[root@29d737551a55 /]# exit
exit
You can stop the running container with the docker stop command.
$ docker stop foobar
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
You can start a stopped container with the docker start command.
$ docker start foobar
Launch a shell inside the container and log in.
$ docker exec -it foobar bash
Install the httpd package with the dnf command.
[root@29d737551a55 /]# dnf install httpd
You have now installed Apache HTTP Server. Check the version.
[root@29d737551a55 /]# httpd -v
Server version: Apache/2.4.37 (centos)
Server built: Dec 23 2019 20:45:34
Set to manage with systemd.
[root@29d737551a55 /]# 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.
[root@29d737551a55 /]# systemctl start httpd
By default, the document root is / var / www / html /, so create an HTML file in /var/www/html/index.html.
[root@29d737551a55 /]# 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.
[root@29d737551a55 /]# 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>