---
# System prepended metadata

title: Quick Start Container Development Environment
tags: [Docker, tutorial]

---

# Quick Start Container Development Environment
###### tags: `Docker` `tutorial`


## Check User

```bash=
$ whoami
yylin
```

## Create script 

```bash=
$ cat <<EOF > run.sh
#!/bin/bash -xe
CONTAINER="${USER}_application_container"
START_SHELL="/bin/bash"

BASE_IMAGE=ubuntu:16.04

# test if the container is running
HASH=`docker ps -q -f name=$CONTAINER`

# test if the container is stopped
HASH_STOPPED=`docker ps -qa -f name=$CONTAINER`

if [ -n "$HASH" ];then
    echo "founding existing running container $CONTAINER, proceed to exec another shell"
    docker exec -it $HASH $START_SHELL
elif [ -n "$HASH_STOPPED" ];then
    echo "founding existing stopped container $CONTAINER, proceed to start"
    docker start --attach -i $HASH_STOPPED
else
    echo "existing container not found, createing a new one, named $CONTAINER"
    docker pull $BASE_IMAGE
    docker run --name=$CONTAINER --hostname=$CONTAINER -ti \
        -v $PWD:/app -w /app --entrypoint=$START_SHELL $BASE_IMAGE
fi
echo "see you, use'docker rm $CONTAINER'to kill the vm if you want a fresh env next time"
```

> Note: script will bind run space path in container `/app`


## Run script to build container

```bash=
$ ./run.sh

+ CONTAINER=yylin_application_container
+ START_SHELL=/bin/bash
+ BASE_IMAGE=ubuntu:16.04
++ docker ps -q -f name=yylin_application_container
+ HASH=
++ docker ps -qa -f name=yylin_application_container
+ HASH_STOPPED=
+ '[' -n '' ']'
+ '[' -n '' ']'
+ echo 'existing container not found, createing a new one, named yylin_application_container'
existing container not found, createing a new one, named yylin_application_container
+ docker pull ubuntu:16.04
16.04: Pulling from library/ubuntu
e92ed755c008: Pull complete
b9fd7cb1ff8f: Pull complete
ee690f2d57a1: Pull complete
53e3366ec435: Pull complete
Digest: sha256:db6697a61d5679b7ca69dbde3dad6be0d17064d5b6b0e9f7be8d456ebb337209
Status: Downloaded newer image for ubuntu:16.04
docker.io/library/ubuntu:16.04
+ docker run --name=yylin_application_container --hostname=yylin_application_container -ti -v /Users/yylin/workdir:/app -w /app --entrypoint=/bin/bash ubuntu:16.04
root@yylin_application_container:/app#
```


## Get the development container in you machine

```bash=
$ docker ps -a
CONTAINER ID        IMAGE                                        COMMAND                  CREATED              STATUS                      PORTS                                            NAMES
38ab0625aada        ubuntu:16.04                                 "/bin/bash"              About a minute ago   Exited (0) 3 seconds ago                                                     yylin_application_container
```

## ReRun script will login container again

```bash=
$ ./run.sh
+ CONTAINER=yylin_application_container
+ START_SHELL=/bin/bash
+ BASE_IMAGE=ubuntu:16.04
++ docker ps -q -f name=yylin_application_container
+ HASH=
++ docker ps -qa -f name=yylin_application_container
+ HASH_STOPPED=38ab0625aada
+ '[' -n '' ']'
+ '[' -n 38ab0625aada ']'
+ echo 'founding existing stopped container yylin_application_container, proceed to start'
founding existing stopped container yylin_application_container, proceed to start
+ docker start --attach -i 38ab0625aada
root@yylin_application_container:/app#
```

> 補充說明:
>> 這個 script 協助開發者更快速 container 開發自己的 project ，開發者填寫的 container image 只是單存開發環境，project與檔案都存放在 `/app`中，所以隨時可以更換 sctipt中的 image 進行切換 新build的container進行開發