steadylearner / docker-examples

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to learn docker

Start with these.

  1. Official Website

  2. Docker Curriculum

  3. Reponsive Course

Install

Search and read the documenation for your system or $docker in your CLI and follow the instruction.

Verify it

Refer to this..

$docker search nginx
$docker pull nginx
$docker run --name nginx-webserver -p 80:80 nginx

Then visit localhost and will show this.

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

Start the docker container in detached mode and execute commands with bash

$docker run --name nginx-webserver -p 80:80 -d nginx

execute commands for the container with

$docker exec -it CONTAINER_ID bash

Use it with ubuntu

$docker pull ubuntu
$docker run -it ubuntu sh

Then, install Curl first to download other programs.

$apt-get update
$apt-get install curl
$curl https://www.steadylearner.com

If you are out of the container, you can start it with

$docker exec -it CONTAINER_id bash

Read more about docker lifecycle.

If you want to set up the Node development environment. Follow these processes.

You can use Dockerfile or $docker run -d steadylearner/ubuntu_node instead.

Node, NPM, Yarn
curl -sL https://deb.nodesource.com/setup_12.x | bash

will show this.

## Run `sudo apt-get install -y nodejs` to install Node.js 12.x and npm
## You may also need development tools to build native addons:
    sudo apt-get install gcc g++ make
## To install the Yarn package manager, run:
    curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
    echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
    sudo apt-get update && sudo apt-get install yarn

Command without sudo for root permission

apt-get install gcc g++ make
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list

apt-get update && apt-get install yarn

Follow the commmands and install them all.

Test node work with

$node
$console.log("Hello from www.steadylearner.com");
Vim
apt install --assume-yes vim

It will install Vim text editor.

$vim hello.js

Type

console.log("Hello from www.steadylearner.com");

:wq to save and quit.
$node hello.js
Hello from www.steadylearner.com
Git
$apt-get --assume-yes git-core

It will install git and verify it with

$git --version

Then, use your github user name and email

$git config --get user.name
$git config --get user.email

and use them in docker container

$git config --global user.name yourname
$git config --global user.name youremail

use the same command(--get) before to verify it

Test git clone work to download files from your previous project

For example, clone steadylearner/docker-examples repository with this.

$git clone https://github.com/steadylearner/docker-examples.git

Test them

Verify the yarn version first.

$yarn -v

Start a Node project.

$cd /home
$mkdir node && cd node
$yarn init
$yarn add chalk

Test it work with this.

// $node

const chalk = require("chalk");
const blue = chalk.blue;
const hello = blue("Hello from www.steadylearner.com");
console.log(hello);

We verified that NPM modules work in a docker container with this.

If you want, use this also.

$vim ~/.bashrc

Type

alias work="cd /home/node"
then :wq to save and quit
$source ~/.bashrc

Then, you can visit your node project with $work whenever you want. You can also define WORKDIR later in Dockerfile or docker-compsose.yml instead for the similar purpose.

List and remove previous docker instances

$docker ps -a

It will show the list of the container instances you executed before. Then, use these commands to remove what you wouldn't use.

$docker stop containerid
$docker rm containerid

or

$docker rm containerid -f

How to move your local files and folers to docker contaienrs

We could use git commands to donwload files from the GitHub. We will also learn how to use docker commands to move local files and folder in your conatiners and vice versa.

  1. Files
$docker cp from_localhost.txt containerid:/from_localhost.txt
$docker cp containerid:/from_docker from_docker.txt
  1. Folders
$docker cp from_localhost containerid:/from_localhost
$docker cp containerid:/from_localhost from_localhost

How to use the web framework with docker containers

Express

Install the dependencies we will use inside the docker container with this.

$yarn add express chalk

Then, we will build hello-world app with JavaScript code below.

// server.js
const express = require('express')
const chalk = require("chalk");

const app = express()
const port = 3000

app.get('/', (req, res) => res.send('Hello World!'))

const blue = chalk.blue
const target = blue(`http://localhost:${port}`)

app.listen(port, () => console.log(`Express Server ready at ${target}`))

Then, $node server.js will show this message.

Express Server ready at http://localhost:3000

But $curl http://localhost:3000 or visiting it in your browser won't work yet.

Each container has its own ip and we should inspect the docker container with $docker inspect containerid > inspect.txt to find them.

It will be similar to 172.17.0.2.

You can also make getIP.js and $node getIP.js to save your time.

const fs = require('fs')

const filename = "inspect.txt";
in
fs.readFile(filename, 'utf8', function(err, data) {
  if (err) throw err;

  // console.log(`Read ${filename}`);
  const dataObject = JSON.parse(data);

  // console.log(payload);
  // console.log(typeof payload);

  const ip = dataObject[0].NetworkSettings.IPAddress;
  console.log(`IP is ${ip}`);
});

$curl http://172.17.0.2:3000/ or verify this in your browser.

Hello World!

You could also use $docker port command.

$docker port --help
$docker port containername

or

$docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' containerId

Refer to the inspect command from the docker official website.

Useful Commands

Logs of the container

$docker logs containerid | name

History of the image

#docker history steadylearner/ubuntu_node

Remove unused images

$docker images
$docker image rm dockerimagename or docker rmi

Rename the container

$docker rename randomname whatyouwant

You can also use id instead of name also.

Pause and unpause the containers

$docker pause containerid | name
$docker ps -a
$docker unpuase containerid | name
$docker ps -a

Start and stop them

$docker stop containerid | name
$docker ps -a
$docker start containerid | name
$docker ps -a

Remove containers

$docker container rm containerid | name

How to modify network ports for docker image

Search and start with custom port you want to use.

$docker run -it --name ubuntu_node -p 80:80 ubuntu

By default, the port on the host is mapped to 0.0.0.0, which means all IP addresses. You can specify a particular IP address when you define the port mapping,> for example, -p 127.0.0.1:80:80

docker run -d --name ubuntu_node -p 80:80 ubuntu:latest

-d make the container run in background.

Images and Containers

Containers are istances of images.

  1. You can pull or run(pull and start) images and it will make docker containers in your local machine.

  2. You can edit containers with $docker exec -it containername bash.

  3. Make images from them with $docker commit containername account/image && docker push account/image.

or you can start with Dockerfile instead of 1. and 2. and commit your docker images.

How to create a docker image from a container

Create a repository first at Dockerhub and login to your CLI with this.

$docker login

Then, use $docker commit. Refer to this blog post

$docker commit ubuntu_node

Then, verify it with

$docker images

Give it tag(name) with

$docker tag imageid steadylearner/ubuntu_node

or you could execute this command instead of them.

$docker commit ubuntu_node steadylearner/ubuntu_node

How to push your docker image to Docker Hub

$docker push steadylearner/ubuntu_node // yourusername/image

Wait for uploading process to complete and use this.

$docker run -it steadylearner/ubuntu_node bash

Then, follow the same process you used before to test it worked and you may exit the container.

You can restart the containers with this.

$docker restart containerid
$docker exec -it containerid bash

You can remove container made from steadylearner/ubuntu_node container or yours with this.

$docker stop ubuntu_node
$docker container rm ubuntu_node
$docker image rm ubuntu

or rename the container.

$docker container rename randomname ubuntu_node

Use yours instead of ubuntu_node or steadylearner/ubuntu_node.

How to push the container after you modify it

Modify the project then use the commands similar to this.

$docker commit ubuntu_node steadylearner/ubuntu_node

or

$docker commit --message "Test message and will be similar to github -m option" ubuntu_node steadylearner/ubuntu_node

with commit message.

Then use this to commit the image made from it.

$docker push steadylearner/ubuntu_node

and

$docker run -it steadylearner/ubuntu_node bash

or

$docker history steadylearner/ubuntu_node

to verify the result.

How to use database with docker containers

You should learn how volume and compose-file work first.

Postgresql
  1. Official Example

  2. Read the pratical example

How to deploy your docker images

  1. Code

  2. Make docker images with Dockerfile.

  3. Deploy them with docker-compose. .

  4. Test end points with Curl or other test frameworks.

  5. Learn SDK for Docker, aws and others

You may read this and test it in your local machine.

READ MORE

  1. Building Blocks of Amazon ECS
  2. rds or docker Cloudstor plugin with volume to use postgresql.

About


Languages

Language:JavaScript 95.8%Language:Dockerfile 3.2%Language:Python 1.0%