In this 3-part article, we will learn about Docker and container service.
What is Docker?
Docker service help to build containers. A container includes all the necessary packages, libraries, and dependencies to run an application. which helps to build, run and ship any application on any platform. One server can host multiple numbers of containers and all these containers are controlled by Docker engine/service.
The best method to understand more about Docker, install it on your machine. For this tutorial, I am using a newly installed centos-6 machine.
To install Docker in CentOS-6, we should enable EPEL repositories.
#yum install epel-release #yum install docker-io
Once the docker installation is done, start the service and enable it system-wide.
# service docker start Starting cgconfig service: [ OK ] Starting docker: [ OK ] # chkconfig docker on
Check the current status of docker by running
# service docker status docker dead but pid file exists
If you getting the above error then you have to install lib-device-mapper
# yum install device-mapper-event-libs
Then restart the docker service again and confirm service is running
# service docker status docker (pid 1624) is running...
To get more information about the docker run ‘docker info’ command.
Docker service installed, now proceed with building a container. To build a docker container we need an image. Images can be downloaded from the Docker Hub repository or you can create one your self. We will discuss it later.
Here, I am using Ubuntu as my container image and will setup a LAMP stack inside it for running a WordPress powered website.
To search for an image issue the following command
# docker search ubuntu
The command list the available images and description. You can choose any one image based on your needs. Here I chose the latest Ubuntu. If you want to download a particular version of an image you have to mention it’s tag name or the latest available version will be downloaded.
For ex: If you want to download CentOS-6 version then use below command.
#docker pull centos:6
Here “6” is the tag name.
Download Ubuntu image for our container by entering following command
To list all available images present in your host enter “docker images” command.
Next, we will run a container from this image. To run a container run the following command
docker run --name lampstack -p 80:80 -ti ubuntu /bin/bash
Here we build a container with the name “lampstack” and opened port 80 for Apache, from the Ubuntu image.
–name : Container name
-it : Interactive terminal
-p (port): Outgoing : Incoming port number
/bin/bash : Command to be run on the container
To exit from the Ubuntu container shell, press Ctrl + P + Q
Docker ps -a command help to list all running containers. Here we can see our container lampstack is created 11 seconds ago.
To start and stop docker container you can use following commands
#docker stop lampstack #docker start lampstack #docker restartlampstack
By default docker logs will be written in /var/log/docker. Below screenshot shows the logs of stopping and starting a container.
As I mentioned earlier here we are creating a container to run a website. To run a website we need Apache webserver, a MySQL database server, and PHP language
To log in to the container issue the following command
# docker exec -it lampstack bash
After logging into the container install above-mentioned packages.
Please refer any online tutorials for installing above packages in Ubuntu 16.04. Once you installed all the above packages you can test it by accessing your server IP address in the browser. The below image shows the website loading from our “lampstack” docker container.
Now we have setup a Docker container with Apache2, MySQL, and PHP running it.
Next tutorial we will learn how to create an image from the container.