bikram-cloud / Kddi_work

some document
0 stars 0 forks source link

jenkins and docker setting documentations #1

Open bikram-cloud opened 4 years ago

bikram-cloud commented 4 years ago

ローカルのmacにセットアップしたdockerの上で動くjenkinsの手順書

pull jenkins with docker

$ docker pull jenkins/jenkins:lts

Locally create jenkins_home

$ docker run -d -v `pwd`/jenkins_home:/var/jenkins_home -p 8080:8080 -p 50000:50000 jenkins/jenkins:lts

List running images

$ docker images ls

List running containers

$ docker container ls

List all containers (Even if not running)

$ docker container ls -a

start jenkins

$ docker start [id]

stop jenkins

$ docker stop [id]

delete jenkins

$ docker rm [id]

dockerでjenkinsをインストールしたらそれぞれのusernameとpasswordを設定する必要があります。

docker jenkinsでgithubとSSHをインストールする方法

bikram-cloud commented 4 years ago

docker を ローカルのmacにセットアップする手順

Docker for Macをインストール

公式サイトからDockerのアカウントを作ってログインし、DockerHubからダウンロードしてインストールします。

https://hub.docker.com/editions/community/docker-ce-desktop-mac

Docker Commands, Help & Tips

Show commands & management commands

$ docker

Docker version info

$ docker version

Show info like number of containers, etc

$ docker info

WORKING WITH CONTAINERS

Create and run a container in foreground

$ docker container run -it -p 80:80 nginx

Create and run a container in background

$ docker container run -d -p 80:80 nginx

Shorthand

$ docker container run -d -p 80:80 nginx

Naming Containers

$ docker container run -d -p 80:80 --name nginx-server nginx

List running containers

$ docker container ls

OR

$ docker ps

List all containers (Even if not running)

$ docker container ls -a

Stop container

$ docker container stop [ID]

Stop all running containers

$ docker stop $(docker ps -aq)

Remove container (Can not remove running containers, must stop first)

$ docker container rm [ID]

To remove a running container use force(-f)

$ docker container rm -f [ID]

Remove multiple containers

$ docker container rm [ID] [ID] [ID]

Remove all containers

$ docker rm $(docker ps -aq)

Get logs (Use name or ID)

$ docker container logs [NAME]

List processes running in container

$ docker container top [NAME]

IMAGE COMMANDS

List the images we have pulled

$ docker image ls

We can also just pull down images

$ docker pull [IMAGE]

Remove image

$ docker image rm [IMAGE]

Remove all images

$ docker rmi $(docker images -a -q)

Some sample container creation

NGINX:

$ docker container run -d -p 80:80 --name nginx nginx (-p 80:80 is optional as it runs on 80 by default)

APACHE:

$ docker container run -d -p 8080:80 --name apache httpd

MONGODB:

$ docker container run -d -p 27017:27017 --name mongo mongo

MYSQL:

$ docker container run -d -p 3306:3306 --name mysql --env MYSQL_ROOT_PASSWORD=123456 mysql

CONTAINER INFO

View info on container

$ docker container inspect [NAME]

Specific property (--format)

$ docker container inspect --format '{{ .NetworkSettings.IPAddress }}' [NAME]

Performance stats (cpu, mem, network, disk, etc)

$ docker container stats [NAME]

ACCESSING CONTAINERS

Create new nginx container and bash into

$ docker container run -it --name [NAME] nginx bash

For Git Bash, use "winpty"

$ winpty docker container run -it --name [NAME] nginx bash

Run/Create Ubuntu container

$ docker container run -it --name ubuntu ubuntu

(no bash because ubuntu uses bash by default)

You can also make it so when you exit the container does not stay by using the -rm flag

$ docker container run --rm -it --name [NAME] ubuntu

Access an already created container, start with -ai

$ docker container start -ai ubuntu

Use exec to edit config, etc

$ docker container exec -it mysql bash

Alpine is a very small Linux distro good for docker

$ docker container run -it alpine sh

(use sh because it does not include bash) (alpine uses apk for its package manager - can install bash if you want)

NETWORKING

"bridge" or "docker0" is the default network

Get port

$ docker container port [NAME]

List networks

$ docker network ls

Inspect network

$ docker network inspect [NETWORK_NAME]
("bridge" is default)

Create network

$ docker network create [NETWORK_NAME]

Create container on network

$ docker container run -d --name [NAME] --network [NETWORK_NAME] nginx

Connect existing container to network

$ docker network connect [NETWORK_NAME] [CONTAINER_NAME]

Disconnect container from network

$ docker network disconnect [NETWORK_NAME] [CONTAINER_NAME]

Detach network from container

$ docker network disconnect

IMAGE TAGGING & PUSHING TO DOCKERHUB

tags are labels that point ot an image ID

$ docker image ls

If denied, do

$ docker login

DOCKERFILE PARTS

Build image from dockerfile (reponame can be whatever)

From the same directory as Dockerfile

$ docker image build -t [REPONAME] .

EXTENDING DOCKERFILE

Custom Dockerfile for html paqge with nginx

FROM nginx:latest # Extends nginx so everything included in that image is included here
WORKDIR /usr/share/nginx/html
COPY index.html index.html

Build image from Dockerfile

$ docker image build -t nginx-website

Running it

$ docker container run -p 80:80 --rm nginx-website

VOLUMES

Volume - Makes special location outside of container UFS. Used for databases

Bind Mount -Link container path to host path

Check volumes

$ docker volume ls

Cleanup unused volumes

$ docker volume prune

Pull down mysql image to test

$ docker pull mysql

Inspect and see volume

$ docker image inspect mysql

Run container

$ docker container run -d --name mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=True mysql

Inspect and see volume in container

$ docker container inspect mysql

Check volumes

$ docker volume ls

There is no way to tell volumes apart for instance with 2 mysql containers, so we used named volumes

Named volumes (Add -v command)(the name here is mysql-db which could be anything)

$ docker container run -d --name mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=True -v mysql-db:/var/lib/mysql mysql

Inspect new named volume

$ docker volume inspect mysql-db

TIP: Instead of typing out local path, for working directory use \$(pwd):/path/container - On windows may not work unless you are in your users folder

Run and be able to edit index.html file (local dir should have the Dockerfile and the index.html)

$ docker container run  -p 80:80 -v $(pwd):/usr/share/nginx/html nginx

Go into the container and check

$ docker container exec -it nginx bash
$ cd /usr/share/nginx/html
$ ls -al

You could create a file in the container and it will exiost on the host as well

$ touch test.txt

To run

$ docker-compose up

You can run in background with

$ docker-compose up -d

To cleanup

$ docker-compose down