How to Install, Run Applications inside Docker Containers
In our previous article, we have instructed about Docker installation and configuration on Major Linux distribution such as Debian, Ubuntu, LinuxMint, CentOS, RHEL, Fedora, openSUSE, Arch Linux and also we have played around with Docker Images & Docker Containers. Here, we are going to teach you, how to install application inside the container, how to view the installed application from the container.
1) Install Application on Docker container
First run the docker container and install the application which you want. Here we are going to install apache web server on Ubuntu docker container.
[Run a Docker Container] # docker run -ti ubuntu /bin/bash
Now, you are in inside the container, just fire the usual command to install apache web server.
[Install Apache Web Server] [email protected]:/# apt-get install apache2 Reading package lists... Done Building dependency tree Reading state information... Done The following extra packages will be installed: apache2-bin apache2-data libapr1 libaprutil1 libaprutil1-dbd-sqlite3
CTRL+P followed by
CTRL+Q to exit from interactive shell and note the
[Note container ID] # docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a19e19ab2e15 ubuntu "/bin/bash" About a minute ago Up About a minute stoic_lichterman
Commit the change and create new image with apache installation. My Container ID
a19e19ab2e15 and my new image name
[Commit the change] # docker commit a19e19ab2e15 2g/apache2 sha256:30d347abeadc6816c95de38d11b2c3beb6a49a3a394a549760d1cb396be2deaf [Check the new image] # docker images REPOSITORY TAG IMAGE ID CREATED SIZE 2g/apache2 latest 30d347abeadc About a minute ago 202.4 MB ubuntu latest 97434d46f197 2 days ago 188 MB ubuntu 12.04 8e3c25486445 2 days ago 138.4 MB centos latest d0e7f81ca65c 2 weeks ago 196.6 MB hello-world latest 690ed74de00f 5 months ago 960 B
2) Running a web application in Docker
First run the docker container and install the application which you want. Here we are going to install apache web server on Ubuntu docker container. When we passed the
-P flag to the
docker run command Docker mapped any ports exposed in our image to our host. In my case Docker has exposed port
80 (the default apache web server port) on port
8080. We need to run apache in foreground so that it will run until container stop.
[Run a Docker Container] # docker run -d -p 8080:80 2g/apache2 /usr/sbin/apache2ctl -D FOREGROUND 28c7920b8e85e42fa2f21d21659ec01f96880eabd78d951906fcb71cc8096740 [Apache Running Inside the Container] # docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 28c7920b8e85 2g/apache2 "/usr/sbin/apache2ctl" 3 minutes ago Up 3 minutes 0.0.0.0:8080->80/tcp compassionate_ptolemy [We can lookup what port is externally mapped to actual port inside the container] # [docker port ID Actual Port] # docker port 28c7920b8e85 80 0.0.0.0:8080
Now, we can access the inside container apache from host system using
localhost:8080. See the screen shot.