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 an Nginx web server inside a container.

Requirements

How To Run and Save a Docker Container

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

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

If you get error ‘E: Unable to locate package nginx‘, then you need to connect to a container with interactive CLI and install nginx as shown.

# docker run -it ubuntu bash
# apt install nginx
# exit

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 new 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 sessions by running docker run -it ubuntu bash command and execute the 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 the container.

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

4. To further test if the recent 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 an Nginx web server, inside Docker, using the ubuntu-nginx image created earlier where the 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 the 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 the 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
OR
# 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 an 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 container to run web servers or other kinds 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.

Further Reading:

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

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.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

14 Comments

Leave a Reply
  1. 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?

    Reply
    • @Ramakrishna,

      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..

      Reply
  2. 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.

    [root@docker ~]# 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

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

    i got this error message

    [root@ip-172-31-19-219 ~]# 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

    Thanks!

    Reply
    • 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.

      Reply
      • 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 ‘docker.io/nickistre/ubuntu-lamp‘, you will be able install the nginx daemon inside the container.

        [root@docker ~]# docker images
        REPOSITORY                        TAG                 IMAGE ID            CREATED             SIZE
        ubuntu-nginx                      latest              7f6682022f10        23 minutes ago      642.6 MB
        docker.io/nickistre/ubuntu-lamp   latest              28db94cff13b        7 days ago          634.5 MB
        
        [root@docker ~]# docker run ubuntu-nginx whereis nginx
        nginx: /usr/sbin/nginx /etc/nginx /usr/share/nginx /usr/share/man/man1/nginx.1.gz
        [root@docker ~]#
        
        Reply
    • 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.

      Reply
      • Hi,
        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

        Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.