How To Create Docker File With Apache and SSH On Linux

How To Create Docker File With Apache and SSH On Linux

I was wondering how to create a docker file with apache and ssh. So finally, I got it. Now I’m going to share it with you. We can create an apache and install the OpenSSH server into the docker container using the dockerfile. Once you have installed it. we can do ssh to the docker container.

We will create a simple dockerfile with basic installation. I will install only Apache and OpenSSH server. I have created the image manually to show you how to troubleshoot the issues when you face it in the container. We can also create a container from dockerfile.

Check Out: How To Mount the Same Volume To Multiple Container In Docker

Let’s create a dockerfile:

I have created a folder data and put the dockerfile here in this folder.

[root@docker data]# mkdir data
[root@docker data]# vim dockerfile
FROM centos:7
RUN yum -y install wget net-tools openssh-server httpd
EXPOSE 80 22
CMD ["/usr/sbin/sshd", "-D"]

Now you have to build the image using the below command.

[root@docker data]# docker build -t apachecon .
Sending build context to Docker daemon 2.048kB
Step 1/4 : FROM centos:7
---> b5b4d78bc90c
Step 2/4 : RUN yum -y install wget net-tools openssh-server
---> Running in 6bb54e392be3
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
* base:
* extras:
* updates:
Resolving Dependencies
--> Running transaction check
---> Package net-tools.x86_64 0:2.0-0.25.20131004git.el7 will be installed
---> Package openssh-server.x86_64 0:7.4p1-21.el7 will be installed
--> Processing Dependency: openssh = 7.4p1-21.el7 for package: openssh-server-7.4p1-21.el7.x86_64
--> Processing Dependency: fipscheck-lib(x86-64) >= 1.3.0 for package: openssh-server-7.4p1-21.el7.x86_64
--> Processing Dependency: for package: openssh-server-7.4p1-21.el7.x86_64
--> Processing Dependency: for package: openssh-server-7.4p1-21.el7.x86_64
---> Package wget.x86_64 0:1.14-18.el7_6.1 will be installed
--> Running transaction check
---> Package fipscheck-lib.x86_64 0:1.4.1-6.el7 will be installed
--> Processing Dependency: /usr/bin/fipscheck for package: fipscheck-lib-1.4.1-6.el7.x86_64
---> Package openssh.x86_64 0:7.4p1-21.el7 will be installed
---> Package tcp_wrappers-libs.x86_64 0:7.6-77.el7 will be installed
--> Running transaction check
---> Package fipscheck.x86_64 0:1.4.1-6.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
Package Arch Version Repository
net-tools x86_64 2.0-0.25.20131004git.el7 base 306 k
openssh-server x86_64 7.4p1-21.el7 base 459 k
wget x86_64 1.14-18.el7_6.1 base 547 k
Installing for dependencies:
fipscheck x86_64 1.4.1-6.el7 base 21 k
fipscheck-lib x86_64 1.4.1-6.el7 base 11 k
openssh x86_64 7.4p1-21.el7 base 510 k
tcp_wrappers-libs x86_64 7.6-77.el7 base 66 k
Transaction Summary
Install 3 Packages (+4 Dependent packages)
Total download size: 1.9 M
Installed size: 5.9 M
Downloading packages:
warning: /var/cache/yum/x86_64/7/base/packages/fipscheck-lib-1.4.1-6.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for fipscheck-lib-1.4.1-6.el7.x86_64.rpm is not installed
Total 259 kB/s | 1.9 MB 00:07
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
Userid : "CentOS-7 Key (CentOS 7 Official Signing Key) <>"
Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
Package : centos-release-7-8.2003.0.el7.centos.x86_64 (@CentOS)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : fipscheck-1.4.1-6.el7.x86_64 1/7
Installing : fipscheck-lib-1.4.1-6.el7.x86_64 2/7
Installing : openssh-7.4p1-21.el7.x86_64 3/7
Installing : tcp_wrappers-libs-7.6-77.el7.x86_64 4/7
Installing : openssh-server-7.4p1-21.el7.x86_64 5/7
Installing : wget-1.14-18.el7_6.1.x86_64 6/7
install-info: No such file or directory for /usr/share/info/
Installing : net-tools-2.0-0.25.20131004git.el7.x86_64 7/7
Verifying : fipscheck-lib-1.4.1-6.el7.x86_64 1/7
Verifying : fipscheck-1.4.1-6.el7.x86_64 2/7
Verifying : net-tools-2.0-0.25.20131004git.el7.x86_64 3/7
Verifying : tcp_wrappers-libs-7.6-77.el7.x86_64 4/7
Verifying : wget-1.14-18.el7_6.1.x86_64 5/7
Verifying : openssh-7.4p1-21.el7.x86_64 6/7
Verifying : openssh-server-7.4p1-21.el7.x86_64 7/7
net-tools.x86_64 0:2.0-0.25.20131004git.el7
openssh-server.x86_64 0:7.4p1-21.el7
wget.x86_64 0:1.14-18.el7_6.1
Dependency Installed:
fipscheck.x86_64 0:1.4.1-6.el7 fipscheck-lib.x86_64 0:1.4.1-6.el7
openssh.x86_64 0:7.4p1-21.el7 tcp_wrappers-libs.x86_64 0:7.6-77.el7
Removing intermediate container 6bb54e392be3
---> 64762c9b148a
Step 3/4 : EXPOSE 80 22
---> Running in 87c15f7bcd77
Removing intermediate container 87c15f7bcd77
---> 57dc5217a315
Step 4/4 : CMD ["/usr/sbin/sshd", "-D"]
---> Running in 11a53805dd01
Removing intermediate container 11a53805dd01
---> 14734e090d9e
Successfully built 14734e090d9e
Successfully tagged apachecon:latest

Now you can check the image that has been created.

[root@docker data]# docker image ls
REPOSITORY      TAG            IMAGE ID                  CREATED              SIZE
apachecon             latest          14734e090d9e           18 seconds ago       272MB

Check Out: Install and Configure AWS CLI Version 2 Tool On Windows

Let’s create a container using the command docker container run -it apachecon /bin/bash

[root@docker ~]# docker container ls

I’m going to create a file inside the /var/www/html directory.

[root@001fed8bbadc ssh]# cd /var/www/html/
[root@001fed8bbadc html]# echo "testing website" > index.html

Start the apache service using the below commands.

[root@001fed8bbadc html]# /usr/sbin/httpd -k start

Now test the website running with below commands.

[root@001fed8bbadc html]# curl
testing website

Configure SSH:

Login to the container using docker attach 001fed8bbadc and try to start ssh service. I’m inside the container If you face the below error related to the SSH key.

Check Out: How To Install Deluge Bit Torrent Client on Ubuntu 20.04

You can also add these RUN /usr/bin/ssh-keygen -A line in the dockerfile.

[root@001fed8bbadc conf]# /usr/sbin/sshd -k start
Could not load host key: /etc/ssh/ssh_host_rsa_key
Could not load host key: /etc/ssh/ssh_host_ecdsa_key
Could not load host key: /etc/ssh/ssh_host_ed25519_key
sshd: no hostkeys available -- exiting.

To fix this issue, you can run the below commands to create a host keys inside the container.

[root@001fed8bbadc /]# /usr/bin/ssh-keygen -A
ssh-keygen: generating new host keys: RSA1 RSA DSA ECDSA ED25519

Now I will find the container IP address using the below commands and start the ssh service using /usr/sbin/sshd -k start.

Check the ports listening

 install apache with dockerfile

Find the IP address of the running container.

[root@docker ~]# docker container inspect 001fed8bbadc | grep "IPAddress"
              "SecondaryIPAddresses": null,
              "IPAddress": "",
                          "IPAddress": "",

Check Out: How To Install OpenShot Video Editor On Ubuntu 20.04

We have to check I’m able to ssh the container from the docker host machine using the commands.

[root@docker ~]# ssh root@
The authenticity of host ' (' can't be established.
ECDSA key fingerprint is SHA256:NuVeDDHd0HVmODXveE6+qtwVSyQqgbVD8AAJFgMBgXA.
ECDSA key fingerprint is MD5:68:82:21:b8:36:d9:da:07:f2:9b:55:5a:37:bb:9e:59.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '' (ECDSA) to the list of known hosts.
root@'s password:
Last login: Fri May 15 06:35:49 2020 from gateway
[root@001fed8bbadc ~]# ls /

Once you’re done with your work inside the container and want to save as a new image. You can create a new container using this image that will have Apache and ssh configured.

Save the container image using the below commands.


docker commit [options] repository [repository:[tag]]

[root@docker ~]# docker commit -m "apache with ssh" 001fed8bbadc 001fed8bbadc:apache-ssh

Check Out: How To Install Moz SEO Extension In Google Chrome

Verify image has saved with the name apache-ssh

install openssh docker container

We have successfully logged in to the container and you can only access the container from the docker host machine because we haven’t bound the port with docker host machine to container port.

You’re done Install apache with dockerfile openssh container  docker

Share on:

I'm the founder of Curious Viral. I hope this blog will provide you complete information about Linux Technology & I would like to share my technical knowledge with you which I have learned during this period.

Other Posts You May Like...

Leave a comment