freeCodeCamp/guide/english/containers/docker/creating-a-web-server-insid.../index.md

80 lines
2.2 KiB
Markdown
Raw Normal View History

2018-10-19 12:53:51 +00:00
---
title: Creating a Web Server inside Docker
---
# Creating a Web Server inside Docker
### Start docker and its container -
```
2018-10-19 12:53:51 +00:00
$ systemctl restart docker
$ systemctl enable docker
$ docker run -it --name webserver centos:latest
```
### Install httpd
- **yum** is already configured in centos docker image.
2018-10-19 12:53:51 +00:00
So directly install **httpd** software -
`$ yum install httpd -y`
2018-10-19 12:53:51 +00:00
- Create a dummy web page to check the server -
```
2018-10-19 12:53:51 +00:00
$ cd /var/www/html
$ vi index.html
```
2018-10-19 12:53:51 +00:00
### Start services -
- If we use **systemctl** to start the services, this will not work and gives an error.
- **systemctl** doesn't work in docker.
- In actual RedHat system, when we start a service it actually runs a script in background. That script starts daemons.
2018-10-19 12:53:51 +00:00
- To find the path of that script, check status of service
`$ systemctl status httpd`
`Loaded` option shows script file path.
2018-10-19 12:53:51 +00:00
- In that file, we have a line which actually starts service -
`ExecStart = /usr/sbin/httpd....... `
So the command `/usr/sbin/httpd` actually starts **httpd** server.
2018-10-19 12:53:51 +00:00
- Service is running or not, can be checked by -
`$ ps -aux | grep httpd`
2018-10-19 12:53:51 +00:00
- So we don't require `systemctl` we can directly start our web server using-
`$ /usr/sbin/httpd`
This will start the web server.
2018-10-19 12:53:51 +00:00
- `ifconfig` doesn't work in docker.
- So install software, which gives `ifconfig` command.
2018-10-19 12:53:51 +00:00
- It can be checked in actual Redhat system by running this command-
`$ rpm -qf /usr/sbin/ifconfig`
- This comes from **net-tools** package.
- So Install **net-tools** in docker os.
2018-10-19 12:53:51 +00:00
- Making _image_ of created web server -
`$ docker commit webserver apacheimg:v1`
- Name of container is _webserver_.
- This image can be share with exact setup with other users.
2018-10-19 12:53:51 +00:00
- To save created image -
`$ docker save apacheimg:v1 -o mywebserver.tar`
- To start **httpd** service automatically when container starts -
2018-10-19 12:53:51 +00:00
- Write `/usr/sbin/httpd` in following file.
`$ vim /root/.bashrc`
2018-10-19 12:53:51 +00:00
- To copy a file in container from the base system -
`$ docker cp /root/form.txt myconatiner:/`
## Summary
Summary of all the commands -
```
$ docker run -it --name webserver centos:latest
$ yum install httpd -y
$ rpm -q httpd
$ cd /var/www/html
$ vi index.html
// write some web content
$ /usr/sbin/httpd
$ yum install net-tool
$ ifconfig
```