Sunteți pe pagina 1din 13

First things first: what is DockerHub and why is it important?

DockerHub is a cloud-based repository run and managed by Docker Inc. It's an online repository where Docker
images can be published and used by other users. There are both public and private repositories. If you are a
company, you can have a private repository for use within your own organization, whereas public images can
be used by anyone.

You can also use official Docker images that are published publicly. I use many such images, including for my
test WordPress installations, KDE plasma apps, and more. Although we learned last time how to create your
own Docker images, you don't have to. There are thousands of images published on DockerHub for you to use.
DockerHub is hardcoded into Docker as the default registry, so when you run the docker pull command against
any image, it will be downloaded from DockerHub.

Download images from Docker Hub and run locally


Please check out the previous articles in the series to get started. Then, once you have Docker running on your
system, you can open the terminal and run:

$ docker images
This command will show all the docker images currently on your system. Let's say you want to deploy Ubuntu
on your local machine; you would do:

$ docker pull ubuntu


If you already have Ubuntu image on your system, the command will automatically update that image to the
latest version. So, if you want to update the existing images, just run the docker pull command, easy peasy. It's
like apt-get upgrade without any muss and fuss.

You already know how to run an image:

$ docker run -it <image name>

$ docker run -it ubuntu


The command prompt should change to something like this:

root@1b3ec4621737:/#
Now you can run any command and utility that you use on Ubuntu. It's all safe and contained. You can run all
the experiments and tests you want on that Ubuntu. Once you are done testing, you can nuke the image and
download a new one. There is no system overhead that you would get with a virtual machine.

You can exit that container by running the exit command:

$ exit
Now let's say you want to install Nginx on your system. Run search to find the desired image:

$ docker search nginx


As you can see, there are many images of Nginx on DockerHub. Why? Because anyone can publish an image.
Various images are optimized for different projects, so you can choose the appropriate image. You just need to
install the appropriate image for your use-case.

Let's say you want to pull Bitnami’s Nginx container:

$ docker pull bitnami/nginx


Now run it with:

$ docker run -it bitnami/nginx


How to publish images to Docker Hub?
Previously, we learned how to create a Docker image, and we can easily publish that image to DockerHub.
First, you need to log into DockerHub. If you don't already have an account, please create one. Then, you can
open terminal app and log in:

$ docker login --username=<USERNAME>


Replace <USERNAME> with the name of your username for Docker Hub. In my case it’s arnieswap:

$ docker login --username=arnieswap>


Enter the password, and you are logged in. Now run the docker images command to get the ID of the image that
you created last time.

$ docker images
Now, suppose you want to push the ng image to DockerHub. First, we need to tag that image (learn more about
tags):

$ docker tag e7083fd898c7 arnieswap/my_repo:testing


Now push that image:

$ docker push arnieswap/my_repo


The push refers to repository [docker.io/arnieswap/my_repo]

12628b20827e: Pushed

8600ee70176b: Mounted from library/ubuntu

2bbb3cec611d: Mounted from library/ubuntu

d2bb1fc88136: Mounted from library/ubuntu

a6a01ad8b53f: Mounted from library/ubuntu

833649a3e04c: Mounted from library/ubuntu

testing: digest: sha256:286cb866f34a2aa85c9fd810ac2cedd87699c02731db1b8ca1


cfad16ef17c146 size: 1569
Eureka! Your image is being uploaded. Once finished, open DockerHub, log into your account, and you can see
your very first Docker image. Now anyone can deploy your image. It’s the easiest and fastest way to develop
and distribute software. Whenever you update the image, users can simply run:

$ docker run arnieswap/my_repo


Now you know why people love Docker containers. They solve many problems that traditional workloads face
and allow you develop, test, and deploy applications in no time. And, by following the steps in this series, you
can try them out for yourself.

Learn more about Linux through the free "Introduction to Linux" course from The Linux Foundation and edX.

How to Create a Docker Image


container-image.jpg

Learn the basic steps for creating Docker images in this tutorial.
Creative Commons Zero
Pixabay
In the previous article, we learned about how to get started with Docker on Linux, macOS, and
Windows. In this article, we will get a basic understanding of creating Docker images. There are
prebuilt images available on DockerHub that you can use for your own project, and you can publish
your own image there.

We are going to use prebuilt images to get the base Linux subsystem, as it’s a lot of work to build
one from scratch. You can get Alpine (the official distro used by Docker Editions), Ubuntu,
BusyBox, or scratch. In this example, I will use Ubuntu.

Before we start building our images, let’s “containerize” them! By this I just mean creating
directories for all of your Docker images so that you can maintain different projects and stages
isolated from each other.

$ mkdir dockerprojects

cd dockerprojects
Now create a Dockerfile inside the dockerprojects directory using your favorite text editor; I prefer
nano, which is also easy for new users.

$ nano Dockerfile
And add this line:

FROM Ubuntu
Save it with Ctrl+Exit then Y.

Now create your new image and provide it with a name (run these commands within the same
directory):

$ docker build -t dockp .


(Note the dot at the end of the command.) This should build successfully, so you'll see:

Sending build context to Docker daemon 2.048kB

Step 1/1 : FROM ubuntu

---> 2a4cca5ac898

Successfully built 2a4cca5ac898

Successfully tagged dockp:latest


It’s time to run and test your image:

$ docker run -it Ubuntu


You should see root prompt:
root@c06fcd6af0e8:/#
This means you are literally running bare minimal Ubuntu inside Linux, Windows, or macOS. You
can run all native Ubuntu commands and CLI utilities.

Let’s check all the Docker images you have in your directory:

$docker images

REPOSITORY TAG IMAGE ID CREATE


D SIZE

dockp latest 2a4cca5ac898 1 hour


ago 111MB

ubuntu latest 2a4cca5ac898 1 hour


ago 111MB

hello-world latest f2a91732366c 8 week


s ago 1.85kB
You can see all three images: dockp, Ubuntu, and hello-world, which I created a few weeks ago
when working on the previous articles of this series. Building a whole LAMP stack can be
challenging, so we are going create a simple Apache server image with Dockerfile.

Dockerfile is basically a set of instructions to install all the needed packages, configure, and copy
files. In this case, it’s Apache and Nginx.

You may also want to create an account on DockerHub and log into your account before building
images, in case you are pulling something from DockerHub. To log into DockerHub from the
command line, just run:

$ docker login
Enter your username and password and you are logged in.

Next, create a directory for Apache inside the dockerproject:


$ mkdir apache
Create a Dockerfile inside Apache folder:

$ nano Dockerfile
And paste these lines:

FROM ubuntu

MAINTAINER Kimbro Staken version: 0.1

RUN apt-get update && apt-get install -y apache2 && apt-get clean
&& rm -rf /var/lib/apt/lists/*

ENV APACHE_RUN_USER www-data

ENV APACHE_RUN_GROUP www-data

ENV APACHE_LOG_DIR /var/log/apache2

EXPOSE 80

CMD ["/usr/sbin/apache2", "-D", "FOREGROUND"]


Then, build the image:

docker build -t apache .


(Note the dot after a space at the end.)

It will take some time, then you should see successful build like this:

Successfully built e7083fd898c7

Successfully tagged ng:latest


Swapnil:apache swapnil$
Now let’s run the server:

$ docker run –d apache

a189a4db0f7c245dd6c934ef7164f3ddde09e1f3018b5b90350df8be85c8dc98
Eureka. Your container image is running. Check all the running containers:

$ docker ps

CONTAINER ID IMAGE COMMAND CREATED

a189a4db0f7 apache "/usr/sbin/apache2ctl" 10 seconds ago


You can kill the container with the docker kill command:

$docker kill a189a4db0f7


So, you see the “image” itself is persistent that stays in your directory, but the container runs and
goes away. Now you can create as many images as you want and spin and nuke as many containers
as you need from those images.

That’s how to create an image and run containers.

To learn more, you can open your web browser and check out the documentation about how to
build more complicated Docker images like the whole LAMP stack. Here is a Dockerfile file for
you to play with. In the next article, I’ll show how to push images to DockerHub.

Learn more about Linux through the free "Introduction to Linux" course from The Linux
Foundation and edX.

How to Install Docker CE on Your Desktop


Follow these simple steps to install Docker CE on your Linux, Mac, or Windows desktop.
Creative Commons Zero
In the previous article, we learned some of the basic terminologies of the container world. That
background information will come in handy when we run commands and use some of those terms
in follow-up articles, including this one. This article will cover the installation of Docker on
desktop Linux, macOS, and Windows, and it is intended for beginners who want to get started with
Docker containers. The only prerequisite is that you are comfortable with command-line interface.
Why do I need Docker CE on my local machine?
As a new user, you many wonder why you need containers on your local systems. Aren’t they
meant to run in cloud and servers as microservices? While containers have been part of the Linux
world for a very long time, it was Docker that made them really consumable with its tools and
technologies.

The greatest thing about Docker containers is that you can use your local machine for development
and testing. The container images that you create on your local system can then run “anywhere.”
There is no conflict between developers and operators about apps running fine on development
systems but not in production.

The point is that in order to create containerized applications, you must be able to run and create
containers on your local systems.

You can use any of the three platforms -- desktop Linux, Windows, or macOS as the development
platform for containers. Once Docker is successfully running on these systems, you will be using
the same commands across platforms so it really doesn’t matter which OS you are running
underneath.

That’s the beauty of Docker.

Let’s get started


There are two editions of Docker. Docker Enterprise Edition (EE) and Docker Community Edition
(CE). We will be using the Docker Community Edition, which is a free of cost version of Docker
intended for developers and enthusiasts who want to get started with Docker.

There are two channels of Docker CE: stable and edge. As the name implies, the stable version
gives you well-tested quarterly updates, whereas the edge version offers new updates every month.
After further testing, these edge features are added to the stable release. I recommend the stable
version for new users.
Docker CE is supported on macOS, Windows 10, Ubuntu 14.04, 16.04, 17.04 and 17.10; Debian
7.7,8,9 and 10; Fedora 25, 26, 27; and centOS. While you can download Docker CE binaries and
install on your Desktop Linux systems, I recommend adding repositories so you continue to receive
patches and updates.

Install Docker CE on Desktop Linux


You don’t need a full blown desktop Linux to run Docker, you can install it on a bare minimal
Linux server as well, that you can run in a VM. In this tutorial, I am running it on Fedora 27 and
Ubuntu 17.04 running on my main systems.

Ubuntu Installation
First things first. Run a system update so your Ubuntu packages are fully updated:

$ sudo apt-get update


Now run system upgrade:

$ sudo apt-get dist-upgrade


Then install Docker PGP keys:

sudo add-apt-repository "deb [arch=amd64] https://download.docker.


com/linux/ubuntu $(lsb_release -cs) stable"
Update the repository info again:
$ sudo apt-get update
Now install Docker CE:

$ sudo apt-get install docker-ce


Once it's installed, Docker CE runs automatically on Ubuntu based systems. Let’s check if it’s
running:

$ sudo systemctl status docker


You should get the following output:

docker.service - Docker Application Container Engine


Loaded: loaded (/lib/systemd/system/docker.service; enabled; ven
dor preset: enabled)
Active: active (running) since Thu 2017-12-28 15:06:35 EST; 19mi
n ago
Docs: https://docs.docker.com
Main PID: 30539 (dockerd)
Since Docker is installed on your system, you can now use Docker CLI (Command Line Interface)
to run Docker commands. Living up to the tradition, let’s run the ‘Hello World’ command:

$ sudo docker run hello-world


Congrats! You have Docker running on your Ubuntu system.

Installing Docker CE on Fedora


Things are a bit different on Fedora 27. On Fedora, you first need to install def-plugins-
core packages that will allow you to manage your DNF packages from CLI.

$ sudo dnf -y install dnf-plugins-core


Now install the Docker repo on your system:

$ sudo dnf config-manager \


--add-repo \
https://download.docker.com/linux/fedora/docker-ce.repo
It’s time to install Docker CE:
$ sudo dnf install docker-ce
Unlike Ubuntu, Docker doesn’t start automatically on Fedora. So let’s start it:

$ sudo systemctl start docker


You will have to start Docker manually after each reboot, so let’s configure it to start automatically
after reboots. $ systemctl enable docker Well, it’s time to run the Hello World command:

$ sudo docker run hello-world


Congrats, Docker is running on your Fedora 27 system.
Cutting your roots
You may have noticed that you have to use sudo to run Docker commands. That’s because of
Docker daemon’s binding with the UNIX socket, instead of a TCP port and that socket is owned by
the root user. So, you need sudo privileges to run the docker command. You can add system user to
thedocker group so it won’t require sudo:

$ sudo groupadd docker


In most cases, the docker user group is automatically created when you install Docker CE, so all
you need to do is add your user to that group:

$ sudo usermod -aG docker $USER


To test if the group has been added successfully, run the groups command against the name of the
user:

$ groups swapnil
(Here, Swapnil is the user.)

This is the output on my system:

$ swapnil : swapnil adm cdrom sudo dip plugdev lpadmin sambashare


docker
You can see that the user also belongs to the docker group. Log out of your system, so that group
changes take effect. Once you log back in, try the Hello World command without sudo:

$ docker run hello-world


You can check system wide info about the installed version of Docker and more by running this
command:

$ docker info
Install Docker CE on macOS and Windows
You can easily install Docker CE (and EE) on macOS and Windows. Download the official Docker
for Mac and install it the way you install applications on macOS, by simply dragging them into the
Applications directory. Once the file is copied, open Docker from spotlight to start the installation
process. Once installed, Docker will start automatically and you can see it in the top bar of macOS.
macOS is UNIX, so you can simply open the terminal app and start using Docker commands
natively. Test the hello world app:

$ docker run hello-world


Congrats, you have Docker running on your macOS.

Docker on Windows 10
You need the latest version of Windows 10 Pro or Server in order to run/install Docker on it. If you
are not fully updated, Windows won’t install Docker. I got an error on my Windows 10 system and
had to run system updates. My version was still behind, and I hit this bug. So, if you fail to install
Docker on Windows, just know you are not alone. Keep an eye on that bug to find a solution.

Once you install Docker on Windows, you can either use bash shell via WSL or use PowerShell to
run docker commands. Let’s test the “Hello World” command in PowerShell:

PS C:\Users\swapnil> docker run hello-world


Congrats, you have Docker running on Windows.

In the next article, we will talk about pulling images from DockerHub and running containers on
our systems. We will also talk about pushing our own containers to Docker Hub.

Learn more about Linux through the free "Introduction to Linux" course from The Linux Foundation
and edX.

S-ar putea să vă placă și