|
|
# Learn Docker basics
|
|
|
Now you might be thinking :thinking: about why we need to learn docker. The reason is it becomes hard to manage dependencies as we build large projects and if we want to deploy them in various environments. So we will be using docker to deploy our product. For this you don't need to master docker but need to have basic skills. Learn Docker Basics here.
|
|
|
# Docker basics
|
|
|
|
|
|
### What will you learn ?
|
|
|
* Introduction to docker
|
|
|
* Docker Usage and flow
|
|
|
|
|
|
# Index
|
|
|
1. [Docker Terminologies](#docker-terminologies)
|
|
|
2. [Docker Components](#docker-components)
|
|
|
3. [Docker Installation (ubuntu16.04)](#docker-installation)
|
|
|
2. [Docker Installation (ubuntu16.04)](#docker-installation)
|
|
|
4. [Docker Basic Command](#docker-basic-command)
|
|
|
5. [Creating Docker application](#creating-an-application)
|
|
|
|
|
|
# Docker Terminologies
|
|
|
## 1. Docker
|
|
|
* Docker is a platform for developers and system admins to develop, deploy, and run applications with containers.
|
|
|
* Docker supports multiple applications with different application requirements and dependencies, to be hosted together on the same host, as long as they have the same operating system requirements.
|
|
|
|
|
|
## 2. Docker v/s Virtual machine
|
|
|
![image](/uploads/ea3b6d3f450879a4da41bdb235e2f529/image.png)
|
|
|
## Why docker ?
|
|
|
It becomes hard to manage dependencies as we build large projects and if we want to deploy them in various environments. So we will be using docker to deploy our product. For this you don't need to master docker but need to know the basics.
|
|
|
|
|
|
* Containers are containing the binaries, libraries, and the application itself. Containers do not contain a guest operating system which ensures that containers are lightweight.
|
|
|
* In contrast, the virtual machine is running on the hypervisor(responsible to run virtual machines) including its own operating system which makes it significantly large. Hence it makes virtual machine complex to set and takes more resources.
|
|
|
# Docker Terminologies
|
|
|
## 1. Docker
|
|
|
* Docker is a program for developers to develop, and run applications with containers.
|
|
|
|
|
|
## 3. Docker Image
|
|
|
* A Docker image is containing everything needed to run an application as a container. This includes:
|
|
|
## 2. Docker Image
|
|
|
* A Docker image is contains everything needed to run an applications as a container. This includes:
|
|
|
* code
|
|
|
* runtime
|
|
|
* libraries
|
|
|
* environment variables
|
|
|
* configuration files
|
|
|
* The image can then be deployed to any Docker environment and executable as a container.
|
|
|
* The image can then be deployed to any Docker environment and as a container.
|
|
|
|
|
|
## 4. Container
|
|
|
* A Docker container is a runtime instance of an image.
|
|
|
* From one image you can create multiple containers (all running the sample application) on multiple Docker platforms.
|
|
|
* You can create multiple containers for multiple applications in a docker.
|
|
|
## 3. Container
|
|
|
* A Docker container is a running Docker image.
|
|
|
* From one image you can create multiple containers .
|
|
|
|
|
|
## 5. Docker file
|
|
|
* Docker images are a set of instructions used to create docker containers. Docker image is created using a docker file.
|
|
|
|
|
|
# Docker Components
|
|
|
## Docker Engine
|
|
|
* Docker Engine is one of the core components of Docker. It is responsible for the overall functioning of the Docker platform.
|
|
|
* Docker Engine is a client-server based application and consists of 3 main components.
|
|
|
* Server: It is responsible for creating and managing Docker Images, Containers, Networks and Volumes on the Docker platform.
|
|
|
* REST API: The REST API specifies how the applications can interact with the Server, and instruct it to get their job done.
|
|
|
* Client: The Client is nothing but a command-line interface, that allows users to interact with Docker using the commands.
|
|
|
|
|
|
![image](/uploads/b9c35944c61a9d15a0dbd895a00b36f8/image.png)
|
|
|
|
|
|
## Docker Hub
|
|
|
Docker Hub is the official online repository where you could find all the Docker Images that are available for us to use.
|
|
|
## 4. Docker Hub
|
|
|
* Docker Hub is like GitHub but for docker images and containers.
|
|
|
|
|
|
# Docker Installation
|
|
|
|
|
|
Learn to Install docker on to your Ubuntu 16.04.
|
|
|
|
|
|
1. Uninstall the older version of docker if is already installed
|
|
|
```sh
|
|
|
$ sudo apt-get remove docker docker-engine docker.io containerd runc
|
... | ... | @@ -79,30 +65,9 @@ $ sudo apt-get install docker-ce docker-ce-cli containerd.io |
|
|
$ sudo docker run hello-world
|
|
|
```
|
|
|
|
|
|
[Referred link](https://docs.docker.com/install/linux/docker-ce/ubuntu/)
|
|
|
|
|
|
|
|
|
# Docker basic command
|
|
|
[Referred link](https://medium.com/free-code-camp/docker-simplified-96639a35ff36#06d9)
|
|
|
## docker create
|
|
|
* This command allows us to create a new container.
|
|
|
* Syntax
|
|
|
```sh
|
|
|
docker create [options] IMAGE [commands] [arguments]
|
|
|
```
|
|
|
`Note: Anything enclosed within the square brackets is optional.`
|
|
|
|
|
|
e.g.
|
|
|
```sh
|
|
|
$ docker create fedora
|
|
|
|
|
|
02576e880a2ccbb4ce5c51032ea3b3bb8316e5b626861fc87d28627c810af03
|
|
|
```
|
|
|
In the above example, the docker create command would create a new container using the latest Fedora image.
|
|
|
|
|
|
if container creation is successful the command will return `unique container ID`, in above example it `02576e880a2ccbb4ce5c51032ea3b3bb8316e5b626861fc87d28627c810af03`. We refer to the container using it's container ID.
|
|
|
# Docker basic commands
|
|
|
|
|
|
Refer given link for more details.
|
|
|
Learn basic docker commands
|
|
|
|
|
|
## docker ps
|
|
|
* The docker ps command allows us to view all the containers that are running on the Docker Host.
|
... | ... | @@ -164,92 +129,122 @@ Whereas in this example, Docker will stop the container named sneha. |
|
|
## docker rm
|
|
|
|
|
|
|
|
|
## Creating an application
|
|
|
1. Create a folder with these 2 files inside it `Dockerfile` and `main.py`. Normally you should have this folder structure.
|
|
|
## Common Operations on Dockers
|
|
|
|
|
|
Typically, you will be using docker in the given flow.
|
|
|
|
|
|
1. Download/pull the docker images that you want to work with.
|
|
|
1. Copy your code inside the docker
|
|
|
1. Access docker terminal
|
|
|
1. Install and additional required dependencies
|
|
|
1. Compile and Run the Code inside docker
|
|
|
1. Document steps to run your program in README.md file
|
|
|
1. Commit the changes done to the docker.
|
|
|
1. Push docker image to the docker-hub and share repository with people who want to try your code.
|
|
|
|
|
|
---
|
|
|
|
|
|
Commands for doing the operations on docker are
|
|
|
|
|
|
**1. Download the docker.**
|
|
|
|
|
|
```shell
|
|
|
docker pull snehabhapkar/trydock
|
|
|
```
|
|
|
.
|
|
|
├── Dockerfile
|
|
|
└── main.py
|
|
|
0 directories, 2 files
|
|
|
|
|
|
This is an empty docker image.
|
|
|
|
|
|
**2. Run the docker image with this command.**
|
|
|
|
|
|
```shell
|
|
|
docker run -ti snehabhapkar/trydock /bin/bash
|
|
|
```
|
|
|
2. Edit main.py
|
|
|
```sh
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
print("Docker is magic!")
|
|
|
Which will output like this
|
|
|
|
|
|
```shell
|
|
|
root@e0b72ff850f8:/#
|
|
|
```
|
|
|
3. Edit Dockerfile
|
|
|
```sh
|
|
|
# A dockerfile must always start by importing the base image.
|
|
|
# We use the keyword 'FROM' to do that.
|
|
|
# In our example, we want import the python image.
|
|
|
# So we write 'python' for the image name and 'latest' for the version.
|
|
|
FROM python:latest
|
|
|
|
|
|
# In order to launch our python code, we must import it into our image.
|
|
|
# We use the keyword 'COPY' to do that.
|
|
|
# The first parameter 'main.py' is the name of the file on the host.
|
|
|
# The second parameter '/' is the path where to put the file on the image.
|
|
|
# Here we put the file at the image root folder.
|
|
|
COPY main.py /
|
|
|
|
|
|
# We need to define the command to launch when we are going to run the image.
|
|
|
# We use the keyword 'CMD' to do that.
|
|
|
# The following command will execute "python ./main.py".
|
|
|
CMD [ "python", "./main.py" ]
|
|
|
```
|
|
|
4. Create the Docker image
|
|
|
* Once your code is ready and the Dockerfile is written, all you have to do is create your image to contain your application.
|
|
|
```sh
|
|
|
$ docker build -t python-test .
|
|
|
|
|
|
This will create a container ID in your system for this image. Container ID in my case is e0b72ff850f8.
|
|
|
|
|
|
**3. Let copy our code inside docker with this command.** (Make sure you are not inside docker terminal, enter `exit` in command line to get out of container terminal)
|
|
|
|
|
|
* In this case, lets copy a simple python program which just prints "hello, I am talking from container". So lets consider you have this file hello.py
|
|
|
|
|
|
```python
|
|
|
print("hello, I am talking from container")
|
|
|
```
|
|
|
* The ’-t’ option allows you to define the name of your image. In our case we have chosen ’python-test’ but you can put what you want.
|
|
|
|
|
|
5. Run the Docker image
|
|
|
* Once the image is created, your code is ready to be launched.
|
|
|
```sh
|
|
|
$ docker run python-test
|
|
|
* Copy file inside the docker container
|
|
|
|
|
|
```shell
|
|
|
docker cp hello.py e0b72ff850f8:/
|
|
|
```
|
|
|
* You need to put the name of your image after `docker run`.
|
|
|
That’s it. You should normally see “Docker is magic!” displayed in your terminal.
|
|
|
|
|
|
## Push your image on Dockerhub
|
|
|
[Referred link](https://medium.com/@deepakshakya/beginners-guide-to-use-docker-build-run-push-and-pull-4a132c094d75)
|
|
|
where e0b72ff850f8 is the containerID. This will copy hello.py inside docker in root directory.
|
|
|
|
|
|
1. Create Dockerhub account
|
|
|
2. Create Repository
|
|
|
![createrepo](extras/createrepo.png)
|
|
|
* All write a script for installing dependencies - requirements.sh
|
|
|
|
|
|
Click on Create Repository button, put the name and enter.
|
|
|
![repo2](extras/repo2.png)
|
|
|
```
|
|
|
apt update
|
|
|
apt install python3
|
|
|
```
|
|
|
|
|
|
3. Now we will tag the image and push it to dockerhub repository which we just created.
|
|
|
```sh
|
|
|
# Run this commend to list docker images
|
|
|
$ docker images
|
|
|
* Copy file inside docker
|
|
|
|
|
|
```shell
|
|
|
docker cp requirements.sh e0b72ff850f8:/
|
|
|
```
|
|
|
which will give you this list
|
|
|
```sh
|
|
|
REPOSITORY TAG IMAGE ID CREATED SIZE
|
|
|
snehabhapkar/python-test latest c7857f97ebbd 2 hours ago 933MB
|
|
|
|
|
|
**4. Install dependencies**
|
|
|
|
|
|
* Give permission to run shell script
|
|
|
|
|
|
```shell
|
|
|
docker exec -it e0b72ff850f8 chmod +x requirements.sh
|
|
|
```
|
|
|
Image ID is used to tag the image. So lets tag syntax is `docker tag <image-id of docker-whale> <your dockerhub username>/docker-whale:latest`
|
|
|
```sh
|
|
|
$ docker tag c7857f97ebbd snehabhapkar/python-test:latest
|
|
|
# Push image to the repository
|
|
|
$ docker push snehabhapkar/python-test
|
|
|
|
|
|
* Install dependencies
|
|
|
|
|
|
```shell
|
|
|
docker exec -it e0b72ff850f8 /bin/bash ./requirements.sh
|
|
|
```
|
|
|
|
|
|
## Pull and run the image
|
|
|
**5. Run the program inside container with this command.**
|
|
|
|
|
|
1. Let’s remove all versions of python-test image on our local system. Use Image ID to do that.
|
|
|
```sh
|
|
|
$ docker rmi -f c7857f97ebbd
|
|
|
```shell
|
|
|
docker start e0b72ff850f8
|
|
|
docker exec e0b72ff850f8 python3 hello.py
|
|
|
```
|
|
|
|
|
|
2. Lets run the image, if it doesn't find it on local machine, it fetched it from dockerhub and run.
|
|
|
```sh
|
|
|
$ docker run snehabhapkar/python-test
|
|
|
where e0b72ff850f8 is the containerID.
|
|
|
|
|
|
**6. Save your copied program inside docker image with docker commit.**
|
|
|
|
|
|
```shell
|
|
|
docker commit e0b72ff850f8 snehabhapkar/trydock
|
|
|
```
|
|
|
|
|
|
where e0b72ff850f8 is the containerID.
|
|
|
|
|
|
**7. Push docker image to the dockerhub and share repository name in the summary of assignment.**
|
|
|
|
|
|
* Tag image name with different name
|
|
|
|
|
|
```shell
|
|
|
docker tag snehabhapkar/trydock username/repo
|
|
|
```
|
|
|
|
|
|
where username is your username on dockerhub and repo is the name of image.
|
|
|
|
|
|
* Push on dockerhub
|
|
|
|
|
|
```shell
|
|
|
docker push username/repo
|
|
|
```
|
|
|
This will give the desired output.
|
|
|
|
|
|
and submit the link to your repository in the docker summary.
|
|
|
|
|
|
|
|
|
|