How to Install, Run and Delete Applications inside Docker Containers – Part 2

Following the previous Docker article, this tutorial will discuss how to save a Docker container into a new image, remove a container and run a Nginx web server inside a container.

Install and Run Applications in Docker Containers

Install and Run Applications in Docker Containers – Part 2


  1. Install Docker on CentOS and RHEL 7/6

How To Run and Save a Docker Container

1. In this example we will run and save an Ubuntu based Docker container where Nginx server will be installed. But before committing any changes to container, first start the container with the below command which installs Nginx daemon into Ubuntu image:

# docker run ubuntu bash -c "apt-get -y install nginx" 
Install Nginx on Ubuntu Docker Container

Install Nginx on Ubuntu Docker Container

2. Next, after Nginx package is installed, issue the command docker ps -l to get the ID or name of the running container.

# docker ps -l
Find Docker Container ID Name

Find Docker Container ID Name

And apply changes by running the below command:

# docker commit 5976e4ae287c ubuntu-nginx

Here, 5976e4ae287c represents the container ID and ubuntu-nginx represents the name of the newly image that has been saved with committed changes.

In order to view if the new image has been successfully created just run docker images command and a listing of all saved images will be shown.

# docker images
Docker Container Changes

Docker Container Changes

Chances are that the installation process inside the container finishes fast which leads to a non-running container (container is stopped). In this case the docker ps command won’t show any output because no container is running.

In order to be able to still get the container’s id run docker ps -a | head -3 to output the most recent containers and identify the container based on the command issued to create the container and the exited status.

3. Alternatively, you can actively enter container session by running docker run -it ubuntu bash command and execute further apt-get install nginx command. While the command is running, detach from the container using Ctrl-p + Ctrl-q keys and the container will continue running even if the Nginx installation process finishes.

# docker run -it ubuntu bash
# apt-get install nginx
Install Nginx on Docker Container

Install Nginx on Docker Container

Then, get the running container id with docker ps and commit changes. When finished, re-enter to container console using docker attach and type exit to stop container.

# docker ps
# docker attach 3378689f2069
# exit
Attach Docker Container

Attach Docker Container

4. To further test if the recently image has been committed properly (in this case Nginx service has been installed), execute the below command in order to generate a new container which will output if Nginx binary was successfully installed:

# docker run ubuntu-nginx whereis nginx
Generate New Docker Container

Generate New Docker Container

5. To remove a container use the rm command against a container ID or name, which can be obtained using docker ps -a command:

# docker ps -a
# sudo docker rm 36488523933a
Remove Docker Container

Remove Docker Container

How to Run Nginx inside Docker Container

6. In this part we will concentrate on how you can run and access a network service, such as a Nginx web server, inside Docker, using the ubuntu-nginx image created earlier where Nginx daemon was installed.

The first thing that you need to do is to create a new container, map host-container ports and enter container shell by issuing the below command:

# docker run -it -p 81:80 ubuntu-nginx /bin/bash
# nginx &

Here, the -p option exposes the host port to container port. While the host port can be arbitrary, with the condition that it should be available (no other host services should listen on it), the container port must be exactly the port that the inside daemon is listening to.

Once you’re connected to container session, start Nginx daemon in background and detach from container console by pressing Ctrl-p + Ctrl-q keys.

Run Nginx Inside Docker Container

Run Nginx Inside Docker Container

7. Now, run docker ps to get the state of your running container. You can also view host network sockets by issuing the following command:

# docker ps
# netstat -tlpn 
View Docker Container Running State

View Docker Container Running State

8. In order to visit the page served by the Nginx container, open a browser from a remote location in your LAN and type the IP address of your machine using the HTTP protocol.

Verify Nginx Running under Docker Container

Verify Nginx Running under Docker Container

9. To stop the container run the following command followed by container ID or name:

# docker ps
# docker stop fervent_mccarthy
# docker ps
Stop Running Docker Container

Stop Running Docker Container

As alternative to stop the running container, enter container shell command prompt and type exit to finish process:

# docker attach fervent_mccarthy
# exit

Be aware that using this kind of containers to run web servers or other kind of services are best suited only for development purposes or tests due to the fact that the services are only active while the container is running. Exiting the container disrupts all running services or any changes made.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

Matei Cezar

I'am a computer addicted guy, a fan of open source and linux based system software, have about 4 years experience with Linux distributions desktop, servers and bash scripting.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide
The Complete Linux System Administrator Bundle
Become an Ethical Hacker Bonus Bundle

You may also like...

14 Responses

  1. Ramakrishna Nissankula says:

    I am struggling with the following. Can you please help me?

    I installed docker in a Ubuntu VM on my virtual box.

    My goal is that: From a centos based container that is running in this docker, I want to launch other docker containers. How do I do this?

    Do I need to yum install docker inside my first docker image?

    docker is present only in host Ubuntu VM. How can the centos based container run docker commands?

    • Ravi Saive says:


      Your question is still not clear to me..

      If you want to run CentOS container under Ubuntu VM, you can create container as explained in this article..

  2. Muthukumar says:

    I am getting below error. How do I edit /etc/apt/source.list Vi command is not working in Ubuntu image. Please help me on this.

    [[email protected] ~]# docker run ubuntu bash -c “apt-get -y install nginx”
    Reading package lists…
    Building dependency tree…
    Reading state information…
    E: Unable to locate package nginx

  3. kosmos says:

    Hi, when I run the command # docker run ubuntu bash -c “apt-get -y install nginx”

    i got this error message

    [[email protected] ~]# docker run ubuntu bash -c “apt-get -y install nginx”
    Reading package lists…
    Building dependency tree…
    Reading state information…
    E: Unable to locate package nginx


    • Matei Cezar says:

      What image are you using? verify /etc/apt/sources.list and always run sudo apt-get update before installing anything so that the package list is up-to-date.

      • Debojyoti Bose says:

        The above error indicates that the repos (universe) inside the sources.list doesn’t have the required package (nginx) for the image ‘ubuntu’. Things will work If you try using the image ‘‘, you will be able install the nginx daemon inside the container.

        [[email protected] ~]# docker images
        REPOSITORY                        TAG                 IMAGE ID            CREATED             SIZE
        ubuntu-nginx                      latest              7f6682022f10        23 minutes ago      642.6 MB   latest              28db94cff13b        7 days ago          634.5 MB
        [[email protected] ~]# docker run ubuntu-nginx whereis nginx
        nginx: /usr/sbin/nginx /etc/nginx /usr/share/nginx /usr/share/man/man1/nginx.1.gz
        [[email protected] ~]#
    • ISAHAK says:

      It is because there is no package cache in the image, you need to run:

      # apt-get update
  4. Shambhu says:

    Could you please let me know how can I set IP in docker image ?

    • Matei Cezar says:

      You cannot set an IP address inside a docker container, but you can create a bridge interface on the host and instruct a docker container to use that bridge using the -b option.

      • Shambhu says:

        Thanks But after apache install how I will call at browser ? Could you please share any doc or tutorial for this ?

        I will be thank full

  5. Ravi says:

    Please post next part of Docker..

    Many Thanks

Leave a Reply to Shambhu Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.