Quantcast
Channel: Oracle
Viewing all articles
Browse latest Browse all 1814

Wiki Page: Getting Started with Docker Engine

$
0
0
Written by Deepak Vohra Consider the use case that Apache Hadoop is required to be installed on Linux. Using the regular method, first the binaries tar files would have to be downloaded and extracted. Subsequently, the environment variables would need to be set, symlinks may need to be created, the configuration files would need to be modified, and the Hadoop nodes would need to be started. All of these; download, installation, configuration and startup, are quite involved and could take a significant amount of time. And, still the result is Apache Hadoop running on a single platform. If Apache Hadoop is to be installed on a different system the complete process would need to be repeated. How Docker Engine makes it easier to Install, Configure and Run Software? Docker Engine is a technology to build, package, and run distributed applications. Any software could be built, packaged and run on Docker Engine, which runs on Linux. Docker Engine provides Dockerfile in which all the software that is to be downloaded packaged and run including environment variables and dependencies may be specified using Dockerfile instructions. Software packaged as Docker images runs in Docker containers, which run in isolation from each other as a lightweight encapsulation of the underlying OS kernel. How is Docker Engine Virtualization better than a Virtual Machine? The virtualization provided by Docker Engine is similar to virtual machines in that it runs software in isolation but Docker Engine is more portable and efficient as it is able to run several lightweight containers that are a micro encapsulation of the same underlying OS in contrast to a virtual machine, which encapsulates a whole OS in addition to the software binaries to run. Each Docker container that runs a Docker image provides its own file system and networking. A Docker container exposes port/s on the host. Docker is supported on most Linux distributions. Docker is also supported on some Windows OS and Mac OS X. In this chapter we shall install Docker on Ubuntu. We shall use an Ubuntu AMI based Amazon EC2 instance. This tutorial has the following sections. Setting the Environment Logging into Ubuntu on Amazon EC2 Installing the Pre-Requisite Packages Installing Docker Engine Testing the Docker Installation Running a Bash Shell Running a Hello World Docker Application Setting the Environment The following software is required for this tutorial. -Docker Engine -Docker Images for Hello World Applications Create an Amazon EC2 instance from AMI Ubuntu Server 14.04 LTS (HVM), SSD Volume Type - ami-d05e75b8. Any of the other supported Linux distributions could also be used but the procedure to install Docker Engine would be slightly different. The minimum kernel version requirement to install Docker Engine on Ubuntu is 3.10. Logging into Ubuntu on Amazon EC2 To login to the AmazonEC2 instance first obtain its Public IP Address. The Public IP would be different for different users. SSH into the Ubuntu instance on Amazon EC2 (Public IP is 52.23.229.182 in following command). ssh -i "docker.pem" ubuntu@52.23.229.182 The Amazon EC2 instance gets logged in. Verify the kernel version with the following command. uname –r The output kernel version of 3.13 is supported as it is more than 3.10. Installing Pre-Requisite Packages Ubuntu package management is based on apt , which stores a list of repositories in the /etc/apt/sources.list list, and Docker's apt repository is kept in the /etc/apt/sources.list.d/docker.list file. Add a new repository key ( gpg key ) for the Docker repository with the following command. sudo apt-key adv --keyserver hkp://pgp.mit.edu:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D A new gpg key gets added. To update the apt sources for the Docker repository in the /etc/apt/sources.list.d/docker.list file find the Ubuntu distribution with the following command. lsb_release –a The Ubuntu distribution listed is Ubuntu Trusty. For Ubuntu Trusty, add the following line to the /etc/apt/sources.list.d/docker.list file; the docker.list file, which may be opened or created with vi editor. deb https://apt.dockerproject.org/repo ubuntu-trusty main Save the /etc/apt/sources.list.d/docker.list file with :wq if vi editor is used. The entry to add for different Ubuntu distributions is listed in following Table. Ubuntu Distribution Entry Ubuntu Precise 12.04 (LTS) deb https://apt.dockerproject.org/repo ubuntu-precise main Ubuntu Trusty 14.04 (LTS) deb https://apt.dockerproject.org/repo ubuntu-trusty main Ubuntu Vivid 15.04 deb https://apt.dockerproject.org/repo ubuntu-vivid main Ubuntu Wily 15.10 deb https://apt.dockerproject.org/repo ubuntu-wily main Run the following commands to update the apt package index. sudo apt-get update Apt package index gets updated as shown in the output from the commands. Purge the old repository with the following command. sudo apt-get purge lxc-docker* The output indicates that the old packages are not installed and therefore not removed. Run the following command to verify that apt is pulling from the updated repository for Docker. sudo apt-cache policy docker-engine The output indicates that the new repository ubuntu-trusty is being used. Update the package manager with the following command. sudo apt-get update The package manager gets updated. Install the linux-image-extra package. sudo apt-get install linux-image-generic-lts-trusty When the preceding command is run select Y if prompted with a message prompt. After the previous command completes reboot the system with the following command. sudo reboot The connection gets closed for reboot. Re-connect with the Amazon EC2 Ubuntu instance with the same ssh command as before. ssh -i "docker.pem" ubuntu@52.23.229.182 After the host system reboots update the package manager again. sudo apt-get update Package manager gets updated. Installing Docker Engine Install Docker Engine with the following command. sudo apt-get install docker-engine Select Y if the following command prompt is displayed. The Docker engine gets installed. Start the Docker service. sudo service docker start Verify the status of the Docker service. sudo service docker status The docker engine is indicated as running as process 2685, which would be different for different users. Next we shall test the Docker installation. Testing the Docker Installation A Docker image is run with the docker run command. Run the Docker image hello-world to test the Docker Engine installation. sudo docker run hello-world The output from the preceding command should be the following, shown in the next illustration. The Docker image gets pulled from library/hello-world and the software packaged in the Docker image runs in a Docker container. The message “Hello from Docker” gets output. The procedure sequence used is also listed. List the Docker images with the following command. sudo docker images The hello-world Docker image gets listed. Docker image detail may be found from the Docker Hub ( https://hub.docker.com/_/hello-world/ ), which has several Docker images. Running a Bash Shell As suggested in the output from running the hello-world Docker image run the ubuntu image. The following command starts a Docker container for the “ubuntu” Docker image. The –it option starts an interactive bash shell for the Docker container. sudo docker run –it ubuntu bash The command prompt becomes root@ . Any Linux command may be run in the interactive terminal. For example, list the files and directories. ls –l The files and directories get listed. The Dockerfile used to build the Docker image “ubuntu” is listed at https://github.com/tianon/docker-brew-ubuntu-core/blob/c390883f9aa4a069c48a16dc01d49dc2643eea25/trusty/Dockerfile . An interactive terminal may be started for only those Docker images that either are based on a Linux image such as “ubuntu” or “debian” or include an explicit command to run a bash shell. The main command in a Docker image is specified in the ENTRYPOINT instruction and the arguments/flags are specified in the CMD instruction. The CMD could used to start a bash shell as in the "ubuntu" Docker image with CMD set to CMD ["/bin/bash"] . Not all Docker images are Linux based or start a bash shell. For example, the hello-world Docker image is not Linux based and does not start a bash shell explicitly, as shown in the Dockerfile. Exit the shell with the “exit” command. The Docker container running the Ubuntu image also exits when the container is no longer being used. List the running Docker containers. sudo docker ps No container gets listed as the one running “ubuntu” has exited when the terminal is exited. List all Docker containers, running or exited. sudo docker ps –a The Docker container used to run “ubuntu” Docker image is listed as Exited. The “Ubuntu” Docker image may be run again with the same command. sudo docker run –it ubuntu bash Exit the terminal with “exit”. List all the Docker containers. sudo docker ps –a When the same command is run again, the same container does not get started but a new container gets started. Running a Hello World Docker Application In this section and the subsections we shall discuss running another Docker image, tututm/hello-world. Pulling the Docker Image A Docker image is run with the docker run command. When the docker run command is provided a Docker image as an argument the Docker image gets pulled from its online repository if not available locally. Alternatively, the Docker image may be first pulled separately using the docker pull command. Pull the tutum/hello-world Docker image. sudo docker pull tutum/hello-world The Docker image gets pulled. Listing the Docker Images List the Docker images with the docker images command. sudo docker images The tutum/hello-world Docker image gets listed in addition to any other Docker images listed previously. Running the Docker Image Run the Docker image tutum/hello-world to start a Docker container named hello-world-app . The –d command parameter indicates that the Docker container should be started in detached mode. In detached mode a Docker container runs in the background. A Docker container running in detached mode exits when the process used to run the container exits. The default mode, if -d is not specified, is foreground mode. In foreground mode the software is run in the container and a console is attached to the process's standard input, output and standard error streams. The –p parameter sets the port as 80. sudo docker run -d -p 80 --name hello-world-app tutum/hello-world A Docker container gets started to run the software packaged in the Docker image tutum/hello-world. Listing the Docker Containers List the Docker containers. sudo docker ps A Docker container called hello-world-app for Docker image tutum/hello-world is listed as running. A Docker container id is assigned to the container. The container port exposed to the host is listed as 32768. The application running in the Docker container may be invoked on the command line with curl or in a web browser. We shall discuss each of these. Invoking the Docker Application Invoke the hello-world application with the following curl command. curl http://localhost:32768 The command output is HTML markup. Testing the Hello World Application in a Browser As the hello-world application generates HTML markup the application may be invoked in a web browser. Find the Public DNS of the Ubuntu instance from the Amazon EC2 Console. As we shall be invoking the Docker application from a local machine we need to enable all traffic between the Ubuntu host and the local machine. Adding the “default” security group to an Amazon EC2 instance opens all traffic to and from the host. To add the “default” security group if not already added select Actions>Networking>Change Security Groups. In the Change Security Groups page select the “default” security group and click on Assign Security Groups. The port to use to invoke the hello-world application may be obtained from the output of the docker ps command or alternatively the docker port command may be used. sudo docker port hello-world-app The port gets listed as 32768. Using the Public DNS and the port invoke the application in a web browser on the local machine with url http://ec2-52-23-229-182.compute-1.amazonaws.com:32768/ . The HTML output for the hello-world application gets displayed. Stopping a Docker Application To stop a Docker container hello-world-app run the docker stop command. sudo docker stop hello-world-app Subsequently, list the running containers and also all containers, running or exited. sudo docker ps sudo docker ps -a The Docker container for the tutum/hello-world image is listed as Exited and does not get listed as a running container. The Docker container may be removed with the docker rm command. The container id or name may be used to remove a container. sudo docker rm d4070ecc9db9 Removing a Docker Image If the docker images command lists the tutum/hello-world image, the image may be removed with the docker rmi command. sudo docker images sudo docker rmi tututm/hello-world The tutum/hello-world image gets removed. To remove all images in a single command run the following command. sudo docker rmi $(sudo docker images –q) All Docker images get removed. Sometimes a Docker image could be downloaded improperly or incompletely and gets listed as . Such an image is called a dangling image. To remove all dangling Docker images run the following command. sudo docker rmi $(sudo docker images -f "dangling=true" -q) The dangling Docker images get removed. In this tutorial we introduced Docker Engine. We discussed installing Docker Engine and testing the installation with the hello-world Docker image. We also discussed running Docker image tutum/hello-world. We invoked the hello-world application on the command line and in a web browser.

Viewing all articles
Browse latest Browse all 1814

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>