Docker
Installation
- Sign up for a docker ID
- Choose docker CE from docker store
- Open terminal, run
docker -v
docker run busybox echo hello world
docker ps -a
docker rm <image_name>
linux container)
Playing with Alpine (very smalldocker run --name myalpine -it --rm alpine sh
for git bash in windows:winpty docker run --name myalpine -it --rm alpine sh
- in separate terminal:
docker ps -a
- in alpine shell:
apk update && apk add --no-cache curl git zsh nano
- install oh-my-szh:
sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
nano ~/.zshrc
- choose your theme, and change ZSH_THEME. e.g.:
ZSH_THEME="bira"
ctrl + o
,ctrl + x
zsh
exit
x2
node docker image
Playing withdocker run -p 3000:3000 --name mynode -it --rm node:alpine sh
node -v
npm -v
npm install -g create-react-app
create-react-app --version
mkdir ~/Projects
cd ~/Projects
create-react-app test-react
cd test-react
npm start
- Open in
localhost:3000
host browser
Starting postgres
docker pull postgres:alpine
docker run -p 5432:5432 --name purwadhika-postgres1 -e POSTGRES_PASSWORD=pwd123 -d postgres:alpine
docker ps -a
docker exec -it purwadhika-postgres1 psql --version
docker exec -it purwadhika-postgres1 psql -U postgres -c 'SELECT version();'
docker exec -it -e PGPASSWORD=pwd123 purwadhika-postgres1 psql -U postgres
\l
create database test;
\c test
create table users (name text, email text);
\d
insert into users values ('kemal', 'kemalelmizan@gmail.com');
select * from users;
\q
to quit
pg_dump
Postgres without daemon, e.g. to run docker run -it --rm postgres:alpine bash
pg_dump --help
exit
Starting mysql
docker pull mysql:latest
docker run -p 3306:3306 --name purwadhika-mysql1 -e MYSQL_ROOT_PASSWORD=pwd1234 -d mysql:latest
docker ps -a
docker exec -it purwadhika-mysql1 mysql --version
docker exec -it purwadhika-mysql1 mysql -h"localhost" -P"3306" -u"root" -p"pwd1234" -e"SELECT VERSION();"
docker exec -it purwadhika-mysql1 mysql -u"root" -p"pwd1234"
create database test;
show databases;
use test;
create table users (name text, email text);
describe users;
show tables;
insert into users values ('kemal', 'kemalelmizan@gmail.com');
select * from users;
\q
to quit
Starting mongo
docker run -p 27017:27017 --name purwadhika-mongo1 -d mongo:latest
docker exec -it purwadhika-mongo1 mongo
- Run mongo commands in CLI
Starting redis
docker run -p 6379:6379 --name purwadhika-redis1 -d redis:alpine
docker exec -it purwadhika-redis1 redis-cli
info
set user:123 token123abc ex 3600
get user:123
ttl user:123
set user:124 token124abc
expire user:124 3600
ttl user:124
exit
Creating your own app image
- Create
hello.js
console.log('Hello world!')
node hello.js
- Create
Dockerfile
FROM node:alpine
COPY ./hello.js /
CMD [ "node", "hello.js" ]
- Build your image :
docker build . -t node-hello
- Running container :
docker run --name hello-container1 -d node-hello
- See container log :
docker logs hello-container1
- Remove container :
docker rm hello-container1
- Remove image :
docker image rm node-hello
Dockerizing Node.js Web App
Follow this tutorial
Docker common commands
- Docker process list :
docker ps -a
- Starting container :
docker start <name/ID>
- Stopping container :
docker stop <name/ID>
- Removing container :
docker rm <name/ID>
- List docker images :
docker images
- Removing image :
docker image rm <image id>
- Copy files from container to host :
docker cp <container name>:/ .