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.

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.

#centos #docker #nginx #redhat #virtualization #centos tips #docker tips #nginx tips #rhel tips #virtualization

How to Install, Run and Delete Applications Inside Docker Containers - Part 2
5.55 GEEK