docker runserves to launch Docker containers from images.
In this article, We’ll show you how to run a container from an image using the example of the latest official base Docker image of Ubuntu.
We’ll show you how to install
apache2inside a container with Ubuntu and how to save this container as a new image.
And in the end, you will be able to run containers from this new image in the interactive and background modes.
Good advice: An image vs. Container … What’s the difference? Read More →
Running the Container from the Image into the Docker
Do not Confuse: The Docker image itself cannot be “run”. The command
docker runtakes the Docker image as a template and creates a container from it, which is launched.
Locate the required image on the Docker Hub :
$ 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]
Download the Docker image from the repository using the command
$ docker pull ubuntu
Start the container from the Docker image:
$ docker run -it ubuntu / bin / bash
root @ e785d56f4312: / #
When you execute
Docker run image, the Docker engine takes
image, adds the writeable top layer and initializes various parameters (network ports, container name, identifier and resource limits).
Install the Web server
apache2inside the container, and then exit it:
root @ e785d56f4312: / # apt update
root @ e785d56f4312: / # apt install apache2 -y
root @ e785d56f4312: / # exit
From the stopped container in which you installed
apache2, create a new image and name it
$ docker commit e785d56f4312 apache_snapshot
To view all the images on the Docker host:
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
apache_snapshot latest 13037686eac3 22 seconds ago 249MB
ubuntu latest 00fd29ccc6f1 3 weeks ago 111MB
Now from the new Docker-image, you can start the containers in interactive mode:
$ docker run -i -t apachesnapshot /bin/bash
Alternatively, you can run the container from the Docker image in the background with the port
:80inside the Docker container flown to the
:8080Docker host port :
$ docker run -d -p 8080:80 apache snapshot /usr/sbin/apache2ctl -D FOREGROUND
In this case, to make sure that
apache2inside the container is running, simply open http://localhost:8080/ and you will see the start page: “Apache2 Ubuntu Default Page”.
Join our reader's list to start receiving latest updates.
Get free Tech, Linux tips and resources delivered directly to your inbox.