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"
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
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
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
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
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
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
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.
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
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.
9. To stop the container run the following command followed by container ID or name:
# docker ps # docker stop fervent_mccarthy # docker ps
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:
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?
@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..
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
Enter container interactive cli with -it options and manually edit sources. Use nano editor If vim is not available.
Running ‘apt update’ first fixed this for me
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!
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.
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.
It is because there is no package cache in the image, you need to run:
hi,
Could you please let me know how can I set IP in docker image ?
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.
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
Please post next part of Docker..
Many Thanks
Ravi
@Ravi
Already published part 3, here is the link just go through it..
https://www.tecmint.com/build-and-configure-docker-container-images-with-dockerfile/