In this post, I want to help to start with Docker for the basis. I’m starting to learn Docker, then the following posts are my study step-by-step. Please comment below or in the forum is something is wrong.
What is Docker?
- an open-source project for automating the deployment of applications as portable, self-sufficient containers that can run on the cloud or on-premises
- is also a company that promotes and evolves this technology, working in collaboration with cloud (like Azure), Linux, and Windows vendors, including Microsoft.
So, Docker containers can run anywhere, on-premises in the customer datacenter, in an external service provider or in the cloud, on Azure. Docker image containers can run natively on Linux and Windows. However, Windows images can run only on Windows hosts and Linux images can run on Linux hosts and Windows hosts (using a Hyper-V Linux VM, so far), where host means a server or a VM.
Then, developers can use development environments on Windows, Linux, or macOS. On the development computer, the developer runs a Docker host where Docker images are deployed, including the app and its dependencies. Developers who work on Linux or on macOS use a Docker host that is Linux based, and they can create images only for Linux containers. Developers who work on Windows can create images for either Linux or Windows Containers.
First step in this long post is to install Docker. In the office website, there is a section for Getting started where you can download the version of Docker for your operating system. After the installation, you will restart your machine.
Then, I recommend to create an account in Docker to download containers from the hub.
Create a website
In this tutorial, to start with Docker, I want to create a website cloning a repository from GitHub. For this reason, the first operation to perform is to download and use locally a Docker image. For the purpose of this tutorial, I’m going to use Ubuntu. As a reminder, if you are using Windows 10, you take advantage from Microsoft’s Windows Subsystem for Linux.
Download the first Docker image
Getting images onto your Docker host is called “pulling“. To pull the first image, open a Command Prompt or Terminal and type:
docker image pull ubuntu:latest
How you can see in the screenshot, Docker downloads the image.
latest: Pulling from library/ubuntu da7391352a9b: Already exists 14428a6d4bcd: Already exists 2c2d948710f2: Already exists Digest: sha256:c95a8e48bf88e9849f3e0f723d9f49fa12c5a00cfc6e60d2bc99d87555295e4c Status: Downloaded newer image for ubuntu:latest docker.io/library/ubuntu:latest
Now, if you open Docker Desktop, you can see a new image called Ubuntu.
If you want to have a list of Docker image in your computer, run in your terminal this command:
The result is a list of the Docker images installed in your computer.
How you can see in the image above, each image as an
Image ID. This is quite important for future operations such as copying files or delete an image.
When referencing images, you can refer to them using either IDs or names. If you’re working with image ID’s, it’s usually enough to type the first few characters of the ID – as long as it’s unique. Docker will know which image you mean.
Create a container
Now that we have an image pulled locally, we can use the Docker command to launch a container from it. In your prompt, type
docker container run -it ubuntu:latest /bin/bash
Look closely at the output from the previous commands. You should notice that the shell prompt has changed in each instance. This is because the
-it flags switch your shell into the terminal of the container. You are literally inside of the new container!
Let’s examine that docker container run command.
docker container run tells the Docker daemon to start a new container. The
-it flags tell Docker to make the container interactive and to attach the current shell to the container’s terminal (we’ll get more specific in the chapter on containers).
So far, I gave you some information about Docker and images. Now, we start the funny part.
Download a project from GithHub
In this section, we are going to clone a simple project that contains a website and run it as a container.
First, in one of your local folders, open the Command Prompt and clone this project:
git clone https://github.com/erossini/psweb
So, we can build a container from this file. Enter in folder that your clone. There are few files for the project. Take a look to the
# Linux x64 FROM alpine LABEL maintainer="email@example.com" # Install Node and NPM RUN apk add --update nodejs nodejs-npm # Copy app to /src COPY . /src WORKDIR /src # Install dependencies RUN npm install EXPOSE 8080 ENTRYPOINT ["node", "./app.js"]
This file tells Docker to download an image called alpine, then run
apk, copy the files from the local path to the
src folder. Then, run
EXPOSE tells to what port the webserver is listening. Now, you are going to build the container from this Dockerfile. In the prompt type:
docker image build -t test:latest .
After a minute or two, Docker has built the image and restore all the
npm packages. Ready for the next step.
Run the container
Finally, we can run the container. Type:
docker container run -d --name web1 --publish 8080:8080 test:latest
This command doesn’t do much but you if you the browser… type:
you have your website up and running.
3 thoughts on “Start with Docker”
[…] we have to understand a bit of Docker: you can read this post to have good start and install Docker in your machine. Then, we can use […]
[…] First step in this long post is to install Docker. In the office website, there is a section for Getting started where you can download the version of Docker for your operating system. After the installation, you will restart your machine. For more details, you can visit my post “Start with Docker“. […]
[…] as any other projects in R, the deployment will be with a Docker container. I have created another post where I explain how to deploy R application with Docker and […]
You must log in to post a comment.