Showing posts with label docker. Show all posts
Showing posts with label docker. Show all posts

docker volumes

# bind mount in container

docker run -it --name ctrName  -v ~/host/dir:/dataCtr ubuntu:latest /bin/bash
  usecase:- webserver mount directory

to debug run "docker inspect <containerId> and look for mounts to check if mounted or not

# docker volume
docker run -it --name ctrName2 -v volName:/dataCtrFolder ubuntu:latest /bin/bash

the volume will be mounted to  /dataCtrFolder  directory of the container "ctrName2"

docker volume

usecase: persisting data/share between containers eg. mysql

# mount volumes from another container eg. volume from master1 to slave1

docker run -it --name master1 -v backup:/backup -v logs:/logs ubuntu:latest /bin/bash

docker run -it --name slave1 --volumes-from master1 ubuntu:latest /bin/bash

docker volume inspect volName  # see json details


# persisting data
to export key-value env variables to container
#docker run -it -e DOC_ROOT=/var/www/html -e SHELL=/bin/bash ubuntu bash

if there are lots of env variables
 create file with valiables eg. env.list
MODE=test
DOC_ROOT=/var/www

docker run -it --env-file /path/env.list ubuntu bash

  # echo $MODE
        test
 # echo $DOC_ROOT
      /var/www

# To override the value in env.list use  -e option again
docker run -it --env-file /path/env.list -e MODE=dev ubuntu bash


part 5 dockerizing simple flask application and docker exec

### vim Dockerfile

FROM python:3.5
RUN pip install  Flask==0.11.1
RUN useradd -ms /bin/bash admin
USER admin
WORKDIR /app
COPY app /app
CMD ["python", "app.py"]

###

docker build -t urgen/dockerapp:1.01

docker images   <copy imageid>

docker run -d -p 5000:8080

    -d = run container in background
    -p = expose port
    5000 = port of container
    8080 = port of real host


user1@localhost ~ $ docker run -d -i -t busybox
8ba73738bf52630e31106be2f3a1cb4d4e0ab8f79d311f38c6ec8ff5ce7eba24

# to docker exec the container must be running in background (-d option)
docker exec -it 8ba73738bf52630e31106be2f3a1cb4d4e0ab8f79d311f38c6ec8ff5ce7eba24  sh

part 3 docker build images and commit , delete and stop running containers

Docker image is made up of list of readonly layes that represent filesystem differences. image layers are stacked on top of each other to form up a base for container's filesystem

Install GIT in a base image called ubuntu:latest

# docker run -it ubuntu:latest
apt-get update && apt-get install -y git

exit

# docker ps -a     ...grab container id


Docker commit - Create a new image from a container's changes
## commit changes and save to a new container named ubuntu:1.00
docker commit 4188d246e83c urg3n/ubuntu:1.00

docker commit d823hjhd8fj2    repositoryname:tag

# list new images created
--> docker images

docker run -it urg3n/ubuntu:1.00

## Delete container !!! ##

docker ps -a   and grab container id

docker rm 247e45581fed    ### removes container with ID 247e45581fed

part 2 docker port mapping and run in background

## remove docker container after running the command called "sleep 100"
docker run --rm busybox:1.24  sleep 100

## specify name for docker container we want to run
docker run  --name  mycontainer100 busybox:1.24
docker run  --name  mycontainer200 busybox:latest
docker ps -a

docker inspect containerid
##outpus image id , logpath,mac id

user1@localhost$ docker run -d busybox:latest sleep 10
a7393fc5cc8e1db7e87b9eb24151147

user1@localhost$ docker inspect a7393fc5cc8e1db7e87b9eb24151147


### docker tomcat
expose container port 8080 to host port 8888

docker run -it -p 8888:8080 tomcat:8.0

-->open browser--goto--> localhost:8888  it willopen tomcat server


# run container in detached mode

docker run -it -d -p 8888:8080 tomcat:8.0 

docker exec <name> /bin/cat /etc/profile
docker logs <name>

#docker run -d -p <hostport>:<containerPort>  nginx:latest

a7393fc5cc8e1db7e87b9eb24151147sf
docker logs a7393fc5cc8e1db7e87b9eb24151147sf

# Docker attach or login into the running container :-

docker exec -it containeridORname /bin/bash

# Docker management
# delete all containers
docker ps -aq | xargs docker rm -f

docker logs <cont id>   # view the output logs of container






part 1 docker getting started

add your user to docker group
# usermod -a -G docker user1

to use docker commands as user1 without logging out of system, run below command as user1

~$ newgrp docker

docker provides many ways to run commands in containers
container are about running process
focusing on "docker run"

docker run ubuntu:latest  ls -lh /etc/postfix

# get inside docker

docker run -i -t ubuntu:latest
type exit to exit



## remove docker container after running the command called "sleep 100"
docker run --rm busybox:1.24  sleep 100


## remove docker container after running the command called "sleep 100"
docker run --rm busybox:1.24  sleep 100

## specify name for docker container we want to run
docker run  --name  mycontainer100 busybox:1.24
docker run  --name  mycontainer200 busybox:latest
docker ps -a

docker inspect containerid
##outpus image id , logpath,mac id

user1@localhost$ docker run -d busybox:latest sleep 10
a7393fc5cc8e1db7e87b9eb24151147

user1@localhost$ docker inspect a7393fc5cc8e1db7e87b9eb24151147