docker run command serves for creating containers from images.
In this article i will show how to run a Docker image as a container on the example of the official latest base image of Ubuntu.
I will show how to install
apache2 inside a container with Ubuntu and how to save this container as a new image.
And finally i will show how to run this new Docker image as a container in the both interactive and background modes.
Cool Tip: Image vs. Container … What is the difference? Read More →
Run Docker Image as a Container
Do Not Confuse: Docker can’t run images themselves. The
docker run command takes the Docker image as a template and produces a container from it.
Search Docker Hub for an image to download:
$ docker search ubuntu NAME DESCRIPTION STARS OFFICIAL AUTOMATED ubuntu Ubuntu is a Debian... 6759 [OK] dorowu/ubuntu-desktop-lxde-vnc Ubuntu with openss... 141 [OK] rastasheep/ubuntu-sshd Dockerized SSH ser... 114 [OK] ansible/ubuntu14.04-ansible Ubuntu 14.04 LTS w... 88 [OK] ubuntu-upstart Upstart is an even... 80 [OK]
Pull the Docker image from a repository with the
docker pull command:
$ docker pull ubuntu
$ docker run -it ubuntu /bin/bash root@e485d06f2182:/#
When you execute
docker run IMAGE, the Docker engine takes the
IMAGE and creates a container from it by adding a top writable layer and initializing various settings (network ports, container name, ID and resource limits).
apache2 web-server inside the container and exit:
root@e485d06f2182:/# apt update root@e485d06f2182:/# apt install apache2 -y root@e485d06f2182:/# exit
Create a new image from the stopped container inside which you have installed
apache2 and name it
$ docker commit e485d06f2182 apache_snapshot
To view a list of all images on your Docker host, run:
$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE apache_snapshot latest 13037686eac3 22 seconds ago 249MB ubuntu latest 00fd29ccc6f1 3 weeks ago 111MB
Cool Tip: How do i list ( running | stopped | all ) Docker containers! Read More →
Now you can run the new Docker image as a container in interactive mode:
$ docker run -it apache_snapshot /bin/bash
Or, for example, you can run the Docker image as a container in background with port
:80 inside the Docker container forwarded to the port
:8080 on the Docker host:
$ docker run -d -p 8080:80 apache_snapshot /usr/sbin/apache2ctl -D FOREGROUND
In this case, to check that
apache2 inside the container is running, just visit http://localhost:8080/ and you will see “Apache2 Ubuntu Default Page”.